How to judge Inf and Nan in Python

Source: Internet
Author: User
This article mainly introduces how to judge Inf and Nan in Python. This article is very detailed and has some reference value for everyone. if you need it, let's take a look at it. This article mainly introduces how to judge Inf and Nan in Python. This article is very detailed and has some reference value for everyone. if you need it, let's take a look at it.

We all know that in Python, positive and negative infinity can be expressed in the following way:

Float ("inf") # positive infinity float ("-inf") # negative infinity

Exploitationinf(infinite)Multiply by 0 to getnot-a-number(NaN). If a number exceeds infiniteNaN(not a number)Number. In NaN, its exponent part is the maximum value that can be expressed, that is, FF (single precision), 7FF (double precision), and 7FFF (extended double precision ). The difference between the number of NaN and the number of infinite is that the significand part of the number of infinite is 0 (the extended bit63 bit is 1), while the significand part of the number of NaN is not 0.

Let's take a look 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>>> nan == nanFalse>>> 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 have not tried to determine whether a floating point number is NaN in Python, you will be surprised by the above output results. First of all, for positive and negative infinity and NaN itself and their use of is operations, the result is True, it seems that there is no problem here; but if the = operation is used, the result is different, naN becomes False. If we use float to redefine a variable to compare it with is and =, the results are still unexpected. The reason for this situation is a little complicated. I will not be involved here. if you are interested, please refer to the relevant information.

If you want to correctly judge the Inf and Nan values, you should use the math module'smath.isinf Andmath.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

In this way, it will be accurate. Now that I'm talking about this, I suggest you not to use is and = in Python to determine whether an object is positive or negative infinity or NaN. Use the math module.

Of course, there are other methods for judgment. the following uses NaN as an example, but we recommend using the math module to avoid confusion.

Identify yourself with 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 judge the entire 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)

Herenp.isnan Returns a boolean array. if the corresponding position is NaN, True is returned. otherwise, False is returned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.