[Python-ideas] Python Float Update
Chris Angelico
rosuav at gmail.com
Mon Jun 1 18:20:04 CEST 2015
On Tue, Jun 2, 2015 at 12:58 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> This one is even worse:
>
> py> getcontext().prec = 1
> py> x = D('51.6')
> py> y = D('51.8')
> py> (x+y)/2 # should be 51.7
> Decimal('5E+1')
>
> Instead of the correct answer of 51.7, Decimal calculates the answer as
> 50 exactly.
To be fair, you've actually destroyed precision so much that your
numbers start out effectively equal:
>>> from decimal import Decimal as D, getcontext
>>> getcontext().prec = 1
>>> x = D('51.6')
>>> y = D('51.8')
>>> x == y
False
>>> x + 0 == y + 0
True
They're not actually showing up as equal, but only because the
precision setting doesn't (apparently) apply to the constructor. If
adding zero to both sides of an equation makes it equal when it wasn't
before, something seriously screwy is going on.
(Actually, this behaviour of decimal.Decimal reminds me very much of
REXX. Since there are literally no data types in REXX (everything is a
string), the numeric precision setting ("NUMERIC DIGITS n") applies
only to arithmetic operations, so the same thing of adding zero to
both sides can happen.)
So what you're really doing here is averaging 5E+1 and 5E+1, with an
unsurprising result of... 5E+1. Your other example is more significant
here, because your numbers actually do fit inside the precision limits
- and then the end result slips outside the bounds.
ChrisA
More information about the Python-ideas
mailing list