简体   繁体   中英

Casting big NSInteger to float issue

1) define big NSInteger
2) cast to float
3) log float
4) turns into slightly smaller number every time???

NSInteger val = 485935336;
float val_float = (float)val;
NSLog(@"%f", val_float); //logs 485935328, not 485935336

Any clue why?

Any idea why? Of course. float has about six digits of precision. double has 15 digits precision, which is why you should always use double unless you can give a good reason why not. You will get the same problem, but only when the numbers are a lot bigger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM