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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:16:11  来源:igfitidea点击:

What's the difference between float and double?

iosobjective-c

提问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

floatis 32-bit while doubleis 64-bit. A float has fewer significant digits than double.

float是 32 位,而double64 位。浮点数的有效数字少于双精度数。

A floatvalue 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。