inf and nan in Python

Posted by longtone on Fri, 02 Aug 2019 12:21:25 +0200

In Python, positive and negative infinity can be expressed as follows

>>> float('inf') # inf is infinite, case-insensitive, and float('InF') is just as good.
inf
>>> float('-inf') # Negative infinite, case-insensitive.
-inf

When involving > and < operations, all numbers are larger than - inf and all numbers are smaller than + inf.
 

>>> float('nan')
nan

nan stands for Not A Number (not a number), which is not equal to 0, because nan is not a number, so the relevant calculations can not get numbers.

Positive infinite float('inf')

>>> float('inf') + 100
inf
>>> float('inf') - 100
inf
>>> float('inf') * 100
inf
>>> float('inf') / 100
inf
>>> float('inf') + float('inf')
inf
>>> float('inf') - float('inf')
nan
>>> float('inf') * float('inf')
inf
>>> float('inf') / float('inf')
nan
>>> 100 + float('inf')
inf
>>> 100 - float('inf')
-inf
>>> 100 * float('inf')
inf
>>> 100 / float('inf')
0.0

Negative infinite float('inf')

>>> float('-inf') + 100
-inf
>>> float('-inf') - 100
-inf
>>> float('-inf') * 100
-inf
>>> float('-inf') / 100
-inf
>>> float('-inf') + float('-inf')
-inf
>>> float('-inf') - float('-inf')
nan
>>> float('-inf') * float('-inf')
inf
>>> float('-inf') / float('-inf')
nan
>>> 100 + float('-inf')
-inf
>>> 100 - float('-inf')
inf
>>> 100 * float('-inf')
-inf
>>> 100 / float('-inf')
-0.0

It can be seen that positive infinite float('inf') and negative infinite float('inf') operations have the same advantages and disadvantages.

Operations between positive infinite float('inf') and negative infinite float('inf'):

>>> float('inf') + float('-inf')
nan
>>> float('inf') - float('-inf')
inf
>>> float('-inf') - float('inf')
-inf
>>> float('inf') * float('-inf')
-inf
>>> float('inf') / float('-inf')
nan
>>> float('-inf') / float('inf')
nan

NaN
All operations involving Nan return nan.

>>> float('nan') + 100
nan
>>> float('nan') - 100
nan
>>> float('nan') * 100
nan
>>> float('nan') / 100
nan

 
When comparing operations, all returned are False, even if the two floats ('nan') are not equal to each other.

>>> float('nan') > float('inf')
False
>>> float('nan') > float('-inf')
False
>>> float('nan') < float('inf')
False
>>> float('nan') < float('-inf')
False
>>> float('nan') == float('nan') # Be careful
False

math.isinf() and math.isnan() can be used in Python to determine whether the data is inf or nan.

>>> import math
>>> math.isinf(float('inf'))
True
>>> math.isinf(float('-inf'))
True
>>> math.isnan(float('nan'))
True

In addition, there are other methods to determine whether the data is inf and nan, but the above method is the most recommended, so other methods will not be repeated.

Positive and Negative Infinity and NaN is Sum==Judgment

>>> inf = float("inf")
>>> ninf = float("-inf")
>>> nan = float("nan")
>>> inf is inf
True
>>> ninf is ninf
True
>>> nan is nan
True
>>> inf == inf
True
>>> ninf == ninf
True
>>> nan == nan
False
>>> inf is float("inf")
False
>>> ninf is float("-inf")
False
>>> nan is float("nan")
False
>>> inf == float("inf")
True
>>> ninf == float("-inf")
True
>>> nan == float("nan")
False

First of all, for both positive and negative infinity and NaN's own operation with is, the result is True, and there seems to be no problem here; but if you use == operation, the result is different, and NaN becomes False. If you redefine a variable with float and compare it with is and ==, the result is still unexpected. The reasons for this situation are slightly more complicated, so there is no need to do it here. Interested in it, you can consult the relevant information.
Now that I'm talking about this, I'd like to advise you not to try to use is and== in Python to judge whether an object is positive or negative infinite or NaN. Use math module to OK, otherwise it will set fire to the body.

Topics: Python