开发者

python Decimal precision

开发者 https://www.devze.com 2023-03-15 22:57 出处:网络
For some reason Decimal object looses precision when multiplied. There is no reason to happen so. Please check the testcase and enlighten me.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号