Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python decimal.Decimal precision doesn't match wolfram alpha's

I have the following python code:

In [1]: import decimal

In [2]: decimal.getcontext().prec = 80

In [3]: (1-decimal.Decimal('0.002'))**5
Out[3]: Decimal('0.990039920079968')

Shouldn't it match 0.99003992007996799440405766290496103465557098388671875 according to this http://www.wolframalpha.com/input/?i=SetPrecision%5B%281+-+0.002%29%5E5%2C+80%5D ?

like image 814
prgDevelop Avatar asked Feb 27 '26 09:02

prgDevelop


1 Answers

Here's what's happening here: Because it looks like syntax from the Mathematica programming language, WolframAlpha is interpreting the input SetPrecision[(1 - 0.002)^5, 80] as Mathematica source code, which it proceeds to evaluate. In Mathematica, as others have surmised in other answers, 0.002 is a machine precision floating point literal value. Roundoff error ensues. Finally, the resulting machine precision value is cast by SetPrecision to the nearest 80-precision value.

To get around this, you have a couple of options.

  1. You could try to make WolframAlpha not think you are entering code from the Mathematica programming language, so that it will do its own magic. As njzk2 mentioned, entering (1 - 0.002)^5 will do this.
  2. In Mathematica code that you ask WolframAlpha to evaluate, you could enter an infinite-precision literal instead of the machine precision literal 0.002. There are several ways, but here is one: SetPrecision[(1 - 2*^-3)^5, 80].

Finally, I want to point out that in Mathematica, and by extension in a WolframAlpha query consisting of Mathematica code, you usually want N (documentation) rather than SetPrecision. They are often similar (identical in this case), but there is a subtle difference:

  • SetPrecision[..., n] first sets all enclosed numbers to precision n, then evaluates everything (roundoff error will ensue)
  • N[..., n] essentially repeatedly tries SetPrecision at higher and higher precision until the final roundoff error is almost certainly less than n.

N works slightly harder but gets you the right number of correct digits (assuming the input is sufficiently precise).

So my final suggestion for using WolframAlpha to do this calculation via Mathematica Code is N[(1 - 2*^-3)^5, 80].

like image 79
Andrew Moylan Avatar answered Mar 01 '26 22:03

Andrew Moylan