Difference Between Np. Inf and Float('Inf')

Difference Between Np. Inf and Float('Inf')

Is there some difference between NumPy np.inf and float('Inf')? float('Inf') == np.inf returns True, so it seems they are interchangeable, thus I was wondering why NumPy has defined its own "inf" constant, and when should I use one constant instead of the other (considering style concerns too)?

2

1 Answer

TL, DR: There is no difference and they can be used interchangeably.

Besides having the same value as math.inf and float('inf'):

>>> import math
>>> import numpy as np

>>> np.inf == float('inf')
True
>>> np.inf == math.inf
True

It also has the same type:

>>> import numpy as np
>>> type(np.inf)
float
>>> type(np.inf) is type(float('inf'))
float

That's interesting because NumPy also has it's own floating point types:

>>> np.float32(np.inf)
inf
>>> type(np.float32(np.inf))
numpy.float32
>>> np.float32('inf') == np.inf  # nevertheless equal
True

So it has the same value and the same type as math.inf and float('inf') which means it's interchangeable.

Sophia Al-Mansoor
Author

Sophia Al-Mansoor

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.