At 09:20 PM 11/30/2006, Tim Peters wrote: >[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.
Well, I did find (2 - 2**-52)*2**1023 on http://babbage.cs.qc.edu/IEEE-754/References.xhtml , which I realize now is equivalent to your (2**53-1)*2**971. I can't say I understand the explanation. >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? Well, I like your round_to_n() function, and it's useful to know at what point it will fail. round_to_n(1.7976931348623158e+308,16) will compute on my machine, but round_to_n(1.7976931348623159e+308,16) will not. I was also curious as to what the precise max integer to float would be, even if that integer is not precisely relevant to your function. Far from it. Even this computes: >>> i = (2**53-1)*2**971 >>> >>> round_to_n(i+10**291.999, 16) '1.797693134862316e+308' (but this will not: round_to_n(i+10**292, 16) ) For people who have lost my original post, here's Tim's round_to_n() again: ==================================================== def round_to_n(x, n): """ Rounds float x to n significant digits, in scientific notation. Written by Tim Peters. See his Tutor list post of 7/3/04 at http://mail.python.org/pipermail/tutor/2004-July/030324.html """ if n < 1: raise ValueError("number of significant digits must be >= 1") return "%.*e" % (n-1, x) ======================================================== >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 Thanks for that. Dick _______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
