ios float 和 double 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16108431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between float and double?
提问by saturngod
When I run the following code ,
当我运行以下代码时,
NSString* s= @"10000000.01";
float f = [s floatValue];
double d = [s doubleValue];
if(f > 10000000)
{
NSLog(@"Over Value");
}
else {
NSLog(@"OK Float");
}
if(d > 10000000)
{
NSLog(@"Over value");
}
else {
NSLog(@"OK Double");
}
The response is like following.
响应如下。
2013-04-19 17:07:29.284 float[2991:907] OK Float
2013-04-19 17:07:29.287 float[2991:907] Over value
Why float value changed to 10000000.00 instead of 10000000.01 ?
为什么浮点值改为 10000000.00 而不是 10000000.01 ?
回答by rmaddy
float
is 32-bit while double
is 64-bit. A float has fewer significant digits than double.
float
是 32 位,而double
64 位。浮点数的有效数字少于双精度数。
A float
value doesn't store enough to hold the 10 digits of your 10000000.01
.
一个float
值不能存储足够的 10 位数字10000000.01
。
Also see Difference between float and doublefor more details. That is about C/C++ but it applies to Objective-C as well.
另请参阅float 和 double 之间的差异以获取更多详细信息。那是关于 C/C++ 的,但它也适用于 Objective-C。