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

Answer from Helpyhelperton on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › 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   July 12, 2025
Discussions

How to use Python Floating Point Numbers - Explained with Examples - freeCodeCamp Community - The freeCodeCamp Forum
Some general information about floating point numbers and how they work in Python, can be found here. Nearly all implementations of Python follow the IEEE 754 specification: Standard for Binary Floating-Point Arithmetic. More information found on the IEEE site. More on freecodecamp.org
🌐 freecodecamp.org
0
3 weeks ago
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
How stupid is the idea of having infinity included in integer type? More over, how to design a integer/floating point system that makes more sense mathematically?
As a mathematician: Infinity is not an integer. The nice thing when you don't have infinity as a term is that you can define numbers inductively, and you lose that when including infinity. On the other hand, you could implement general ordinal numbers, that could be fun. But I would have an extra type, I think. To floats: You could have arbitrary precision floats/big decimals, or rationals. Or even symbolic execution, but that is probably a bit too much, you're building a CAS at that point. More on reddit.com
🌐 r/ProgrammingLanguages
65
26
July 26, 2023
How to represent infinity in a programming language?
This behavior is described by IEEE standard 754 for floating point arithmetic, and it is implemented in your CPU and GPU hardware. Most other languages (C/C++, Java, JavaScript, etc.) use it as provided, just like Rust does. In IEEE floating point, +/- infinity is represented by a floating point value with the largest possible exponent (all 1's) and a zero mantissa. The sign bit determines whether it is a positive or negative infinity. This specific behavior is actually one of the more reasonable (and even useful) edge cases along those lines in IEEE 754. However, in general, the big selling point is that you don't need to test or throw exceptions until it is convenient: you can run big number crunching loops, then check at intervals to see if your computation has gone off the rails. In many cases, if you want floating point division-by-zero (or similar events) to throw an interrupt, you can set some flags in your floating point control registers to generate that behavior instead. More on reddit.com
🌐 r/ProgrammingLanguages
24
40
April 6, 2022
🌐
W3Schools
w3schools.com › python › ref_math_inf.asp
Python math.inf Constant
For negative infinity, use -math.inf. The inf constant is equivalent to float('inf'). ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report ...
🌐
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 - Now, here's a pro tip: Python treats these infinity values according to their mathematical properties. Positive infinity is always greater than any other number, while negative infinity is always smaller.
🌐
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'). ... print(f_inf) # inf print(type(f_inf)) # <class 'float'> ... Negative infinity can be represented by adding - to inf....
Find elsewhere
🌐
Boot.dev
boot.dev › lessons › 9a942c38-7ad9-40b7-8f4b-96b3f71d5de2
Learn to Code in Python: Find Max | Boot.dev
The built-in float() function can create a numeric floating point value of negative infinity. Instead of initializing a base value like 0 or -100000, we can use float("-inf") to represent negative infinity.
🌐
iO Flood
ioflood.com › blog › python-infinity
Python Infinity: Syntax, Uses, and Examples
February 12, 2024 - Python provides a straightforward way to represent positive and negative infinity. You can express positive infinity with float('inf') and negative infinity with float('-inf').
🌐
FavTutor
favtutor.com › blogs › infinity-python
Infinity in Python (float('inf'), math.inf) | FavTutor
1 day ago - In Python, infinity is a float value written as float("inf"). It is larger than every other number, and its negative counterpart float("-inf") is smaller than every other number.
🌐
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’).
🌐
Medium
koshurai.medium.com › infinity-in-python-156b55ae0c54
Infinity in Python. In Python, the value float("inf")… | by KoshurAI | Medium
December 31, 2022 - Infinity in Python In Python, the value float("inf") represents positive infinity, and float("-inf") represents negative infinity. You can use the math.isinf() function from the math module to check …
🌐
Codemia
codemia.io › knowledge-hub › path › how_to_implement_negative_infinity_in_python
How to implement negative infinity in Python?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
W3Schools
w3schools.com › PYTHON › ref_math_isinf.asp
Python math.isinf() Method
# Import math Library import math ... print(math.isinf(-math.inf)) Try it Yourself » · The math.isinf() method checks whether a number is infinite or not....
🌐
NumPy
numpy.org › doc › 1.25 › reference › constants.html
Constants — NumPy v1.25 Manual
Use inf because Inf, Infinity, PINF and infty are aliases for inf. For more details, see inf. ... IEEE 754 floating point representation of Not a Number (NaN). NaN and NAN are equivalent definitions of nan. Please use nan instead of NAN. ... IEEE 754 floating point representation of negative infinity.
🌐
Javatpoint
javatpoint.com › python-infinity
Python Infinity - Javatpoint
Python Infinity with tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, etc.
🌐
Quora
quora.com › How-can-I-store-an-infinity-value-in-a-Python-variable
How to store an infinity value in a Python variable - Quora
Answer (1 of 3): We can definitely store an infinity value in a Python Variable. In the below example we have assigned infinity value to a “num” variable using inf as a parameter to the float() function. We can also store negative infinity as a value in a same way Another method for assigning ...
🌐
Emeritus
emeritus.org › home › blog › coding › what is a float in python and how does it benefit programmers
What is a Float in Python and How Does it Benefit Programmers
August 12, 2025 - Using float() on floating values gives the same output as the input. ... Since infinity cannot be represented by a number in programming languages, the float() function is used to represent an infinite integer in the form of float(inf).
🌐
Splunktool
splunktool.com › how-can-i-represent-an-infinite-number-in-python
Redirecting...
June 27, 2022 - We cannot provide a description for this page right now
🌐
freeCodeCamp
freecodecamp.org › freecodecamp community
How to use Python Floating Point Numbers - Explained with Examples - freeCodeCamp Community - The freeCodeCamp Forum
3 weeks ago - Some general information about floating point numbers and how they work in Python, can be found here. Nearly all implementations of Python follow the IEEE 754 specification: Standard for Binary Floating-Point Arithmetic…