When do we need to use the Infinity values, kindly add a real-world sample if available.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With