1 year ago
#384587
Dan
NaN conversion from float to double changes its underlying data
int main(){
unsigned int a = 2139156962;
float b = *(float*)&a; // nan(0xf1e2)
double c = (double)b; // nan canonicalization?
float d = (float)c; // nan(0x40f1e2), d != b
unsigned int e = *(int*)&d; // e != a
return 0;
}
NaN values can be represented in many different ways. As the example above shows, converting a NaN value of nan(0xf1e2)
to a double type is not preserving the input bit patterns, and casting it back to a float doesn't return the same value as the original input.
From this link I can see on x64, CVTSS2SD
seems to canonicalize Qnan inputs?
the sign bit is preserved, the 8-bit exponent FFH is replaced by the 11-bit exponent 7FFH, and the 24-bit significand is extended to a 53-bit significand by appending 29 bits equal to 0.
So regardless of what the Qnan bit pattern of our input is, the output will use 0x7FF
and won't preserve all the original bits? Is this some sort of NaN canonicalization?
If so then this answer may not be fully accurate?
floats can be promoted to double and the value is unchanged.
Our output is still a NaN but the underlying data is now changed and c != b
.
c
casting
floating-point
nan
0 Answers
Your Answer