It is famous that for float numbers:

.1 + .2 != .3



It seems that multiplying floats by 10 allows you to preserve more precision. To further illustrate the case, we can do this in python:


sum([3000000000.001 * 1000]*300) / 1000

By multiplying each element in the list by 1000 and divide the sum of the list by 1000, I can get the "correct" answer. I am wondering: 1) why it's the case. 2) Will this always work, and 3) At what magnitude, will this method backfire, if it will.


The only numbers that can be stored exactly are rational numbers whose denominator is a power of 2 (read any source on floating point numbers to understand why). For example, floating point numbers do satisfy $$ 1/4 + 2/4 = 3/4 $$ but do not satisfy (or rather, don't necessarily satisfy) $$ 1/3 + 2/3 = 3/3. $$ There's nothing magical about the number 10. It's just an artifact of the common decimal representation. So in general it won't help to multiply by a power of 10.


To understand that, you need to understand how floating point numbers are stored n memory.

Basically, it’s not possible to store the decimal part exactly (precisely). So, they are approximated to the nearest value which could be stored. Hence, they loose some precision while storing.

And hence, the above mentioned behaviour.

If you want more insight on what exactly is happening in the memory, try understanding how exactly the above mentioned numbers are getting stored, by referring to Floating-point arithmetic


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.