Double的MIN_VALUE和Integer的MIN_VALUE

Integer的MIN_VALUE是:-2147483648

MAX_VALUE是:2147483647

Integer的源碼:

/**
     * A constant holding the minimum value an {@code int} can
     * have, -2<sup>31</sup>.
     */
    @Native public static final int   MIN_VALUE = 0x80000000;

    /**
     * A constant holding the maximum value an {@code int} can
     * have, 2<sup>31</sup>-1.
     */
    @Native public static final int   MAX_VALUE = 0x7fffffff;

java中int(Integer)是4字節32位有符號數.MIN_VALUE是int能表示的最小的負數,MAX_VALUE則是int能表示的最大的正數.

但是double(Double)卻不是這樣.Double的源碼:


    /**
     * A constant holding the largest positive finite value of type
     * {@code double},
     * (2-2<sup>-52</sup>)&middot;2<sup>1023</sup>.  It is equal to
     * the hexadecimal floating-point literal
     * {@code 0x1.fffffffffffffP+1023} and also equal to
     * {@code Double.longBitsToDouble(0x7fefffffffffffffL)}.
     */
    public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308

    /**
     * A constant holding the smallest positive normal value of type
     * {@code double}, 2<sup>-1022</sup>.  It is equal to the
     * hexadecimal floating-point literal {@code 0x1.0p-1022} and also
     * equal to {@code Double.longBitsToDouble(0x0010000000000000L)}.
     *
     * @since 1.6
     */
    public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308

    /**
     * A constant holding the smallest positive nonzero value of type
     * {@code double}, 2<sup>-1074</sup>. It is equal to the
     * hexadecimal floating-point literal
     * {@code 0x0.0000000000001P-1022} and also equal to
     * {@code Double.longBitsToDouble(0x1L)}.
     */
    public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324

Double的MIN_VALUE是double能表示的最小的正整數,而不是最小的數(Float的MIN_VALUE也一樣).

如下代碼會輸出什麼呢?

System.out.println(Math.min(Double.MIN_VALUE,0.0));

答案是:0.0

此外,整數取餘是比較常見的,那麼浮點數取餘呢?

如下代碼會輸出什麼呢?

double a = 25.64;
System.out.println(a % 10);

答案是:5.640000000000001

還有一個問題,既然Double.MIN_VALUE是double能表示的最小正數,那麼double能表示的最小的數是什麼呢?

我們先看一下,下面的代碼的結果是什麼?

System.out.println(0.0 - Double.MAX_VALUE);

答案是:-1.7976931348623157E308

這個是不是最小值呢?

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章