Python math is off by .000000000000045

nn pruebauno at latinmail.com
Wed Feb 22 19:29:21 CET 2012


On Feb 22, 1:13 pm, Alec Taylor <alec.tayl... at gmail.com> wrote:
> Simple mathematical problem, + and - only:
>
> >>> 1800.00-1041.00-555.74+530.74-794.95
>
> -60.950000000000045
>
> That's wrong.
>
> Proofhttp://www.wolframalpha.com/input/?i=1800.00-1041.00-555.74%2B530.74-...
> -60.95 aka (-(1219/20))
>
> Is there a reason Python math is only approximated? - Or is this a bug?
>
> Thanks for all info,
>
> Alec Taylor

I get the right answer if I use the right datatype:

>>> import decimal

>>> D=decimal.Decimal

>>> D('1800.00')-D('1041.00')-D('555.74')+D('530.74')-D('794.95')

Decimal('-60.95')




More information about the Python-list mailing list