开发者

Cocoa Touch. Why does NSNumber return a difference value for intValue and floatValue?

开发者 https://www.devze.com 2022-12-23 00:40 出处:网络
Ok, this makes absolutely no sense: I creat an NSNumber: NSNumber *n = [NSNumber numberWithInt:37669178];

Ok, this makes absolutely no sense:

I creat an NSNumber:

NSNumber *n = [NSNumber numberWithInt:37669178];  
开发者_StackOverflow社区

I then echo the value as an integer and a float:

int i = [n intValue];  
float f = [n floatValue];

Here are their values:

int: 37669178  
float: 37669176.000000

Huh!?!?!

Could someone please explain to me why this is happening and how to get around it. This surely cannot be a precision issue. 37,669,178 is well within the precision of a float.

Thanks,

Doug

UPDATE

OK, Now I'm totally confused. Refering to math.h

#define MAXFLOAT    ((float)3.40282346638528860e+38)

Integer value 37669178 is 3.7669178e+7, well within the maximum allowable floating point value. So, [n floatValue] should return 37669178.0 not 37669176.0

What am I missing here?


A float only has 23 bits of precision (not to be confused with range) which is around 7 significant decimal digits. Use double if you need more precision than this.

What Every Programmer Should Know About Floating Point


Related to the subject at hand: NSNumber is not intended to do precision mathematical operations with. It's just a means of wrapping a number in an object. If you require precision, you should employ NSDecimal instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消