In Python, you can do:
test = float("inf")
In Python 3.5, you can do:
import math
test = math.inf
And then:
test > 1
test > 10000
test > x
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 Ellipsis, float(inf) is lesser, e.g:
float('inf') < Ellipsis
would return true.
Answer from WilHall on Stack OverflowIn Python, you can do:
test = float("inf")
In Python 3.5, you can do:
import math
test = math.inf
And then:
test > 1
test > 10000
test > x
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 Ellipsis, float(inf) is lesser, e.g:
float('inf') < Ellipsis
would return true.
Since Python 3.5 you can use math.inf:
>>> import math
>>> math.inf
inf
float('inf') is bad practice
algorithm - How to implement negative infinity in Python? - Stack Overflow
Testing for positive infinity, or negative infinity, individually in Python - Stack Overflow
A value doesn't print()
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?
Python has special values float('inf') and float('-inf').
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.
The "pythonic" way is to go with what's readable and maintainable.
That said, x == float("inf") and x == float("-inf") are slightly more readable to me, and I'd prefer them. math.isinf(x) and x > 0 is faster, but only on the order of about 40 nanoseconds per call.
So unless you're checking a whole lot of numbers, it isn't going to make much of a difference in running time.
there is also numpy
>>> import numpy as np
>>> np.isneginf([np.inf, 0, -np.inf])
array([False, False, True], dtype=bool)
>>> np.isposinf([np.inf, 0, -np.inf])
array([ True, False, False], dtype=bool)
and then there is general isinf
>>> np.isinf([np.inf, 0, -np.inf])
array([ True, False, True], dtype=bool)
So in my imagined language, efficiency is not an issue. I decide to use arbitrary precision integers(i.e. big ints). I realize that sometimes you need infinity as a boundary, so I'm curious, how bad is the idea of having positive/negative infinity in integer type?
I know the fact that you have more UBs, like 0*inf doesn't make sense, but it's fundamentally the same as div by 0 problems. And it should be solved the same as div by 0s.
And for floating numbers, we're all plagued by precision problems, so I think it should make sense for any floating number to be encoded by x = (a, b), where it means that: a - b < x < a + b, and as you do floating point arithemetic, b grows and you lose the precision.
In general, is there any efforts on designing a number system for both integer/floating nums that make more sense mathematically, when you don't care about performance?
EDIT: just realized that haskell have both NAN and INF included in the language.