• 10

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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)?

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')
>>> np.inf == math.inf

It also has the same type:

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

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

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

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

  2. 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 also defines some additional aliases for infinity:


    It also defines an alias for negative infinity:


    Similarly for nan:

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

    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.

  4. 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.

  • 28
Reply Report
      • 2
    • There's another reason not to use Float('Inf'): Float('Inf') is Float('Inf') returns False, while both math.inf is math.inf and np.inf is np.inf return True.
      • 1
    • @berna1111 True, but one should never actually compare numbers using ìs. is is for reference equality not for value equality. If you need to compare numbers (except nan, there you have to use an isnan function) always use ==, never is.
    • This first argument comparing the lengths of the different "inf" representations is not exactly fair, since numpy is imported as np. You could also import math as m and use m.inf (although I am not necessarily recommending this).
      • 2
    • @n1000 At the time I wrote the answer Python 3.5 wasn't that common (so math.inf simply wasn't available to almost everyone) so the comparison was more aimed at the float('inf') (and more oriented at the usual import names). However since one can easily create the constant themselves the point is actually mute. It depends mostly on which package you already have imported, because an additional import just to use inf would be a bit pointless. But thank you for bringing this up.

Trending Tags