浮点比较中的转换操作数不符合预期

时间:2016-08-31 14:35:23

标签: c++ floating-point precision

当我想比较两个浮点值时。我得到了奇怪的结果。

我用x86_64-w64-mingw32-g ++ test.c -mfpmath = 387编译的例子:

#include "math.h"
#include "stdio.h"
int main(int argc, char **args) {

    double t1 = atof("999990") * 1e-9;
    double t2 = atof("999990") * 1e-9;
    printf("1) Compare of two variables \"t1 != t2\": ");
    if (t1 != t2) {
        printf("Not Equal\n");
    }
    else {
        printf("Equal\n");  //This is taken
    }
    printf("2) Compare of Variable with calculated value \"t1 != atof(\"999990\") * 1e-9\": ");
    if (t1 != atof("999990") * 1e-9) {
        printf("Not Equal\n");  //This is taken
    }
    else {
        printf("Equal\n");
    }
    printf("3) Compare of Variable with casted calculated value \"t1 != (double)(atof(\"999990\") * 1e-9)\": ");
    if (t1 != (double)(atof("999990") * 1e-9)) {
        printf("Not Equal\n");  //This is taken??
    }
    else {
        printf("Equal\n");
    }
    return 0;
}

首先比较:好的

第二次比较:OK(假设右项为80Bit精度)

为什么评估第三次比较为不平等? 为什么演员要加倍才能被识别?为什么比较的结果与第一种情况不同?

0 个答案:

没有答案