JDK 源码:Double、Float

以下是 JDK 8 中 DoubleFloat 类的源代码概述:

Double 类:

public final class Double extends Number implements Comparable<Double> {
    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_NORMAL = 0x1.0p-1022;
    public static final double MIN_VALUE = 0x0.0000000000001P-1022;

    public static final int MAX_EXPONENT = 1023;
    public static final int MIN_EXPONENT = -1022;

    public static final int SIZE = 64;
    public static final int BYTES = SIZE / Byte.SIZE;

    public static final Class<Double> TYPE = (Class<Double>) Class.getPrimitiveClass("double");

    private final double value;

    public Double(double value) {
        this.value = value;
    }

    public Double(String s) throws NumberFormatException {
        this.value = parseDouble(s);
    }

    public static String toString(double d) {
        return FloatingDecimal.toJavaFormatString(d);
    }

    public static Double valueOf(String s) throws NumberFormatException {
        return new Double(parseDouble(s));
    }

    public static Double valueOf(double d) {
        return new Double(d);
    }

    public static double parseDouble(String s) throws NumberFormatException {
        return FloatingDecimal.parseDouble(s);
    }

    public static boolean isNaN(double v) {
        return v != v;
    }

    public static boolean isInfinite(double v) {
        return v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY;
    }

    public boolean isNaN() {
        return isNaN(value);
    }

    public boolean isInfinite() {
        return isInfinite(value);
    }

    public String toString() {
        return Double.toString(value);
    }

    public byte byteValue() {
        return (byte) value;
    }

    public short shortValue() {
        return (short) value;
    }

    public int intValue() {
        return (int) value;
    }

    public long longValue() {
        return (long) value;
    }

    public float floatValue() {
        return (float) value;
    }

    public double doubleValue() {
        return value;
    }

    public int hashCode() {
        return Double.hashCode(value);
    }

    public boolean equals(Object obj) {
        return (obj instanceof Double) && (doubleToLongBits(((Double) obj).value) == doubleToLongBits(value));
    }

    public static long doubleToLongBits(double value) {
        return Double.doubleToLongBits(value);
    }

    public static native long doubleToRawLongBits(double value);

    public static native double longBitsToDouble(long bits);

    public int compareTo(Double anotherDouble) {
        return Double.compare(value, anotherDouble.value);
    }

    public static int compare(double d1, double d2) {
        return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0);
    }

    public static double sum(double a, double b) {
        return a + b;
    }

    public static double max(double a, double b) {
        return Math.max(a, b);
    }

    public static double min(double a, double b) {
        return Math.min(a, b);
    }
}

Float 类:

public final class Float extends Number implements Comparable<Float> {
    public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
    public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
    public static final float NaN = 0.0f / 0.0f;

    public static final float MAX_VALUE = 0x1.fffffeP+127f;
    public static final float MIN_NORMAL = 0x1.0p-126f;
    public static final float MIN_VALUE = 0x0.000002P-126f;

    public static final int MAX_EXPONENT = 127;
    public static final int MIN_EXPONENT = -126;

    public static final int SIZE = 32;
    public static final int BYTES = SIZE / Byte.SIZE;

    public static final Class<Float> TYPE = (Class<Float>) Class.getPrimitiveClass("float");

    private final float value;

    public Float(float value) {
        this.value = value;
    }

    public Float(String s) throws NumberFormatException {
        this.value = parseFloat(s);
    }

    public static String toString(float f) {
        return FloatingDecimal.toJavaFormatString(f);
    }

    public static Float valueOf(String s) throws NumberFormatException {
        return new Float(parseFloat(s));
    }

    public static Float valueOf(float f) {
        return new Float(f);
    }

    public static float parseFloat(String s) throws NumberFormatException {
        return FloatingDecimal.parseFloat(s);
    }

    public static boolean isNaN(float v) {
        return v != v;
    }

    public static boolean isInfinite(float v) {
        return v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY;
    }

    public boolean isNaN() {
        return isNaN(value);
    }

    public boolean isInfinite() {
        return isInfinite(value);
    }

    public String toString() {
        return Float.toString(value);
    }

    public byte byteValue() {
        return (byte) value;
    }

    public short shortValue() {
        return (short) value;
    }

    public int intValue() {
        return (int) value;
    }

    public long longValue() {
        return (long) value;
    }

    public float floatValue() {
        return value;
    }

    public double doubleValue() {
        return (double) value;
    }

    public int hashCode() {
        return Float.hashCode(value);
    }

    public boolean equals(Object obj) {
        return (obj instanceof Float) && (floatToIntBits(((Float) obj).value) == floatToIntBits(value));
    }

    public static int floatToIntBits(float value) {
        return Float.floatToIntBits(value);
    }

    public static native int floatToRawIntBits(float value);

    public static native float intBitsToFloat(int bits);

    public int compareTo(Float anotherFloat) {
        return Float.compare(value, anotherFloat.value);
    }

    public static int compare(float f1, float f2) {
        if (f1 < f2) {
            return -1;
        }
        if (f1 > f2) {
            return 1;
        }
        int thisBits = floatToIntBits(f1);
        int anotherBits = floatToIntBits(f2);
        return (thisBits == anotherBits ? 0 : (thisBits < anotherBits ? -1 : 1));
    }

    public static float sum(float a, float b) {
        return a + b;
    }

    public static float max(float a, float b) {
        return Math.max(a, b);
    }

    public static float min(float a, float b) {
        return Math.min(a, b);
    }
}

这些类提供了处理浮点数的方法和常量。其中,DoubleFloat 类都包含了表示正无穷大、负无穷大和 NaN(非数字)的常量,以及表示最大值、最小值和字节大小等常量。这些类还提供了用于转换、解析、比较和计算浮点数的方法。

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值