Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSNumber floatValue not equal to NSNumber value

First post here. Having a problem with NSNumber's floatValue method -- somehow, it returns an imprecise number. Here's the problem: I store a bunch of NSNumbers in an array, like this:

NSArray *a = [NSArray arrayWithObjects:
    [NSNumber numberWithFloat:0.04f],
    [NSNumber numberWithFloat:0.028f], 
    [NSNumber numberWithFloat:0.016f],
    [NSNumber numberWithFloat:0.004f],
   nil];

Then I try to retrieve the first value (for example): NSNumber n = (NSNumber) [a objectAtIndex:0]; CGFloat f = [n floatValue];

In the debugger, n shows a value of 0.04 (in the summary column), but f shows a value of 0.0399999991. What am I doing wrong here?

Thanks everyone.

like image 318
Mirkules Avatar asked Nov 23 '25 00:11

Mirkules


1 Answers

You're not doing anything wrong. That's not a problem specifically of NSNumber or CGFloat, either. It's something very universal about the floating point numbers, which is based on powers of 2, not powers of ten. You should read this definitive article or Wikipedia article.

The point is, the standard floating point numbers can only accurately represent a number which is a sum of 1/2, 1/4, 1/8, ... . 1/10 is not. So you can't completely accurately represent 0.1 using standard floating point numbers.

If you really do need to deal with it, there are routines which deal with rational fractions or decimal floating point numbers.

like image 178
Yuji Avatar answered Nov 25 '25 17:11

Yuji