具有以下并集:
union {double first_d; uint64 first;};
union {double second_d; uint64 second;};
...
first_d = <a double value>
second_d = <a double value>
进行以下比较的输出:
if(first_d > second_d)
// CASE 1 OUTPUT
else if(first_d < second_d)
// CASE 2 OUTPUT
else
// CASE 3 OUTPUT
以下总是一样?
if(first> second)
// CASE 1' OUTPUT
else if(first < second)
// CASE 2' OUTPUT
else
// CASE 3' OUTPUT
不。下面是一个使用 NaNs
的反例:
int main()
{
union {double first_d; uint64 first;};
union {double second_d; uint64 second;};
first = 0x7ff8000000000001;
second = 0x7ff8000000000002;
if(first_d > second_d)
printf("greatern");
else if(first_d < second_d)
printf("lessn");
else
printf("othern");
if(first > second)
printf("greatern");
else if(first < second)
printf("lessn");
else
printf("othern");
return 0;
}
输出:
other
less
我还要提到,通过联合进行类型双关并不是 100% 符合标准。所以你也可以说这是未定义的行为。