Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

249
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda