sys.float_info.epsilon

Mark Dickinson dickinsm at gmail.com
Wed Feb 4 15:07:23 EST 2009


On Feb 4, 7:52 pm, Scott David Daniels <Scott.Dani... at Acm.Org> wrote:
> You are missing the whole thing that mes floating point tricky.
> I _believe_ that the epsilon is the smallest positive x such that
>     1.0 != 1.0 + x

Nitpick alert:  this isn't quite the same thing, since that
definition is affected by rounding.  For example, if you're
using IEEE 754 doubles but (somehow) you've got a round-half-up
rounding mode then this makes epsilon 2**-53 rather than 2**-52,
because 1 + 2**-53 rounds to 1 + 2**-52 != 1.

(This confused the hell out of me many years ago when I was
reading Numerical Recipes, and trying to figure out why
on earth IEEE 754 and the VAX format had different epsilons
even though both had 53-bit mantissas.  The answer is that
IEEE 754 uses round-half-even as standard, and VAX uses
round-half-up.  Oh, and the authors of Numerical Recipes
used the 1 != 1+x definition...)

Python's float_info.epsilon comes straight from C's
DBL_EPSILON, which is defined very carefully in the C99 standard
as:

"""the difference between 1 and the least value greater than 1
that is representable in the given floating point type"""

(taking the 'given floating point type' to be double).

Mark




More information about the Python-list mailing list