python 3.44 float addition bug?

Chris Angelico rosuav at gmail.com
Thu Jun 26 00:41:05 EDT 2014


On Thu, Jun 26, 2014 at 2:17 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> I'm not sure if you're agreeing with me or disagreeing with me.
>
> "Repeats" is a property of a number *in a specific base*, not of the
> number itself. So 1/3 does not repeat in base 3, where it would be
> written as the terminating trinary number 0.1. Likewise, 11/10 repeats in
> base 2, but not in base 10.
>
> What I am I saying is that regardless of whether you use binary floats or
> base-10 Decimals, not all rational numbers x/y can be represented
> exactly. I certainly wasn't saying that the same rationals are inexact in
> both bases, just that the surprise "x/y is not exact" occurs whether you
> have binary or decimal floating point numbers.
>
> Likewise for all other floating point issues, except the surprise "this
> base-2 float is not exactly equal to the base-10 number I typed". Because
> Decimal is base-10, what you type is what you get.

Broadly agreeing, but with the caveat that, as you say, "repeats"
depends on the base - and there's one base that most humans use, and
it's not the base that IEEE floats use. So when I said that 11/10
repeats, I mean that it repeats in binary, which causes precision
problems with binary floats; but everyone who's done basic work with
vulgar and decimal fractions understands that 1/3 can't be perfectly
represented in decimal.

So the problems with floating point representations are indeed common
to float and decimal.Decimal, but they're more surprising with float
because humans aren't used to tenths repeating.

ChrisA



More information about the Python-list mailing list