如果中)了解浮点变量比较( [英] Understanding float variable comparison in if()

查看:98
本文介绍了如果中)了解浮点变量比较(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法找到下面这段code的原因:

 的#include<&stdio.h中GT;
诠释的main()
{
    浮动F = 0.1;
    如果(F = = 0.1)
      的printf(真);
    其他
      的printf(假);
    返回0;
}

输出是假的。

 的#include<&stdio.h中GT;
诠释的main()
{
    浮动F = 0.1;
    如果(F ==(浮点)0.1)
      的printf(真);
    其他
      的printf(假);
    返回0;
}

现在显示正确的输出。请告诉我这背后的原因是什么?

还有什么是这种现象的原因。

 的#include<&stdio.h中GT;
主要()
{
    INT n = 0的,m为0;
        如果(N 0)
            如果(M 0)
                    的printf(真);
        其他
            的printf(假);
}


解决方案

0.1 文字是双击。您宽松precision这里浮动F = 0.1;

您可以说我们比较期间失去precision了,所以为什么不是˚F== 0.1 真的呢?因为浮动扩展到双击,而不是相反。在C较小的类型一直延伸到更大的一个。

简化您的例子中,我们可以说,双(浮点(1.0))!= 1.0

可能的解决方案:


  • 使用双击而不是浮动为F型

  • 使用铸铁作为你的第二例子

  • 使用浮动文字 - 全部替换 0.1 0.1F

更好的解决方案

浮点变量有很多与比较的问题。他们,包括这一个,可以定义自己的比较函数解决:

 布尔fp_equal(双A,双B,双EPS = FLT_EPSILON){
  返回晶圆厂(A - B)<晶圆厂(EPS *一);
}


问题的第二部分:

为什么答案是假的,因为其他部分总是对应于最里面的如果块。所以,你被格式化困惑时,code是等价于:

 的#include<&stdio.h中GT;诠释的main()
{
    INT n = 0的,m为0;
    如果(N 0){
        如果(M 0){
            的printf(真);
        }
        其他{
            的printf(假);
        }
    }
}

Unable to find the reason for the following piece of code:

#include <stdio.h>
int main()
{
    float f = 0.1;
    if (f == 0.1)
      printf("True");
    else
      printf("False");
    return 0;
}

The output is false.

#include <stdio.h>
int main()
{
    float f = 0.1;
    if (f == (float)0.1)
      printf("True");
    else
      printf("False");
    return 0;
}

Now shows the correct output. Whats the reason behind this?

Also what is the reason of this behavior.

#include <stdio.h>
main()
{
    int n = 0, m = 0;
        if (n > 0)
            if (m > 0)
                    printf("True");
        else 
            printf("False");
}

解决方案

0.1 literal is double. You loose precision here float f = 0.1;

You could say we loose precision during comparison again, so why isn't f == 0.1 true anyway? Because float extends to double, not the opposite. In C smaller type always extends to the bigger one.

Simplified your example we can say that double(float(1.0)) != 1.0

Possible solutions:

  • use double instead of float as a type of f.
  • use cast as in your 2nd example
  • use float literals - replace all 0.1 with 0.1f

Better solution

Floating point variables have a lot of problems with comparisons. They, including this one, can be solved by defining your own comparison function:

bool fp_equal(double a, double b, double eps = FLT_EPSILON) {
  return fabs(a - b) < fabs(eps * a);
}


The second part of the question:

Why the answer is false is because else part always corresponds to the innermost if block. So you were confused by formatting, the code is equivalent to:

#include <stdio.h>

int main()
{
    int n = 0, m = 0;
    if (n > 0) {
        if (m > 0) {
            printf("True");
        }
        else {
            printf("False");
        }
    }
}

这篇关于如果中)了解浮点变量比较(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆