Python math is off by .000000000000045
benjamin.kaplan at case.edu
Wed Feb 22 19:32:06 CET 2012
On Feb 22, 2012 1:16 PM, "Alec Taylor" <alec.taylor6 at gmail.com> wrote:
> Simple mathematical problem, + and - only:
> >>> 1800.00-1041.00-555.74+530.74-794.95
> That's wrong.
> -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
You aren't doing math with decimal numbers. you're using IEEE
754-compliant double precision floating point numbers. this isn't just a
python thing. You'd get the same results in C, Java, VB, and pretty much
every other general purpose language written in the last 40 years.
Floats are represented in a form similar to scientific notation (a * 2^b),
so just like scientific notation, there's a finite number of significant
figures. And just like there are rational numbers that can't be represented
in decimal, like 1/3, there are numbers that can't be represented in
binary, like 1/10.
Double-precision numbers are accurate to about 15 decimal digits. if you
need more precision, there is the decimal module but it's way slower
because your processor doesn't natively support it.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list