代码之家  ›  专栏  ›  技术社区  ›  Gurwinder Singh

双类中NaN、正无穷大和其他一些常量的值是什么?

  •  1
  • Gurwinder Singh  · 技术社区  · 6 年前

    设定值是什么 NaN , POSITIVE_INFINITY 以及其他一些常量 Double 上课?从源代码中,我看到它们是自己设置的,但这是如何工作的呢?

    public final class Double extends Number implements Comparable<Double> {
        public static final double POSITIVE_INFINITY = POSITIVE_INFINITY;
        public static final double NEGATIVE_INFINITY = NEGATIVE_INFINITY;
        public static final double NaN = NaN;
        public static final double MAX_VALUE = MAX_VALUE;
        public static final double MIN_VALUE = MIN_VALUE;
    
        ...
    }
    

    谢谢。

    1 回复  |  直到 6 年前
        1
  •  4
  •   Andy Turner    6 年前

    至少在 OpenJDK 8 , OpenJDK 9 OpenJDK 10 ,它们在源代码中:

    public static final double POSITIVE_INFINITY = 1.0 / 0.0;
    public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
    public static final double NaN = 0.0d / 0.0;  // (*)
    public static final double MAX_VALUE = 0x1.fffffffffffffP+1023;
    public static final double MIN_VALUE = 0x0.0000000000001P-1022;
    

    (*) In case you're wondering about the " d "...