# sys.float_info.epsilon

Scott David Daniels Scott.Daniels at Acm.Org
Wed Feb 4 20:52:11 CET 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?
>

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
That doesn't mean that x is the smallest representable. for example,
.0125 + epsilon / 4 != .0125

To avoid using epsilon, do something like:
if 1 + abs(x) != 1:

To learn a bit more, look into numerical analysis -- there is a whole
field dedicated to figuring out how to make floating point behave a
little bit like real numbers.  The reason it is tough is that addition
is not associative in real numbers, and associativity is at the core
of a lot of proofs in arithmetic (and group theory).

--Scott David Daniels
Scott.Daniels at Acm.Org

```