简体   繁体   English

投放大型NSInteger以浮动发行

[英]Casting big NSInteger to float issue

1) define big NSInteger 1)定义大NSInteger
2) cast to float 2)浮起来
3) log float 3)日志浮动
4) turns into slightly smaller number every time??? 4)每次变成稍微小的数字???

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. float的精度约为六位数。 double has 15 digits precision, which is why you should always use double unless you can give a good reason why not. double的精度为15位,这就是为什么除非您有充分的理由不使用double的原因,否则应该始终使用double。 You will get the same problem, but only when the numbers are a lot bigger. 您将遇到同样的问题,但是只有当数字大得多时才如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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