float limits

Terry Reedy tjreedy at home.com
Wed Jan 30 17:00:24 EST 2002


"Skip Montanaro" <skip at pobox.com> wrote in message
news:mailman.1012407048.27946.python-list at python.org...
>     Terry> There is a standard algorithm that numerical analysts use
to
>     Terry> calculate machine epsilon: I believe its something like
>
>     Terry> eps1 = 1.0
>     Terry> eps2 = 0.5
>     Terry> while (1.0+eps2 != 1.0):
>     Terry>    eps1 = eps2
>     Terry>    eps2 /= 2.0
>
>     Terry> I think adding 0.0 instead of 1.0 and comparing might be
>     Terry> theoretically just as good, ...

Since the two choices give different answers, I was wrong.  Which is
right depends on the question.

> Maybe even better!  Here's what I get for the two versions:
>
>     >>> eps1 = 1.0
>     >>> eps2 = 0.5
>     >>> while 1.0+eps2 != 1.0:
>     ...   eps1 = eps2
>     ...   eps2 /= 2.0
>     ...
>     >>> eps1
>     2.2204460492503131e-16
>     >>> eps2
>     1.1102230246251565e-16

This is machine epsilon, which is the quantity numerical analysts
usually want.

>     >>> eps1 = 1.0
>     >>> eps2 = 0.5
>     >>> while 0.0+eps2 != 0.0:
>     ...   eps1 = eps2
>     ...   eps2 /= 2.0
>     ...
>     >>> eps1
>     4.9406564584124654e-324

This is the min float, more or less, which is that the original poster
thought he wanted.

Thanks for running these

Terry J. Reedy








More information about the Python-list mailing list