Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python Decimal precision

Tags:

python

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.

like image 365
marrat Avatar asked Jun 26 '11 10:06

marrat


People also ask

How do you round to 2 decimal places in Python?

Use the ceil() function(returns a ceiling value of the number i.e., the smallest integer greater than or equal to the number), to round the number upto the 2 decimal places and print the resultant number.

Does 0.5 round up or down Python?

For <0.5, it rounds down, and for >0.5, it rounds up. For =0.5, the round() function rounds the number off to the nearest even number. So, 0.5 is rounded to zero, and so is -0.5; 33.5 and 34.5 are both rounded off to 34; -33.5 -34.5 are both rounded off to -34, and so on.


1 Answers

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.

like image 136
Thomas Wouters Avatar answered Oct 10 '22 10:10

Thomas Wouters