Floating point 将原始二进制文件转换为小值的浮点打印0

Floating point 将原始二进制文件转换为小值的浮点打印0,floating-point,intel,floating-point-precision,floating-point-conversion,Floating Point,Intel,Floating Point Precision,Floating Point Conversion,我试图读取2个32位值(假设它们是ieee 745单精度浮点值),并使用以下代码段比较它们以检查它们是否相等 #include <stdio.h> #include <stdint.h> void main(){ int a; int b; //a = 0x3f99999a ; //b = 0x3fa66666 ; a = 0xfa98 ; b = 0x65cc ; printf("Comparison is %d

我试图读取2个32位值(假设它们是ieee 745单精度浮点值),并使用以下代码段比较它们以检查它们是否相等

#include <stdio.h>
#include <stdint.h>

void main(){
    int a;
    int b;
    //a = 0x3f99999a ;
    //b = 0x3fa66666 ;
    a = 0xfa98 ;
    b = 0x65cc ;
    printf("Comparison is %d\n",((*(float*)&(a))==(*(float*)&(b))));
    printf("Numbers are a= %f and b = %f \n",(*(float*)&(a)),(*(float*)&(b)));
}
a=0x3F999A和b=0x3fa66666的输出为:

Comparison is 0 // This is correct
Numbers are a= 1.2 and b = 1.3 // True decimal values for given 745 representation
Comparison is 0 // This is correct
Numbers are a= 1.2 and b = 1.3 // True decimal values for given 745 representation
平台B-英特尔i7(英特尔(R)i7-3770),具有ubuntu 14.04和gcc编译器版本(4.8.2(ubuntu 4.8.2-19 ubuntu1))

a=0xfa98且b=0x65cc的输出为:

Comparison is 1       // This is clearly wrong as both numbers are unequal
Numbers are a= 0 and b = 0 // Casting it to float is causing them to 0 ??
Comparison is 0       // This is correct !!
Numbers are a= 0 and b = 0 // Still  0 because numbers are subnormal
a=0x3F999A和b=0x3fa66666的输出为:

Comparison is 0 // This is correct
Numbers are a= 1.2 and b = 1.3 // True decimal values for given 745 representation
Comparison is 0 // This is correct
Numbers are a= 1.2 and b = 1.3 // True decimal values for given 745 representation

所以我的问题是为什么平台A产生了错误的浮动比较结果。第一组值低于正常值,在十进制表示中非常小,但绝对不相等。其中,as代码适用于第二组值(大值)。谁能给我解释一下吗。为什么平台A不显示值与平台B不相同

问题是mxcsr寄存器的DAZ位。一台机器A设置了它,所以它将非规范化处理为0。其中,在机器2上,DAZ被重置,因此它正在对其进行规范化,并将非规范化视为不同的值,这导致比较结果不相等。但我真的很感激所有帮助我的人:)!没有你的帮助是不可能调试的

请注意,
0xfa98
0x65cc
表示非规范数字,但符合IEEE-754的FPU仍应将其视为不同的数字。在标准x86Linux系统上,您的第一个示例为我打印了“比较为0”。那么,你在使用什么平台呢?谢谢你的回复。我使用的是x86机器,其上运行的是Fedora release 20。好的,那么如果您将
%f
替换为
%e
,输出是什么?我得到的
数字是a=8.989610e-41和b=3.651784e-41
。另外,您是否使用任何不寻常的编译器选项?我的机器将a和b都读取为0.000000e+00。根据您的建议,我在不同的机器上尝试了这个代码片段,即使用linux 14.04的intel xeon服务器,它产生了正确的输出(1)!这是否意味着我的初始机器没有与ieee 745兼容的FPU?再次感谢您的帮助!:)机器A(带软呢帽的x-86)-icc和gcc的输出均为1。机器B(使用Ubuntu14.04的xeon)-icc和gcc都给出了0的输出。没有编译器选项。这肯定会导致您看到的问题!你能锁定机器A上设置MXCSR中DAZ位的流氓特工吗?无论是谁,都需要一份bug报告;摆弄别人的控制旗而不把它们放回去是非常粗鲁的!