Bug in Decimal??

Mark Dickinson mdickinson at enthought.com
Sun May 4 06:53:06 EDT 2014


On Tue, 29 Apr 2014 19:37:17 -0700, pleasedontspam wrote:

> Hello, I believe I found a bug in the Decimal library. The natural logarithm
> results seem to be off in a certain range, as compared with Wolfram Alpha.

I had a quick look: this isn't a bug - it's just the result of propagation of
the error in "partial" to "final".

In more detail: we've got a working precision of 2016 significant figures.  For
any small x, we have (1 + x) / (1 - x) = 1 + 2x + 2x^2 + 2x^3 + ....  For your
value of x, `Decimal('1e-1007'), we've got enough precision to store
1 + 2x + 2x^2 exactly, but that's all.

So partial has an absolute error of around 2x^3, or 2e-3021.

And since the derivative of the natural log function is almost exactly 1 at the
point we're interested in, we expect the absolute error in the output to be
close to 2e-3021, too.

And that's *precisely* what you're seeing: the Decimal module is giving you a
result that's exactly `Decimal('2e-1007') - Decimal('1.3e-3021')`, while the
result you were expecting is `Decimal('2e-1007') + Decimal('0.7e-3021')`.  A
difference of exactly `Decimal('2e-3021')`, as expected.

-- 
Mark





More information about the Python-list mailing list