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

5

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").

    
asked by anonymous 07.04.2018 / 03:01

2 answers

4

Infinite

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's 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 isin

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

NaN

NaN means Not a Number which means it is not a number. There are a few different ways to reach 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 give it a nan :

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

It also has the function isnan which tests whether it is nan :

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

Additional reading of inf - inf :

07.04.2018 / 11:02
3

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

  • 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 certain value of the sequence is less than the current value of control, it becomes the new value. The problem is to ensure that the known value that starts the variable is greater than any possible input of the sequence, so that the minimum is, in fact, a value of the sequence. If you start the variable with 9999, for example, employee for any sequence that has an input smaller than this value, but if it is a sequence of miles away from the stars in our galaxy, the result would be wrong. So, by starting the variable as infinite, you get around the problem, since it by definition is greater than any numeric value (see question code). For the maximum function, the opposite happens, having to have a value less than any input, starting at negative infinity;

  • The other approach is to start the variable with a non-numeric value, None , for example, and in the first value of the sequence verify that if this value is not numeric, assign the first value of the sequence to it. So 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
    
        
    07.04.2018 / 15:54