Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When do we need to use the Infinity values, kindly add a real-world sample if available.

like image 870
Homam Avatar asked Nov 23 '10 23:11

Homam


People also ask

What does float (' inf ') mean in Python?

But in python, as it is a dynamic language, float values can be used to represent an infinite integer. One can use float('inf') as an integer to represent it as infinity.

What is the use of float INF?

As stated in answer above, float('inf') is used for setting a variable with an infinitely large value. In simple words, it sets the value as +ve infinty.

What is float (' inf ') in Java?

Java Float isInfinite() Method The isInfinite() method of Java Float class returns a Boolean value for the specified float object or for 'v'. This method returns true if the argument passed is infinitely large in magnitude and returns false for finite floating-point arguments.

How do you handle infinity in C#?

IsInfinity() Method in C# In C#, Double. IsInfinity() is a Double struct method. This method is used to check whether a specified value evaluates to either positive infinity or negative infinity or not.


2 Answers

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;
}
like image 134
Vlad Avatar answered Oct 05 '22 13:10

Vlad


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.

like image 40
ChrisF Avatar answered Oct 05 '22 13:10

ChrisF