124*_*123 2 c floating-point ieee-754
在C中,在使用IEEE-754浮点数的实现中,当我比较两个NaN的浮点数时,它返回0或"false".但是为什么两个浮点数都被认为是相等的呢?
这个程序打印"相等:......"(至少在Linux AMD64下使用gcc),在我看来它应该打印"不同:......".
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
volatile double a = 1e200; //use volatile to suppress compiler warnings
volatile double b = 3e200;
volatile double c = 1e200;
double resA = a * c; //resA and resB should by inf
double resB = b * c;
if (resA == resB)
{
printf("equal: %e * %e = %e = %e = %e * %e\n",a,c,resA,resB,b,c);
}
else
{
printf("different: %e * %e = %e != %e = %e * %e\n", a, c, resA, resB, b, c);
}
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
另一个例子,为什么我认为inf与inf不同,是:自然数和有理数的数量,两者都是无限的但不相同.
那么为什么inf == inf?