Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

270
Vistas
Why does double.IsNegative(double.NaN) return true?

Why does double.IsNegative(double.NaN) unexpectedly return true whereas double.NaN < 0 returns false as expected?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

The IEEE754 defines floating point numbers. The highest bit is used to define the sign with 0 being positive and 1 being negative.

Through a bit of digging, double.NaN seems to be represented as 0xFFF8000000000000 in binary (and 0.0 / 0.0 in code somehow).

double.IsNegative(double d) just checks the highest bit without any actual math being involved. Therefor, NaN is interpreted as a negative value. Meanwhile, double.NaN if used in a binary comparison will always yield false:

double.NaN < 0.0  //false
double.NaN > 0.0  //false
double.NaN <= 0.0 //false
double.NaN >= 0.0 //false
double.NaN == 0.0 //false
over 4 years ago · Santiago Trujillo Denunciar

0

Well, according to refrence source, double.IsNegative just checks the most significant bit:

    [Pure]
    [System.Security.SecuritySafeCritical]  // auto-generated
    internal unsafe static bool IsNegative(double d) {
        return (*(UInt64*)(&d) & 0x8000000000000000) == 0x8000000000000000;
    }

In case of double.NaN the most signifucant bit is set:

    11111111 11111000 00000000 00000000 00000000 00000000 00000000 00000000   
    ||           ||                                                       |
    |<-   Exp   -><-                     Mantissa                        ->
    Sign

That's why double.IsNegative returns true

When we put < or > FPU commands are used which know all ones exponent is a special kind of floating point value which should be treated in a special way.

The very same picture is with Single.NaN.

Note, that we can construct another strange value, negative zero:

    10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000   
    ||           ||                                                       |
    |<-   Exp   -><-                          Mantissa                   ->
    Sign

Please, look:

  double negativeZero = BitConverter.ToDouble(new byte[] { 
    0, 0, 0, 0, 0, 0, 0, 128
  });

  Console.WriteLine(negativeZero == 0 ? "Zero" : "???");

  Console.WriteLine(double.IsNegative(negativeZero) ? "Negative" : "???");
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda