如果是>一种是真的吗? [英] When is a>a true?

查看:245
本文介绍了如果是>一种是真的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

右键,我想我真的是生活在梦中。我有下面这段code的,我编译和AIX机器上运行:

  AIX 3 5
PowerPC_POWER5处理器类型
IBM XL C / C ++的AIX,V10.1
版本:10.01.0000.0003
#包括LT&;&stdio.h中GT;
#包括LT&;&math.h中GT;#定义RADIAN(X)((X)* ACOS(0.0)/ 90.0)双nearest_distance(双半径,双lon1,双LAT1,双lon2,双LAT2){
    双rlat1 = RADIAN(LAT1);
    双rlat2 = RADIAN(LAT2);
    双rlon1 = lon1;
    双rlon2 = lon2;
    双A = 0,B = 0,C = 0;    A = SIN(rlat1)*罪(rlat2)+ COS(rlat1)* COS(rlat2)* COS(rlon2-rlon1);
    的printf(%LF \\ n,一);
    如果(一个大于1){
      的printf(aaaaaaaaaaaaaaaa \\ n);
    }
    B = ACOS(一);
    C =半径* B;    返回半径*(ACOS(SIN(rlat1)*罪(rlat2)+
        COS(rlat1)* COS(rlat2)* COS(rlon2-rlon1)));}INT主(INT ARGC,字符** argv的){
  nearest_distance(6367.47,10,64,10,64);
  返回0;
}

现在,值'一'的计算被报告为'1'后。而且,这AIX机器上,它看起来像1> 1作为我的如果输入的是真实的!而我的是什么,我认为是ACOS'1'返回NanQ因为1比1更大请问如何,甚至是可能的吗?我不知道该怎么想了!

在code适用于其他架构就好其中'一'真的需要什么,我认为是1,ACOS(a)为0的值。


解决方案

  

如果你做一个比较,其中的结果,expctedResult是浮动类型:


 如果(结果== expectedResult)


  

然后,它是不太可能的比较将是正确的。如果比较结果为真,那么它可能是不稳定的。 - 输入值,编译器,或CPU中的微小变化可能改变的结果,使该比较为假


有小量的比较 - 绝对误差

 如果(晶圆厂(结果 -  expectedResult)LT; 0.00001)

比较浮点数


什么每个计算机科学家应该知道关于浮点运算

Right, I think I really am living a dream. I have the following piece of code which I compile and run on an AIX machine:

AIX 3 5
PowerPC_POWER5 processor type
IBM XL C/C++ for AIX, V10.1
Version: 10.01.0000.0003


#include <stdio.h>
#include <math.h>

#define RADIAN(x) ((x) * acos(0.0) / 90.0)

double nearest_distance(double radius,double lon1, double lat1, double lon2, double lat2){
    double rlat1=RADIAN(lat1);
    double rlat2=RADIAN(lat2);
    double rlon1=lon1;
    double rlon2=lon2;
    double a=0,b=0,c=0;

    a = sin(rlat1)*sin(rlat2)+ cos(rlat1)*cos(rlat2)*cos(rlon2-rlon1);
    printf("%lf\n",a);
    if (a > 1) {
      printf("aaaaaaaaaaaaaaaa\n");
    }
    b = acos(a);
    c = radius * b;

    return radius*(acos(sin(rlat1)*sin(rlat2)+
        cos(rlat1)*cos(rlat2)*cos(rlon2-rlon1)));

}

int main(int argc, char** argv) {
  nearest_distance(6367.47,10,64,10,64);
  return 0;
}

Now, the value of 'a' after the calculation is reported as being '1'. And, on this AIX machine, it looks like 1 > 1 is true as my 'if' is entered !!! And my acos of what I think is '1' returns NanQ since 1 is bigger than 1. May I ask how that is even possible ? I do not know what to think anymore !

The code works just fine on other architectures where 'a' really takes the value of what I think is 1 and acos(a) is 0.

解决方案

If you do a comparison where result and expctedResult are float types:

if (result == expectedResult)

Then it is unlikely that the comparison will be true. If the comparison is true then it is probably unstable – tiny changes in the input values, compiler, or CPU may change the result and make the comparison be false.

Comparing with epsilon – absolute error

if (fabs(result - expectedResult) < 0.00001)

From Comparing floating point numbers


What Every Computer Scientist Should Know About Floating-Point Arithmetic

这篇关于如果是&gt;一种是真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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