This article mainly introduces the question of how to judge the inf and Nan in Python, which is introduced in detail, it has certain reference value for everyone, and the friends who need to see it together.
We all know that in Python you can represent positive and negative infinity in the following ways:
Float ("INF") # Positive Infinity float ("-inf") # negative Infinity
inf(infinite)
multiply by 0 to get it not-a-number(NaN)
. If a number exceeds infinite, it is a NaN(not a number)
number. In the NaN number, its exponent part is the maximum value that can be expressed, i.e. FF (single precision), 7FF (double precision), and 7FFF (extended double precision). The difference between the Nan number and the infinite number is that the significand portion of the infinite number is 0 values (the BIT63 bit of the extended double precision is 1), while the significand portion of the Nan number is not a 0 value.
Let's start by looking at the following code:
>>> inf = float ("inf") >>> Ninf = float ("-inf") >>> nan = float ("nan") >>> inf is inftrue >>> Ninf is ninftrue>>> nan is nantrue>>> inf = = inftrue>>> Ninf = = NINFTRUE>>&G T Nan = = nanfalse>>> inf is a float ("INF") false>>> Ninf is a float ("-inf") false>>> nan is float ("Nan ") false>>> inf = = float (" inf ") true>>> Ninf = = Float ("-inf ") true>>> nan = = Float (" nan ") False
If you have not tried to determine whether a floating-point number is NaN in Python, you will definitely be surprised at the output above. First, for positive and negative infinity and NaN itself with the is operation, the result is True, there seems to be no problem here, but if the operation with = =, the result is not the same, NaN then becomes False. If you redefine a variable with float separately to compare it with IS and = =, the result is still unexpected. The reason for this situation is slightly complicated, here is not redundant, interested in the relevant information can be consulted.
If you want to correctly determine the INF and Nan values, then you should use the Math module's math.isinf
and math.isnan
functions:
>>> 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
This will be accurate. Now that I'm talking about this, I'm just saying: Don't try to use is and = = in Python to determine whether an object is positive or negative or NaN. You just use the math module, otherwise it's get burned.
Of course, there are other ways to make judgments, the following NaN for example, but still recommend the math module, lest confuse themselves.
Judge yourself by the object itself
>>> def isnan (num): ... return num! = num ... >>> isNaN (Float ("Nan")) True
Functions using the NumPy module
>>> import numpy as np>>> >>> Np.isnan (Np.nan) true>>> Np.isnan (float ("Nan")) True >>> Np.isnan (Float ("INF")) False
Numpy's isNaN function can also be used to determine the entire list:
>>> LST = [1, Float ("Nan"), 2, 3, Np.nan, float ("-inf"), 4, np.nan]>>> lst[1, Nan, 2, 3, Nan,-inf, 4, n An]>>> Np.isnan (LST) array ([False, True, False, False, True, False, False, True], Dtype=bool)
Returns a np.isnan
boolean array here, if the corresponding position is NaN, returns TRUE, otherwise False is returned.