Reputation: 2251
Since a very large floating value can be represented as follows:
sys.float_info.max
and float("inf")
.
Is one better than the other? If yes, under what circumstances?
Or which is one more preferred for scientific computing?
Upvotes: 2
Views: 260
Reputation: 5805
You should consider NumPy, which is likely to come into play if you're doing scientific computing. According to docs, NumPy has
numpy.inf
IEEE 754 Positive infinitynumpy.NINF
IEEE 754 Negative infinityIt's odd that one is all lower case and one is all caps.
There are also several helper functions, e.g.
numpy.isinf
which elements are + or - infinitynumpy.isposinf
which elements are + infinitynumpy.isneginf
which elements are - infinitynumpy.isfinite
which elements do not belong to NaN or infinityUnless you need crazy arbitrary high precision, NumPy could be your best choice.
Edit:
For crazy arbitrary high precision, there is also the decimal module, which provides
Decimal seems to not be case sensitive at all and +Infinity does not require a + sign.
Upvotes: 1
Reputation: 308138
This should indirectly tell you everything you need to know:
>>> float('inf') > sys.float_info.max
True
>>> float('inf') <= sys.float_info.max
False
Upvotes: 2
Reputation: 70592
Definitely not the first:
>>> import sys
>>> sys.float_info.max
1.7976931348623157e+308
That's the largest finite float.
You're missing the preferred method ;-)
>>> import math
>>> math.inf
inf
Note also:
>>> math.nan
nan
Upvotes: 6