开发者

When do we need to use float.PositiveInfinity and float.NegativeInfinity?

开发者 https://www.devze.com 2023-01-26 08:55 出处:网络
When do we need to use the Infinity valu开发者_高级运维es, kindly add a real-world sample if available.For example, negative infinity is a natural maximum value of an empty list. With this, you have:

When do we need to use the Infinity valu开发者_高级运维es, kindly add a real-world sample if available.


For example, negative infinity is a natural maximum value of an empty list. With this, you have: max(l1 + l2) = max(max(l1), max(l2)), where l1 and l2 are arbitrary lists, possibly empty.

A real-world application of this principle:

float Max(IEnumerable<float> list)
{
    // invariant: max contains maximum over the part of the list
    // considered so far
    float max = float.NegativeInfinity;
    foreach (float v in list)
        if (v > max)
            max = v;
    return max;
}


PostiveInfinity

This constant is returned when the result of an operation is greater than MaxValue.

NegativeInfinity

This constant is returned when the result of an operation is less than MinValue.

So you would use these constants to verify that your values are out of range for their type.

0

精彩评论

暂无评论...
验证码 换一张
取 消