<p><br>
On Feb 22, 2012 1:16 PM, "Alec Taylor" <<a href="mailto:alec.taylor6@gmail.com">alec.taylor6@gmail.com</a>> wrote:<br>
><br>
> Simple mathematical problem, + and - only:<br>
><br>
> >>> 1800.00-1041.00-555.74+530.74-794.95<br>
> -60.950000000000045<br>
><br>
> That's wrong.<br>
><br>
> Proof<br>
> <a href="http://www.wolframalpha.com/input/?i=1800.00-1041.00-555.74%2B530.74-794.95">http://www.wolframalpha.com/input/?i=1800.00-1041.00-555.74%2B530.74-794.95</a><br>
> -60.95 aka (-(1219/20))<br>
><br>
> Is there a reason Python math is only approximated? - Or is this a bug?<br>
><br>
> Thanks for all info,<br>
><br>
> Alec Taylor<br>
> --</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<p>> <a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br>
</p>