What are the other NaN values?

后端 未结 3 1356
悲&欢浪女
悲&欢浪女 2020-12-11 03:38

The documentation for java.lang.Double.NaN says that it is

A constant holding a Not-a-Number (NaN) value of type double. It is equivalent

相关标签:
3条回答
  • 2020-12-11 04:10

    You need doubleToRawLongBits rather than doubleToLongBits.

    doubleToRawLongBits extracts the actual binary representation. doubleToLongBits doesn't, it converts all NaNs to the default NaN first.

    double n = Double.longBitsToDouble(0x7ff8000000000000L); // default NaN
    double n2 = Double.longBitsToDouble(0x7ff8000000000100L); // also a NaN, but M != 0
    
    System.out.printf("%X\n", Double.doubleToLongBits(n));
    System.out.printf("%X\n", Double.doubleToRawLongBits(n));
    System.out.printf("%X\n", Double.doubleToLongBits(n2));
    System.out.printf("%X\n", Double.doubleToRawLongBits(n2));
    

    output:

    7FF8000000000000
    7FF8000000000000
    7FF8000000000000
    7FF8000000000100
    
    0 讨论(0)
  • 2020-12-11 04:24

    Java uses IEEE 754 for its floating point numbers and therefore follows their rules.

    According to the Wikipedia page on NaN it is defined like this:

    A bit-wise example of a IEEE floating-point standard single precision NaN: x111 1111 1axx xxxx xxxx xxxx xxxx xxxx where x means don't care.

    So there are quite a few bit-patterns all of which are NaN values.

    0 讨论(0)
  • 2020-12-11 04:34

    IEEE 754 defines a NaN as a number with all exponent bits which are 1 and a non zero number in the mantissa.

    So for a single-precision number you are looking for:

    S     E            M
    x  11111111   xxxxxx....xxx (with M != 0)
    

    Java handles this like so:

    Double n = Double.longBitsToDouble(0x7ff8000000000000L); // default NaN
    Double n2 = Double.longBitsToDouble(0x7ff8000000000100L); // also a NaN, but M != 0
    
    System.out.println(n.isNaN()); // true
    System.out.println(n2.isNaN()); // true
    System.out.println(n2 != Double.doubleToLongBits(Double.NaN)); // true
    

    To sum, you can use any NaN you want which conforms to the rules aforementioned (all bits 1 in exponent and mantissa != 0).

    0 讨论(0)
提交回复
热议问题