snippetpythonCriticalCanonical
How to represent an infinite number in Python?
Viewed 0 times
howrepresentnumberinfinitepython
Problem
How can I represent an infinite number in python? No matter which number you enter in the program, no number should be greater than this representation of infinity.
Solution
In Python, you can do:
In Python 3.5, you can do:
And then:
Will always be true. Unless of course, as pointed out, x is also infinity or "nan" ("not a number").
Additionally (Python 2.x ONLY), in a comparison to
would return true.
test = float("inf")In Python 3.5, you can do:
import math
test = math.infAnd then:
test > 1
test > 10000
test > xWill always be true. Unless of course, as pointed out, x is also infinity or "nan" ("not a number").
Additionally (Python 2.x ONLY), in a comparison to
Ellipsis, float(inf) is lesser, e.g:float('inf') < Ellipsiswould return true.
Code Snippets
test = float("inf")import math
test = math.inftest > 1
test > 10000
test > xfloat('inf') < EllipsisContext
Stack Overflow Q#7781260, score: 965
Revisions (0)
No revisions yet.