Search code examples
pythonpython-2.7floating-pointfloating-accuracy

Decimal loses accuracy when raised to a power of 1


I came across some accuracy issues in my program, using Decimal.

A simple repro:

from decimal import Decimal

print Decimal(1910944005427272400562113336049664)
print Decimal(1910944005427272400562113336049664)**1
print int(Decimal(1910944005427272400562113336049664)**1)

Gives:

1910944005427272400562113336049664
1.910944005427272400562113336E+33
1910944005427272400562113336000000

As you can see, the original value has become slightly smaller (minus 49664 to be exact).

Now, my actual code does a lot more than just raising a number to the power of 1, so I end up with degraded accuracy.

Is there any "better Decimal" out there that I can make use of?

My input and output are between 0 (inclusive) and 2^256 (exclusive).


Solution

  • As described in the documentation, "the decimal module has a user alterable precision (defaulting to 28 places)". You can set the precision to a higher value to get accurate results:

    >>> decimal.getcontext().prec = 100
    >>> print(int(Decimal(1910944005427272400562113336049664)**Decimal(1)))
    1910944005427272400562113336049664