Python has special values float('inf') and float('-inf').

Answer from Helpyhelperton on Stack Overflow
🌐
FavTutor
favtutor.com › blogs › infinity-python
Infinity in Python (float('inf'), math.inf) | FavTutor
1 day ago - Two operations are undefined and return nan (not a number): inf - inf and inf / inf. forever = float("inf") print(forever + 1000) # Outputs: inf print(forever * -2) # Outputs: -inf print(1 / forever) # Outputs: 0.0 print(forever - forever) # ...
🌐
W3Schools
w3schools.com › python › ref_math_inf.asp
Python math.inf Constant
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... # Import math Library import math # Print the positive infinity print (math.inf) # Print the negative infinity print (-math.inf) Try it Yourself » · The math.inf constant returns a floating...
Discussions

algorithm - How to implement negative infinity in Python? - Stack Overflow
Python has special values float('inf') and float('-inf'). ... Sign up to request clarification or add additional context in comments. ... I prefer using the inf constant from the math module (from math import inf). You can then specify -inf for negative infinity. More on stackoverflow.com
🌐 stackoverflow.com
How to represent an infinite number in Python? - Stack Overflow
This might explain why Python have chosen this akward syntax. 2015-03-05T12:00:52.173Z+00:00 ... And you can do -float("inf") to get minus infinity which is smaller than any other number. 2021-01-06T11:16:44.74Z+00:00 ... Save this answer. ... Show activity on this post. ... Save this answer. ... Show activity on this post. No one seems to have mentioned about the negative ... More on stackoverflow.com
🌐 stackoverflow.com
Python Infinity - Any caveats? - Stack Overflow
So Python has positive and negative infinity: float("inf"), float("-inf") This just seems like the type of feature that has to have some caveat. Is there anything I should be aware of? More on stackoverflow.com
🌐 stackoverflow.com
float('inf') is bad practice
math.inf is there for you, and I prefer it over float('inf') in most if not all cases. More on reddit.com
🌐 r/Python
72
79
April 15, 2024
🌐
Medium
koshurai.medium.com › infinity-in-python-156b55ae0c54
Infinity in Python. In Python, the value float("inf")… | by KoshurAI | Medium
December 31, 2022 - For example: x = float("inf") y = 1 print(x > y) # True print(x + y) # inf print(x - y) # inf print(x * y) # inf print(x / y) # inf · If you need to perform arithmetic operations with infinity, you can use the float("inf") and float("-inf") values as sentinel values to represent infinity in your code...
🌐
Flexiple
flexiple.com › python › python-infinity
Python Infinity - All you need to know | Flexiple Tutorials - Flexiple
March 14, 2022 - Since Infinite numbers are both positive and negative, Therefore, in Python, they can be represented using float(‘inf’) and float(‘-inf’).
🌐
iO Flood
ioflood.com › blog › python-infinity
Python Infinity: Syntax, Uses, and Examples
February 12, 2024 - For instance, the result of adding positive and negative infinity is not a number (nan), which might not be the expected result. positive_infinity = float('inf') negative_infinity = float('-inf') print(positive_infinity + negative_infinity) # nan
🌐
Note.nkmk.me
note.nkmk.me › home › python
Infinity (inf) in Python | note.nkmk.me
August 11, 2023 - You can create inf with float('inf'). Other methods, such as using the math module or the NumPy library, are described later. f_inf = float('inf') print(f_inf) # inf print(type(f_inf)) # <class 'float'> ... Negative infinity can be represented ...
Find elsewhere
🌐
CodeRivers
coderivers.org › blog › python-float-negative-infinity
Python Float Negative Infinity: An In - Depth Exploration - CodeRivers
March 25, 2025 - For example, dividing by negative infinity results in zero, but this can lead to unexpected behavior in some algorithms. Always double - check your code to ensure that the operations involving negative infinity are appropriate for your application. # Division by negative infinity result_div ...
🌐
GeeksforGeeks
geeksforgeeks.org › python-infinity
Python infinity (inf) - GeeksforGeeks
Below is the list of ways one can represent infinity in Python. As infinity can be both positive and negative they can be represented as a float('inf') and float('-inf') respectively.
Published   April 16, 2025
🌐
CodeRivers
coderivers.org › blog › python-negative-infinity
Python Negative Infinity: A Comprehensive Guide - CodeRivers
February 22, 2026 - To initialize a variable with negative infinity in Python, you simply assign the value float('-inf') to it. Here is an example:
🌐
Finxter
blog.finxter.com › home › learn python blog › python infinity
Python Infinity – Be on the Right Side of Change
September 16, 2020 - Even greater than",number) infinity_negative = float('-Inf') if -number < infinity_negative: print("number is lesser than Negative Infinity!") else: print("Negative Infinity is the least!
🌐
Medium
alysialfi.medium.com › python-float-inf-js-number-negative-infinity-5e4350e84b94
Python float(‘-inf’) & JS Number.NEGATIVE_INFINITY | by Alysia Alfi | Medium
June 10, 2025 - This approach doesn’t make 0 the minimum. float(‘-inf’) & Number.NEGATIVE_INFINITY use as a starting point that’s guaranteed to be smaller than any actual number in the array, ensuring the algorithm finds the true maximum value.
🌐
CodeGym
codegym.cc › java blog › learning python › infinity in python: mastering the infinite
Infinity in Python: Mastering the Infinite
August 13, 2024 - positive_infinity = float('inf') print(positive_infinity) # Output: inf ... You're catching on to everything so quickly! Now, here's a pro tip: Python treats these infinity values according to their mathematical properties.
🌐
Python
docs.python.org › 3.2 › library › functions.html
2. Built-in Functions — Python v3.2.6 documentation
The argument may also be a string ... or negative infinity. More precisely, the input must conform to the following grammar after leading and trailing whitespace characters are removed: sign ::= "+" | "-" infinity ::= "Infinity" | "inf" nan ::= "nan" numeric_value ::= floatnumber | infinity | nan numeric_string ::= [sign] numeric_value · Here floatnumber is the form of a Python floating-point literal, described in Floating point literals. Case is not significant, so, for example, “inf”, ...
🌐
CodeRivers
coderivers.org › blog › negative-infinity-python
Understanding Negative Infinity in Python - CodeRivers
March 18, 2025 - To create a variable representing ... than any other real number. Consider the following examples: negative_inf = float('-inf') number = 10 print(negative_inf < number) print(negative_inf < float('-1e100'))...
🌐
STechies
stechies.com › python-infinity
Infinity in Python - Represent an Infinite Number in Python
... import math # Define positive infinity number ptive_inf = float('inf') # Define negative infinity number ntive_inf = float('-inf') print('Positive infinity equal to Negative infinity: ',ptive_inf == ntive_inf)
Top answer
1 of 6
105

Python's implementation follows the IEEE-754 standard pretty well, which you can use as a guidance, but it relies on the underlying system it was compiled on, so platform differences may occur. Recently¹, a fix has been applied that allows "infinity" as well as "inf", but that's of minor importance here.

The following sections equally well apply to any language that implements IEEE floating point arithmetic correctly, it is not specific to just Python.

Comparison for inequality

When dealing with infinity and greater-than > or less-than < operators, the following counts:

  • any number including +inf is higher than -inf
  • any number including -inf is lower than +inf
  • +inf is neither higher nor lower than +inf
  • -inf is neither higher nor lower than -inf
  • any comparison involving NaN is false (inf is neither higher, nor lower than NaN)

Comparison for equality

When compared for equality, +inf and +inf are equal, as are -inf and -inf. This is a much debated issue and may sound controversial to you, but it's in the IEEE standard and Python behaves just like that.

Of course, +inf is unequal to -inf and everything, including NaN itself, is unequal to NaN.

Calculations with infinity

Most calculations with infinity will yield infinity, unless both operands are infinity, when the operation division or modulo, or with multiplication with zero, there are some special rules to keep in mind:

  • when multiplied by zero, for which the result is undefined, it yields NaN

  • when dividing any number (except infinity itself) by infinity, which yields 0.0 or -0.0².

  • when dividing (including modulo) positive or negative infinity by positive or negative infinity, the result is undefined, so NaN.

  • when subtracting, the results may be surprising, but follow common math sense:

  • when doing inf - inf, the result is undefined: NaN;

  • when doing inf - -inf, the result is inf;

  • when doing -inf - inf, the result is -inf;

  • when doing -inf - -inf, the result is undefined: NaN.

  • when adding, it can be similarly surprising too:

  • when doing inf + inf, the result is inf;

  • when doing inf + -inf, the result is undefined: NaN;

  • when doing -inf + inf, the result is undefined: NaN;

  • when doing -inf + -inf, the result is -inf.

  • using math.pow, pow or ** is tricky, as it doesn't behave as it should. It throws an overflow exception when the result with two real numbers is too high to fit a double precision float (it should return infinity), but when the input is inf or -inf, it behaves correctly and returns either inf or 0.0. When the second argument is NaN, it returns NaN, unless the first argument is 1.0. There are more issues, not all covered in the docs.

  • math.exp suffers the same issues as math.pow. A solution to fix this for overflow is to use code similar to this:

      try:
          res = math.exp(420000)
      except OverflowError:
          res = float('inf')
    

Notes

Note 1: as an additional caveat, that as defined by the IEEE standard, if your calculation result under-or overflows, the result will not be an under- or overflow error, but positive or negative infinity: 1e308 * 10.0 yields inf.

Note 2: because any calculation with NaN returns NaN and any comparison to NaN, including NaN itself is false, you should use the math.isnan function to determine if a number is indeed NaN.

Note 3: though Python supports writing float('-NaN'), the sign is ignored, because there exists no sign on NaN internally. If you divide -inf / +inf, the result is NaN, not -NaN (there is no such thing).

Note 4: be careful to rely on any of the above, as Python relies on the C or Java library it was compiled for and not all underlying systems implement all this behavior correctly. If you want to be sure, test for infinity prior to doing your calculations.


¹) Recently means since version 3.2.
²) Floating points support positive and negative zero, so: x / float('inf') keeps its sign and -1 / float('inf') yields -0.0, 1 / float(-inf) yields -0.0, 1 / float('inf') yields 0.0 and -1/ float(-inf) yields 0.0. In addition, 0.0 == -0.0 is true, you have to manually check the sign if you don't want it to be true.

2 of 6
98

You can still get not-a-number (NaN) values from simple arithmetic involving inf:

>>> 0 * float("inf")
nan

Note that you will normally not get an inf value through usual arithmetic calculations:

>>> 2.0**2
4.0
>>> _**2
16.0
>>> _**2
256.0
>>> _**2
65536.0
>>> _**2
4294967296.0
>>> _**2
1.8446744073709552e+19
>>> _**2
3.4028236692093846e+38
>>> _**2
1.157920892373162e+77
>>> _**2
1.3407807929942597e+154
>>> _**2
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
OverflowError: (34, 'Numerical result out of range')

The inf value is considered a very special value with unusual semantics, so it's better to know about an OverflowError straight away through an exception, rather than having an inf value silently injected into your calculations.

🌐
Kodeclik
kodeclik.com › inf-in-python
Infinity in Python: float('inf'), math.inf, and Algorithms
June 16, 2025 - The code creates two variables, one for positive infinity by calling float('inf') and another for negative infinity by calling float('-inf'). When these variables are printed, Python outputs inf for the positive infinity and -inf for the negative ...