python 3.44 float addition bug?
Steven D'Aprano
steve at pearwood.info
Thu Jun 26 00:17:43 EDT 2014
On Thu, 26 Jun 2014 13:13:45 +1000, Chris Angelico wrote:
> On Thu, Jun 26, 2014 at 12:56 PM, Steven D'Aprano <steve at pearwood.info>
> wrote:
>> That's a myth. decimal.Decimal *is* a floating point value, and is
>> subject to *exactly* the same surprises as binary floats, except for
>> one: which Decimal, you can guarantee that any decimal string you enter
>> will appear exactly the same (up to the limit of the current
>> precision).
>
> The important difference is that the issues with decimal floats come
> where humans are comfortable seeing them. If you divide 1 by 3, you get
> 0.333333333 and can understand that adding three of those together won't
> quite make 1.0, because you can see that you shortened it. If you divide
> 11 by 10, it's not obvious that that repeats.
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.
--
Steven
More information about the Python-list
mailing list