float limits

Skip Montanaro skip at pobox.com
Wed Jan 30 11:09:29 EST 2002


    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, ...

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
    >>> eps1 = 1.0
    >>> eps2 = 0.5
    >>> while 0.0+eps2 != 0.0:
    ...   eps1 = eps2
    ...   eps2 /= 2.0
    ... 
    >>> eps1
    4.9406564584124654e-324
    >>> eps2
    0.0

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)




More information about the Python-list mailing list