The judgment of Inf and Nan in Python is explained in detail

  • 2020-05-26 09:22:00
  • OfStack

You know that in Python you can write plus or minus infinity in the following way:


float("inf") #  Is infinite 
float("-inf") #  Minus infinity 

using inf(infinite) You multiply it by 0 and you get not-a-number(NaN) . If you have more than infinite, you have 1 NaN(not a number) The number. In the NaN number, its exponent part is the maximum expressible value, that is, FF (single precision), 7FF (double precision), and 7FFF (extended double precision). The difference between NaN number and infinite number is that the significand part of infinite number is 0 (the bit63 bit of extended double precision is 1). The significand part of the NaN number is not 0.

Let's look at the following code first:


>>> 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

If you haven't tried to determine if a floating point number is NaN in Python, you'll be surprised at the output. First of all, for plus or minus infinity and NaN itself and is itself, the result is True, there seems to be no problem here; But if you use the == operation, the result is not the same. NaN becomes False. If you redefine a variable with float and compare it with is and ==, the result is still surprising. The reason that appears this kind of circumstance is a little bit more complex, here not redundant art, interested can consult relevant data.

If you want to correctly judge the Inf and Nan values, then you should use the math module math.isinf and math.isnan Function:


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

That makes it accurate. Now that I'm talking about it, a word of advice: don't try to use is and == in Python to determine whether an object is plus or minus infinity or NaN. Just use the math module, or you'll get burned.

Of course, there are other ways to make judgments. NaN is used as an example below, but the math module is still recommended to avoid confusing yourself.

Judge yourself by the object itself


>>> def isnan(num):
...  return num != num
... 
>>> isnan(float("nan"))
True

Use the functions of the numpy module


>>> import numpy as np
>>> 
>>> np.isnan(np.nan)
True
>>> np.isnan(float("nan"))
True
>>> np.isnan(float("inf"))
False

The isnan function of Numpy can also judge the whole list:


>>> lst = [1, float("nan"), 2, 3, np.nan, float("-inf"), 4, np.nan]
>>> lst
[1, nan, 2, 3, nan, -inf, 4, nan]
>>> np.isnan(lst)
array([False, True, False, False, True, False, False, True], dtype=bool)

Here, np.isnan Returns an array of Boolean values, True if the corresponding location is NaN, False if not.

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: