Python has special values float('inf') and float('-inf').
Reddit
reddit.com › r/python › float('inf') is bad practice
r/Python on Reddit: float('inf') is bad practice
April 15, 2024 -
I don't know why float('inf') was chosen as the way the language accesses infinity since this is using a magic string. Why couldn't it just be float.inf? That way magic string is avoided. Feels too basic of a best practice to be simply passed up. Anyone know the reason behind this?
Top answer 1 of 5
198
math.inf is there for you, and I prefer it over float('inf') in most if not all cases.
2 of 5
112
it's not magic. It's just a printable value. The (str) and str() constructors are intentionally compatible with each other. I'm not certain, but the value 'inf' is likely to be compatible with the C library Python is built on top of. Consider the following C code: #include #include #include int main(int argc, char** argv) { float f = atof("inf"); assert(f == INFINITY); } Use math.inf if you don't want to use strings.
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
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
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
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
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
Top answer 1 of 4
76
Python has special values float('inf') and float('-inf').
2 of 4
6
As it happens, in Python 2, None is less than any integer, so you can use None. In Python 3 you have (at least) four choices:
- Use min(A) - 1.
- Use
None, and whenever you compare two values, explicitly test for them beingNone. - Define a new data type that consists of either an integer or -∞, and handles comparisons correctly.
- Modify the algorithm so that line 2 is eliminated. You will have to patch
Heap-Increase-Keysomehow.
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 ...
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').
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
File Organizer: Write a Python program that organizes the file in a directory based on the extension ... Generate a Vandermonde Matrix of the Legendre Polynomial with a Float Array of Points in Python using NumPy
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