[Tutor] Rounding a float to n significant digits
Tim Peters
tim.peters at gmail.com
Fri Dec 1 06:20:40 CET 2006
[Dick Moores]
> ...
> But isn't there a PRECISE answer to my question?
Of course ;-)
> Or is it OT?
Well, it's really more a question about your machine's floating-point
hardware than about Python. Good explanations of exact limits for
IEEE-754 floating-point hardware can be found many places on the web.
Does it really help to know that, e.g., for an 8-byte IEEE double, the
largest representable finite positive value is exactly
(2**53-1)*2**971?
BTW, the best way to manipulate "exact" values like this is via math.ldexp:
>>> math.ldexp(2**53-1, 971) # largest finite positive double
1.7976931348623157e+308
>>> math.ldexp(2**53, 971) # add 1 to the mantissa and it's "too big"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: math range error
More information about the Tutor
mailing list