views:

299

answers:

1

Hi Everyone,

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.

+1  A: 

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.

Yuji
That clears it up, thanks. In other words, never do == comparisons with floats unless you round them first. Thanks.
Mirkules