when using the decimal module in python, it seems that division still gives inaccurate results. Here is the code I used to check this, am I doing something wrong or is it just impossible to do division accurately with the decimal module? In that case, is there something else I could use to do this kind of calculations with accuracy?
from decimal import Decimal as D
a = D('39.37007874')
b = D('0.0254')
c = D('1')
print(c / a) #0.02540000000010160000000040640 unexpected
print(c / b) #39.37007874015748031496062992 unexpected
print(c * b) #0.0254 expected
print(c * a) #39.37007874 expected
CodePudding user response:
You could simply use the round function and get any number of decimals you need.
from decimal import Decimal as D
a = D('39.37007874')
b = D('0.0254')
c = D('1')
print(round(c / a, 8)) #0.02540000
print(round(c / b, 8)) #39.37007874
print(round(c * b, 8)) #0.02540000
print(round(c * a, 8)) #39.37007874
CodePudding user response:
You may want to use fractions
.
from fractions import Fraction
a = Fraction('39.37007874')
b = Fraction('0.0254')
c = Fraction(1)
print(c / a) # 50000000/1968503937
print(c / b) # 5000/127
print(c * b) # 127/5000
print(c * a) # 1968503937/50000000
This will make division as accurate as possible, although other operations like sqrt()
or sin()
will still be as accurate as float
.