# Python NumPy For Your Grandma - 3.6 infinity

Contents

In this section, we’ll see how you can represent infinite values in NumPy.

Like `nan`, numpy reserves floating point constants for infinity and negative infinity that behave specially. If you want to insert these values directly, you can use `np.inf` and `np.NINF`

``````import numpy as np

np.array([np.inf, np.NINF])
## array([ inf, -inf])
``````

More commonly, these values occur when you divide by 0.

``````np.array([-1, 1])/0
## array([-inf,  inf])
##
## <string>:1: RuntimeWarning: divide by zero encountered in true_divide
``````

All the special behaviors you might expect for these values exist such as,

``````# multiplying infinity by a positive constant equals infinity
np.inf * 22

# adding infinity to infinity equals infinity
## inf
np.inf + np.inf

# subtracting infinity from infinity has undefined behavior and produces nan
## inf
np.inf - np.inf

# dividing infinity by infinity has undefined behavior and produces nan
## nan
np.inf / np.inf
## nan
``````

Unlike `nan`, positive infinity equals positive infinity and negative infinity equals negative infinity.

``````np.inf == np.inf
## True
np.NINF == np.NINF
## True
``````

So, if you have an array with infinite values like this one, you can find them just by checking `==np.inf` or `==np.NINF`.

``````foo = np.array([4.4, np.inf, 1.0, np.NINF, 3.1, np.inf])

foo == np.inf
## array([False,  True, False, False, False,  True])
foo == np.NINF
## array([False, False, False,  True, False, False])
``````

Alternatively, you can also use the functions `isposinf()`, `isneginf()` and `isinf()`.

``````np.isposinf(foo)
## array([False,  True, False, False, False,  True])
np.isneginf(foo)
## array([False, False, False,  True, False, False])
np.isinf(foo)
## array([False,  True, False,  True, False,  True])
``````