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.

Reasons for using np.inf

  1. It's less to type:
  • np.inf (6 chars)
  • math.inf (8 chars; new in python 3.5)
  • float('inf') (12 chars)

That means if you already have NumPy imported you can save yourself 6 (or 2) chars per occurrence compared to float('inf') (or math.inf).

  1. Because it's easier to remember.

At least for me, it's far easier to remember np.inf than that I need to call float with a string.

Also, NumPy defines some additional aliases for infinity:

    np.Inf
    np.inf
    np.infty
    np.Infinity
    np.PINF

It also defines an alias for negative infinity:

    np.NINF

Similarly for nan:

    np.nan
    np.NaN
    np.NAN
  1. Constants are constants

This point is based on CPython and could be completely different in another Python implementation.

A float CPython instance requires 24 Bytes:

    >>> import sys
    >>> sys.getsizeof(np.inf)
    24

If you can re-use the same instance you might save a lot of memory compared to creating lots of new instances. Of course, this point is mute if you create your own inf constant but if you don't then:

    a = [np.inf for _ in range(1000000)]
    b = [float('inf') for _ in range(1000000)]

b would use 24 * 1000000 Bytes (~23 MB) more memory than a.

  1. Accessing a constant is faster than creating the variable.

     %timeit np.inf
     37.9 ns ± 0.692 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
     %timeit float('inf')
     232 ns ± 13.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
    
     %timeit [np.inf for _ in range(10000)]
     552 µs ± 15.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
     %timeit [float('inf') for _ in range(10000)]
     2.59 ms ± 78.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    

Of course, you can create your own constant to counter that point. But why bother if NumPy already did that for you.

Answer from MSeifert on Stack Overflow
🌐
NumPy
numpy.org › devdocs › reference › constants.html
Constants — NumPy v2.6.dev0 Manual
But infinity is equivalent to positive infinity. ... Try it in your browser! >>> import numpy as np >>> np.inf inf >>> np.array([1]) / 0.
Top answer
1 of 1
57

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.

Reasons for using np.inf

  1. It's less to type:
  • np.inf (6 chars)
  • math.inf (8 chars; new in python 3.5)
  • float('inf') (12 chars)

That means if you already have NumPy imported you can save yourself 6 (or 2) chars per occurrence compared to float('inf') (or math.inf).

  1. Because it's easier to remember.

At least for me, it's far easier to remember np.inf than that I need to call float with a string.

Also, NumPy defines some additional aliases for infinity:

    np.Inf
    np.inf
    np.infty
    np.Infinity
    np.PINF

It also defines an alias for negative infinity:

    np.NINF

Similarly for nan:

    np.nan
    np.NaN
    np.NAN
  1. Constants are constants

This point is based on CPython and could be completely different in another Python implementation.

A float CPython instance requires 24 Bytes:

    >>> import sys
    >>> sys.getsizeof(np.inf)
    24

If you can re-use the same instance you might save a lot of memory compared to creating lots of new instances. Of course, this point is mute if you create your own inf constant but if you don't then:

    a = [np.inf for _ in range(1000000)]
    b = [float('inf') for _ in range(1000000)]

b would use 24 * 1000000 Bytes (~23 MB) more memory than a.

  1. Accessing a constant is faster than creating the variable.

     %timeit np.inf
     37.9 ns ± 0.692 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
     %timeit float('inf')
     232 ns ± 13.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
    
     %timeit [np.inf for _ in range(10000)]
     552 µs ± 15.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
     %timeit [float('inf') for _ in range(10000)]
     2.59 ms ± 78.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    

Of course, you can create your own constant to counter that point. But why bother if NumPy already did that for you.

🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.isneginf.html
numpy.isneginf — NumPy v2.1 Manual
Test element-wise for negative infinity, return result as bool array. ... The input array. ... A location into which the result is stored. If provided, it must have a shape that the input broadcasts to. If not provided or None, a freshly-allocated boolean array is returned. ... A boolean array with the same dimensions as the input. If second argument is not supplied then a numpy boolean array is returned with values True where the corresponding element of the input is negative infinity and values False where the element of the input is not negative infinity.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.isinf.html
numpy.isinf — NumPy v2.2 Manual
numpy.isinf(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'isinf'># Test element-wise for positive or negative infinity.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-infinity
Python infinity (inf) - GeeksforGeeks
As positive infinity is always bigger than every natural number and negative infinity is always smaller than negative numbers. ... import numpy as np a = np.inf b = -np.inf c = 300 d = -300 def compare(x, y): if x>y: print("True") else: print("False") compare(a, b) compare(a, c) compare(a, d) compare(b, c) compare(b, d)
Published   July 12, 2025
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › test-array-values-for-positive-or-negative-infinity-in-numpy
Test array values for positive or negative infinity in Numpy
February 8, 2022 - import numpy as np # Create an array with some inf values arr = np.array([1, 2, 10, 50, -np.inf, 0., np.inf]) # Display the array print("Array...<br>", arr) # Get the type of the array print("\nOur Array type...<br>", arr.dtype) # Get the dimensions of the Array print("\nOur Array Dimensions...<br>",arr.ndim) # Get the number of elements in the Array print("\nNumber of elements...<br>", arr.size) # To test array for positive or negative infinity, use the numpy.isinf() method in Python Numpy print("\nTest array for positive or negative infinity...<br>",np.isinf(arr))
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.isfinite.html
numpy.isfinite — NumPy v2.6.dev0 Manual
But infinity is equivalent to positive infinity. Errors result if the second argument is also supplied when x is a scalar input, or if first and second arguments have different shapes. ... Try it in your browser! >>> import numpy as np >>> np.isfinite(1) True >>> np.isfinite(0) True >>> ...
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.6 documentation
Return a pair of integers whose ratio is exactly equal to the original float. The ratio is in lowest terms and has a positive denominator. Raises OverflowError on infinities and a ValueError on NaNs.
🌐
OrangeMantra
orangemantra.com › home
Digital Transformation & App Development Company | OrangeMantra
January 29, 2026 - OrangeMantra is one of the profound app development and digital transformation company based in India and USA .
🌐
TechFinancials
techfinancials.co.za › home › business › guide to building a profitable crypto trading bot
Guide to Building a Profitable Crypto Trading Bot
March 27, 2026 - Python is popular due to extensive library support (pandas, NumPy, ccxt) and simplicity for both beginners and experts.
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How to check NaN or infinity using NumPy? - Python - Data Science Dojo Discussions
February 21, 2023 - You can use the isfinite() method too for doing the same thing, it is an alternate method for your task and this function returns a Boolean array of the same shape as the input array, where each element is True if the corresponding element in the input array is a finite number (i.e., not NaN or infinity), and False otherwise.
🌐
NumPy
numpy.org
NumPy
Powerful N-dimensional arrays Fast and versatile, the NumPy vectorization, indexing, and broadcasting concepts are the de-facto standards of array computing today. Numerical computing tools NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more.
🌐
w3resource
w3resource.com › python-exercises › numpy › basic › numpy-basic-exercise-6.php
NumPy: Test element-wise for positive or negative infinity - w3resource
August 28, 2025 - Element-wise positive infinity check: [False False True False False False] Element-wise negative infinity check: [False False False True False False] ... The code begins by importing the NumPy library, which is essential for numerical operations on arrays. It then creates a sample NumPy array that includes a mix of finite numbers, positive infinity ('np.inf'), and negative infinity ('-np.inf').
🌐
NumPy
numpy.org › doc › stable › search.html
Search - NumPy v2.4 Manual
Created using Sphinx 7.2.6 · Built with the PyData Sphinx Theme 0.16.1
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.isinf.html
numpy.isinf — NumPy v2.4 Manual
numpy.isinf(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'isinf'># Test element-wise for positive or negative infinity.
Top answer
1 of 2
2

The period of the sine function is rather higher than what is plotted, so what you’re seeing is aliasing from the difference in the sampling frequency and some multiple of the true frequency. Since one of the roots is at 0, the smallest discrepancy that happens to exist between the first few samples and a multiple of π itself scales linearly away from 0, producing a 1/x envelope.

In this example, input[5] is 5(1000/(200-1))=8π−0.007113, so the function is about −141 there, as shown. input[10] is of course 16π−0.014226, so that the function is about −70, and so on as long as the discrepancy is much smaller than π.

It’s possible for some one of the quasi-periodic sample sequences to eventually land even closer to nπ, producing a more complicated pattern like that in the second plot.

2 of 2
1

Why does one see this decreasing tendency even though the function is purely periodic?

Keep in mind that actually at every multiple of pi the function goes to infinity. And the size of the jump displayed actually only reflects the biggest value of the sampled values where the function still made sense. Therefore you get a big jump if you happen to sample a value were the function is big but not too big to be a float.

To be able to plot anything matplotlib throws away values that do not make sense. Like the np.nan you get at multiples of pi and ±np.infs you get for values very close to that. I believe what happens is that one step size away from zero you happen to get a value small enough not to be thrown away but still very large. While when you get to pi and multiples of it the largest value gets thrown away.

How can I make a plot like this consistent i.e. independent of step-size/step-amount?

You get strange behaviour around the values where your function becomes unreasonable large. Just pick a ylimit to avoid plotting those crazy large values.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.transforms import Bbox

x = np.linspace(10**-10,50, 10**4)
plt.plot(x,1/np.sin(x))
plt.ylim((-15,15))

🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.isfinite.html
numpy.isfinite — NumPy v2.5 Manual
But infinity is equivalent to positive infinity. Errors result if the second argument is also supplied when x is a scalar input, or if first and second arguments have different shapes. ... Try it in your browser! >>> import numpy as np >>> np.isfinite(1) True >>> np.isfinite(0) True >>> ...