Search code examples
floating-pointmultiplication

.6 x 100 giving result as 60.00004 instead of 60


Possible Duplicate:
Mysterious calculation error when multiply by 100

Here is a strange problem that I am facing. I store a value .6 to a float variable. When I multiply it with 100 I am getting wrong answer as 60.000004.Where does this .000004 come from? Here is my code

NSlog(@"%f",self.dataHandler.correctPercentage * 100);
if (self.obj.percentage >= (self.dataHandler.correctPercentage * 100) )
{
    //Do something
}

My value stored in self.dataHandler.correctPercentage is .6. But when I NSlog it, I am getting 60.000004. My boundary conditions are going wrong due to this.

Why is this happening?


Solution

  • The computer cannot save most float numbers without rounding error. this has several implications especially for equality checking — as floats tend to be not equal, even if the pure math would make you think that.

    In math, numbers have infinitive space to be stored, an infinitive numbers of digits can be used. But the computer cannot handle infinity, as it is limited in resources.

    You should read What Every Computer Scientist Should Know About Floating-Point Arithmetic (at least the first few paragraphs)

    I also reviewed a code that runs into similar problems yesterday: https://codereview.stackexchange.com/questions/14911/is-there-a-better-way-of-showing-an-image-based-on-the-battery-level/14961#14961