What does float("Nan") and float("inf") mean in Python?

Asked

Viewed 11,799 times

7

I’ve seen a code like this:

def _min(*args):
    res = float('inf')
    for value in args:
        if value < res:
            res = value
    return res

I would like to know what is a float("inf") and a float("Nan").

  • 2

    inf of infinity and Nan of is not a number. Those are the tips I give. (Tips because absolute answers don’t exist with me :D )

  • 1

    The first creates an infinite number, the second creates a number that is not a "Not a Number" number. Like the result of "0.0/0.0", or "inf - inf"

  • Thanks so much for helping

  • 1

    +1 for the excellent question. See the answer in the Soen: https://stackoverflow.com/a/17628637/1377664

  • 1

    @dvd, there is not much more to say than was said in the first comment d: . Maybe some of these links will help AP https://medium.com/@sanatinia/how-to-work-with-Infinity-in-python-337fb3987f06, https://stackoverflow.com/questions/5438745/is-it-possible-to-seta-number-to-nan-or-infinity/5438756#5438756, https://stackoverflow.com/questions/7781260/how-can-i-represent-an-Infinite-number-in-python

  • @Then Miguel could post a reply.

  • That’s what I said: "Too bad no competent to answer"

  • 1

    @dvd, I didn’t attack, don’t get me wrong. I agree with you yes

  • 1

    @Miguel I know friend. I read not as attack or anything, normal rs

Show 4 more comments

2 answers

7


Infinity

As already commented, float("inf") will create an infinite number:

>>> float("inf")
inf

Which is actually equivalent to math.inf:

>>> import math
>>> math.inf
inf
>>> float("inf") == math.inf
True

It also has the -infinite counterpart:

>>> float("-inf")
-inf
>>> -float("inf")
-inf

It is important to note that any number is less than infinite, just as any number is greater than -infinite.

There is also a function in math to know if it is infinite, which is called isinf

>>> math.isinf(float("inf"))
True
>>> math.isinf(float("-inf"))
True
>>> math.isinf(3421)
False

Nan

NaN means Not a Number which means it’s not a number. It has a few different ways to get to this value. One of them is the one you saw with float("NaN"):

>>> float("NaN")
nan

Another would be subtracting infinity to infinity:

>>> float("inf") - float("inf")
nan

Any arithmetic operation on a nan will still give a nan:

>>> float("nan") * 5
nan

It also has the function isnan testing if it is nan:

>>> math.isnan(float("nan"))
True
>>> math.isnan(25)
False

Additional reading of inf - inf:

  • There’s someone with initiative (;

  • 1

    Math.inf only works from Python3 up

4

Just to complete the answer of the Isac that did not comment directly on the code snippet of the question (which is not exactly the focus, but it is interesting also to know), in functions that determine the maximum or minimum of a sequence of numbers there are two very common approaches:

  1. Start a control variable with a known value and update it as the sequence values are checked. Considering the function that determines the minimum, you start the variable with this known value and, if a given sequence value is lower than the current control value, this becomes the new value. The problem is to ensure that the known value that starts the variable is greater than any possible sequence input, so that the minimum is in fact a sequence value. If you start the variable with 9999, for example, employee for any sequence that has an input less than this value, but if it is a sequence of distance, in km, from the stars in our galaxy, the result would be wrong. Thus, by starting the variable as infinite, you circumvent the problem, because it, by definition, is greater than any numerical value (see question code). For the maximum function, the opposite happens, having to have a value less than any input, being initiated in negative infinity;

  2. The other approach is to start the variable with a non-numerical value, None, for example, and in the first value of the sequence check that if it is this non-numerical value, assign the first value of the sequence to it. Thus it is also guaranteed that the result belongs to the sequence;

Example:

def _min(*args): 
    res = None
    for value in args: 
        if res is None or value < res: 
            res = value 
    return res

Browser other questions tagged

You are not signed in. Login or sign up in order to post.