Floating point errors?

Tim Peters tim.one at home.com
Thu May 10 21:39:57 EDT 2001


[Ben Allfree]
> int( (2.3 - 2) * 1000 ) = 299
> int( .3 * 1000 ) = 300
>
> What am I missing? The first one seems too simple to expect a rounding
> error :)

[Erik Max Francis]
> Floating point values are not exact.

Harrumph.  Of course they're exact!  Selling them as some sort of fuzzy
mystery is what keeps people confused about them 50 years after they should
have learned better.

> >>> 2.3 - 2.0
> 0.29999999999999982

And on an IEEE-754 machine, that's the rounded *display* of an exact number,
precisely

   0.29999999999999982236431605997495353221893310546875

OTOH, the 754 double closest to 0.3 is exactly

   0.299999999999999988897769753748434595763683319091796875

Now I'm sure Ben feels confident about his numeric future <wink>.

all-obvious-to-the-most-casual-observer-provided-only-they-think-
    in-base-2-ly y'rs  - tim





More information about the Python-list mailing list