float limits

Tim Peters tim.one at home.com
Wed Jan 30 17:54:32 EST 2002


Folks, don't try to compute these things yourself -- you won't get it right.
There's a reason Pemberton's fine enquire.c has bloated to more than 3K
lines over the years <wink>.  Example:

> eps1 = 1.0
> eps2 = 0.5
> while (1.0+eps2 != 1.0):
>     eps1 = eps2
>     eps2 /= 2.0

Do that in C on a Pentium box, and due to the vagaries of your compiler you
may well end up measuring the width of the 80-bit HW fp registers instead of
the width of a C double.  Python frustrates the hell out of C optimizers so
completely that it's much safer to try stuff like this *in* Python, but it
remains vulnerable to compiler advances.  Even if the above avoids the
80-bit registers, if the 754 rounding mode happens to be set to plus-inf,
the inequality will hold until eps2 underflows to 0 (which won't happen
until > 1000 loop iterations go by).

Also search http://www.netlib.org/; the scientific community has been
wrestling with machine-limit discovery problems for decades.





More information about the Python-list mailing list