[Tutor] decimal module and precision

Richard D. Moores rdmoores at gmail.com
Wed Feb 2 05:33:38 CET 2011


On Tue, Feb 1, 2011 at 04:29, col speed <ajarncolin at gmail.com> wrote:
>
> You can always change the precision in decimal. Just an idea....

Not exactly sure what you mean. But I just tried using decimal to get
123.2345274523452345235432452345 ** 2.3 to 300 digits:

>>> from decimal import Decimal as D
>>> import decimal
>>> decimal.getcontext().prec =300
>>> D('123.2345274523452345235432452345')**D('2.3')
Decimal('64370.1512280246915272663511041234541758816386398199132394466583175597615075198590980955633694480202503045760664137267271735342845242951082979103782026356856312125096217781701992298765824436994198599115081342290327111836807693742546891271393004992808057677786573779518236419674381269758803681315430784')
>>> len('64370.1512280246915272663511041234541758816386398199132394466583175597615075198590980955633694480202503045760664137267271735342845242951082979103782026356856312125096217781701992298765824436994198599115081342290327111836807693742546891271393004992808057677786573779518236419674381269758803681315430784')
301
>>>

I also did this on WolframAlpha.com (except the len) and found that
decimal and WA agree exactly! (I had come to believe that setting
decimal.getcontext().prec to n meant that the result would be in n
digits, but that the accuracy would be much less, for large n.)

Here's a screen shot of the WA result:

<http://www.rcblue.com/Misc/WolframAlphaCalculationResult.gif>.

The decimal module output ends with '15430784'; The 6th line of the WA
output begins with "154307841'

I also tried the same calculation with decimal.getcontext().prec
=1840. WA's and decimal's results agreed exactly.

Dick


More information about the Tutor mailing list