sys.float_info.epsilon

Steve Holden steve at holdenweb.com
Wed Feb 4 15:20:29 EST 2009


Tim Rowe wrote:
> I'm reading Mark Summerfield's "Programming Python 3.0" at the moment,
> and I'm puzzled by some of his uses of sys.float_info.epsilon. I
> appreciate the issues of comparing floating point numbers, but I'm
> puzzled by code like:
>     ...
>     x = float(input(msg))
>     if abs(x) < sys.float_info.epsilon:
>         ...
> 
> What could the float() conversion return that would give different results for:
>     if abs(x) < sys.float_info.epsilon
> and (to my mind, more obvious):
>     if abs(x) == 0.0
> 
> I didn't realise that float() could return anything with an absolute
> value less than sys.float_value.epsilon other than 0.0 (which I think
> all representations can represent exactly).  What am I missing here?
> 
epsilon is the difference between *1.0* and the next smallest number in
the representation.

It seems pretty obvious that the smallest representable number will be
way smaller than that. To me, at least. Think about the representation
of 1. + epsilon - the mantissa will have a leading 1, then all zeros but
for a trailing 1, and the exponent will effectively be 0.

For numbers close to zero the exponent will have a large negative
effective value (exponents are usually stored as biased positive
numbers), and so the smallest value will be tiny compared with epsilon.

Though it's a long time since I did my numerical analysis, and it isn't
clear to me exactly what the test *is* supposed to do.

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list