I’m investigating the accuracy of solutions to an equation.
I’m using this line in the hope that the calculation using x,y,n (all Integers) will be accurate to the precision set by the decimal.context.prec I choose.
z = Decimal(((xy10**n)-(xy))/((10x)-y))
Am I correct that the calculation will be performed to the accuracy of the decimal.context.prec or will floats be used inside the brackets ?
The calculations are performed before calling Decimal, and thus use floats. You should convert your variables to Decimal before performing the calculation.
Will this work ? (I’m still a bit newbie on Python)
xD = Decimal(x)
yD = Decimal(y)
nD = Decimal(n)
z = ((xD yD 10**nD)-(xD yD))/((10 xD)-yD)
I think it should, as long as x, y and n are integers. If they are floats, it is usually better to use strings. Compare:
>>> from decimal import Decimal