Question
Asked By – Casebash
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?
Now we will see solution for issue: Python Infinity – Any caveats?
Answer
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.
This question is answered By – Greg Hewgill
This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0