如何在xCode中比较一系列浮点数?

时间:2012-10-18 17:09:12

标签: iphone ios xcode

此代码不起作用。有什么建议吗?

float tempNum = 0.590000;
int temporary;
if (tempNum >= 0.590000 && tempNum <= 0.610000){
    temporary = 15;
}
// temporary yields zero

1 个答案:

答案 0 :(得分:1)

所以:

默认情况下,0.590000是 DOUBLE

所以float tempNum = 0.59000导致演员。

要证明这一点:

float tempNum = 0.590000f;
int temporary;
if (tempNum >= 0.590000f && tempNum <= 0.610000f){
    temporary = 15;
}

所以,请记住:

aaa.aaa DOUBLE

aaa.aaaf FLOAT