Are the bit patterns of NaNs really hardware-dependent?

前端 未结 4 639
臣服心动
臣服心动 2020-12-01 08:52

I was reading about floating-point NaN values in the Java Language Specification (I\'m boring). A 32-bit float has this bit format:

seee eeee em         


        
4条回答
  •  暖寄归人
    2020-12-01 09:37

    The only other NaN value that I could generate with normal arithmetic operations so far is the same but with the sign changed:

    public static void main(String []args) {
        Double tentative1 = 0d/0d;
        Double tentative2 = Math.sqrt(-1d);
        
        System.out.println(tentative1);
        System.out.println(tentative2);
        
        System.out.println(Long.toHexString(Double.doubleToRawLongBits(tentative1)));
        System.out.println(Long.toHexString(Double.doubleToRawLongBits(tentative2)));
        
        System.out.println(tentative1 == tentative2);
        System.out.println(tentative1.equals(tentative2));
    }
    

    Output:

    NaN

    NaN

    7ff8000000000000

    fff8000000000000

    false

    true

提交回复
热议问题