[Python-ideas] Python Numbers as Human Concept Decimal System
Steven D'Aprano
steve at pearwood.info
Sun Mar 9 04:15:16 CET 2014
On Sat, Mar 08, 2014 at 09:12:53PM -0500, Alexander Belopolsky wrote:
> On Sat, Mar 8, 2014 at 7:39 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>
> > Computer maths cannot fail to give surprises.
> >
> > py> x = 1/Decimal(3)
> > py> sum([x, x, x]) == 1
> > False
> >
> > "No surprises" is simply an impossible request.
> >
>
> Maybe, but there is nothing impossible in eliminating this particular
> surprise:
Of course not. Numeric surprises are like bubbles under wallpaper: you
can push them around, move them from place to place, but you cannot
eliminate them entirely. (The analogy is not perfect: with wallpaper,
you can push the bubble out to the edge, and at least sometimes,
eliminate it.) By using Decimal, you eliminate one class of surprises
("why does my number not equal the decimal digits I typed in?") but at
the cost of creating new surprises ("why is the average of x and y not
always between x and y?").
> q)x:1 % 3f
> q)x
> 0.3333333
> q)1 = x + x + x
> 1b
If I were to write:
y:0.3333333
I'd expect y to equal x, since that's what x looks like. If it didn't,
that would be a surprise. If it did, that would be a surprise too,
because when I went to school, adding y three times ought to get
0.9999999 and not 1.
Since Q is based on APL, I'm guessing that the surprise here is
actually in the = equals operator. Is it doing a fuzzy comparison? If
so, that's also surprising, since that means you can have triplets of
numbers such that a = b and b = c but a != c.
> http://en.wikipedia.org/wiki/Q_(programming_language_from_Kx_Systems)
>
> I am not suggesting that Python should behave like Q in this respect, just
> showing that this particular wart is not present in all languages.
--
Steven
More information about the Python-ideas
mailing list