Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
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.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!