简体   繁体   中英

python Decimal precision

For some reason Decimal object looses precision when multiplied. There is no reason to happen so. Please check the testcase and enlighten me.

from decimal import *
getcontext().prec = 11

a = Decimal('5085.28725881485')
b = 1

print getcontext()
print 'a     = '+str(a)
print 'b     = '+str(b)
print 'a * b = '+str(a * b)

And the output:

Context(prec=11, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, capitals=1, flags=[], traps=[DivisionByZero, InvalidOperation, Overflow])
a     = 5085.28725881485
b     = 1
a * b = 5085.2872588

Not sure if this is relevant, but python2.6 used.

The precision you specify in the context (11 places) is only applied when performing calculations , not when creating decimal.Decimal objects -- and the result, 5085.2872588 does indeed obey that limit. Using 1 as the multiplier does not change the rules with regards to precision; the result of arithmetic operations always considers the precision.

If you were instead looking for decimal.Decimal to return a number rounded to a particular number of places, the create_decimal method of the context object will do that for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM