float和double有什么区别? [英] What's the difference between float and double?
本文介绍了float和double有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我运行以下代码时,
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");
}
响应如下.
2013-04-19 17:07:29.284 float[2991:907] OK Float
2013-04-19 17:07:29.287 float[2991:907] Over value
为什么浮点值从10000000.01改为10000000.00?
Why float value changed to 10000000.00 instead of 10000000.01 ?
推荐答案
float
为32位,而double
为64位.浮点数的有效位数少于双精度数.
float
is 32-bit while double
is 64-bit. A float has fewer significant digits than double.
float
值的存储量不足以容纳10000000.01
的10位数字.
A float
value doesn't store enough to hold the 10 digits of your 10000000.01
.
有关更多详细信息,另请参见 float和double之间的差异.那是关于C/C ++的,但是它也适用于Objective-C.
Also see Difference between float and double for more details. That is about C/C++ but it applies to Objective-C as well.
这篇关于float和double有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文