This math scares me
Costas Menico
costas at meezon.com
Tue Mar 13 02:43:22 CET 2001
I appreciate the lessons in floating point math. Unfortunately I had
to deal with this stuff coding in assembler.
However I am new to Python and didnt realize that it displays numbers
like this unless you format them yourself. Some other languages I use
just do the rounding for you.
Fortunately I discovered I can use "print 5.01 + 5.54" and it does the
job. It has the extra intelligence built into it to autoformat
Thanks for the input.
Costas
>costas at springmail.com <costas at springmail.com> wrote in comp.lang.python:
>> Ok, I can see maybe division having problems. But why does addition of
>> the two numbers below:
>>
>> 5.01+5.54
>>
>> give me this?
>>
>> 10.550000000000001
>>
>> I hope its some setting that can take care of this. I am using Python
>> under Windows 98.
>
>It's worse than that. Try:
>
>>>> 5.01
>5.0099999999999998
>
>The number is stored in binary. It's not possible to represent everything in
>a finite number of binary digits (just like you can't represent 1/3 in a
>finite number of decimal digits - you don't protest either when you see
>0.3333333333333333, do you? It's just as wrong).
>
>This is not a Python problem, this is due to the way computers store
>floating point numbers.
>
>If you can avoid them, don't use floats. If you're working with money, count
>cents instead (and use longs if you have numbers over a few hundred million
>cents).
>
>--
>Remco Gerlich
More information about the Python-list
mailing list