简体   繁体   English

Objective-C如果比较浮动语句不起作用

[英]Objective-C If Statement Comparing Floats not working

This is literally the weirdest thing I have ever encountered. 从字面上看,这是我遇到过的最奇怪的事情。 So, I have a float called ratingDecimal , and I use some conditions to compare it. 因此,我有一个称为ratingDecimal的浮点数,并使用一些条件对其进行比较。 Here is what the conditions look like: 条件如下所示:

    if (ratingDecimal >= 0.0) {

    if (ratingDecimal < 0.3 && ratingDecimal != 0.3) {

        ratingDecimal = 0.0;

        NSLog(@"bigger or equal to 0 and smaller than 0.3");
    }
}

if (ratingDecimal >= 0.3) {

    if (ratingDecimal < 0.8 && ratingDecimal != 0.8) {

        ratingDecimal = 0.5;

        NSLog(@"middle");
    }
}

if (ratingDecimal >= 0.8) {

    NSLog(@"bigger or equal to 0.8");

    ratingDecimal = 1.0;
}

Here is a weird problem. 这是一个奇怪的问题。 I have set the ratingDecimal to 0.8, the console logs ratingDecimal as: 我已将ratingDecimal设置为0.8,控制台将ratingDecimal记录为:

0.8 0.8

but calls: 但致电:

middle 中间

Which should only be called if ratingDecimal is bigger or equal to 0.3 but smaller than 0.8 and DOES NOT equal to 0.8. 仅当ratingDecimal大于或等于0.3但小于0.8 ratingDecimal等于0.8时才应调用ratingDecimal方法。

The statement that should be getting called is: 应该被调用的语句是:

if (ratingDecimal >= 0.8) {

NSLog(@"bigger or equal to 0.8"); 

ratingDecimal = 1.0;

}

But I do not see 但是我看不到

bigger or equal to 0.8 大于或等于0.8

You are probably wondering why my conditions are so tedious and complex, they can be as simple as: 您可能想知道为什么我的条件如此繁琐和复杂,它们可能如此简单:

if (ratingDecimal >= 0.3  < 0.8)

They used to be like that but since this has never worked I kept on breaking the statement down and it is STILL acting weird. 他们曾经是那样,但是由于这从未奏效,所以我一直坚持不懈地声明,这仍然很奇怪。

Why is this happening!? 为什么会这样呢?

Here is an image example: 这是一个图像示例:

问题

Your ratingDecimal value is probably something like 0.7999999999999. 您的ratingDecimal值可能类似于0.7999999999999。 When printed to the console, something is probably rounding it to 0.8, but the comparison still sees it as less than 0.8. 当打印到控制台上时,可能会将其四舍五入为0.8,但比较后仍将其视为小于0.8。

This is due to the binary nature of floating point numbers. 这是由于浮点数的二进制性质。 See What Every Computer Scientist Should Know About Floating-Point Arithmetic for extensive information on this. 有关此方面的广泛信息,请参阅每位计算机科学家应了解的有关浮点运算的知识。

@Greg gave a good answer. @Greg给出了一个很好的答案。 I'd like to point out that you can write that code in a much clearer and simpler fashion: 我想指出的是,您可以以更清晰,更简单的方式编写该代码:

if (ratingDecimal >= 0.0) {
    if (ratingDecimal < 0.3) {
        ratingDecimal = 0.0;

        NSLog(@"bigger or equal to 0 and smaller than 0.3");
    } else if (ratingDecimal < 0.8) {
        ratingDecimal = 0.5;

        NSLog(@"middle");
    } else {
        ratingDecimal = 1.0;

        NSLog(@"bigger or equal to 0.8");
    }
} else {
    // negative
}

As an FYI - a line such as: 仅供参考-像这样的行:

if (ratingDecimal < 0.3 && ratingDecimal != 0.3) {

doesn't make such sense. 没道理。 If a number is less than 0.3 then it must be unequal to 0.3 so the second check doesn't do anything useful. 如果数字小于0.3,则它必须不等于0.3,因此第二次检查没有任何用处。

您可以根据需要使用诸如roundf()之类的函数。

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

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