# Python etc / float (IEEE 754)

Published: 18 March 2021, 18:00

Floating point numbers in Python and most of the modern languages are implemented according to IEEE 754. The most interesting and hardcore part is “arithmetic formats” which defines a few special values:

• inf and -inf representing infinity.
• nan representing a special “Not a Number” value.
• -0.0 representing “negative zero”

Negative zero is the easiest case, for all operations it considered to be the same as the positive zero:

-.0 == .0  # True
-.0 < .0   # False


Nan returns False for all comparison operations (except !=) including comparison with inf:

import math

math.nan < 10        # False
math.nan > 10        # False
math.nan < math.inf  # False
math.nan > math.inf  # False
math.nan == math.nan # False
math.nan != 10       # True


And all binary operations on nan return nan:

math.nan + 10  # nan
1 / math.nan   # nan


Infinity is bigger than anything else (except nan). However, unlike in pure math, infinity is equal to infinity:

10 < math.inf         # True
math.inf == math.inf  # True


The sum of positive and negative infinity is nan:

-math.inf + math.inf  # nan