NSNumber floatValue not equal to NSNumber value

Posted by Mirkules on Stack Overflow See other posts from Stack Overflow or by Mirkules
Published on 2010-05-12T05:19:01Z Indexed on 2010/05/12 5:24 UTC
Read the original article Hit count: 379

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about nsnumber

Related posts about cgfloat