其他 源码解析 https://blog.csdn.net/qq_32726809/article/category/8035214
1类的声明
public final class Double extends Number implements Comparable<Double>
- 继承number 可以进行 number族的相互转化。
- 实现coparable 接口 可以进行比较。
2属性
public static final double POSITIVE_INFINITY = 1.0 / 0.0;//1
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;//2
public static final double NaN = 0.0d / 0.0;//3
public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308//4
public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308//5
public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324//6
public static final int MAX_EXPONENT = 1023;//7
public static final int MIN_EXPONENT = -1022;//8
public static final int SIZE = 64;//9
public static final int BYTES = SIZE / Byte.SIZE;//10
private final double value;//11
- 1 表示正无穷 程序运行值为 Infinity(无穷)
- 2 表示负无穷 运行值 为 -Infinity (负无穷)
- 3 表示非数字的double变量 运行值 为 NaN
- 4 表示最大值
- 5 表示规约所能表示的最小值
- 6 表示最小正数值
- 7 表示 指数的最大值
- 8 表示 指数的最小值
- 9 表示 所占的位数 64位
- 10 表示字节数 8个字节 ,这里除的是 Byte的静态变量,Byte.SIZE大小是8
- 11 表示 Double一旦被创建,值便不可更改 ,类似于string
- p表示2
3构造函数
3.1Double(double value)
public Double(double value) {
this.value = value;
}
- 直接把 double副歌value
3.2 Double(String s)
将string转化为 double
public Double(String s) throws NumberFormatException {
value = parseDouble(s);
}
public static double parseDouble(String s) throws NumberFormatException {
return FloatingDecimal.parseDouble(s);//1
}
- 1处未找见源码,所以不做解释
4方法
4.1 toHexString(double d)
将double转为16进制的字符串
public static String toHexString(double d) {
/*
* Modeled after the "a" conversion specifier in C99, section
* 7.19.6.1; however, the output of this method is more
* tightly specified.
*/
if (!isFinite(d) )/*----------1*/
// For infinity and NaN, use the decimal output.
return Double.toString(d);
else {
// Initialized to maximum size of output.
StringBuilder answer = new StringBuilder(24);
if (Math.copySign(1.0, d) == -1.0) /*--------2*/ // value is negative,
answer.append("-"); // so append sign info
answer.append("0x");/*--------3*/
d = Math.abs(d);
if(d == 0.0) {
answer.append("0.0p0");
} else {
boolean subnormal = (d < DoubleConsts.MIN_NORMAL);
// Isolate significand bits and OR in a high-order bit
// so that the string representation has a known
// length.
long signifBits = (Double.doubleToLongBits(d)/*-----------4*/
& DoubleConsts.SIGNIF_BIT_MASK) |
0x1000000000000000L;
// Subnormal values have a 0 implicit bit; normal
// values have a 1 implicit bit.
answer.append(subnormal ? "0." : "1.");
// Isolate the low-order 13 digits of the hex
// representation. If all the digits are zero,
// replace with a single 0; otherwise, remove all
// trailing zeros.
String signif = Long.toHexString(signifBits).substring(3,16);
answer.append(signif.equals("0000000000000") ? // 13 zeros
"0":
signif.replaceFirst("0{1,12}$", ""));
answer.append('p');/*-------5*/
// If the value is subnormal, use the E_min exponent
// value for double; otherwise, extract and report d's
// exponent (the representation of a subnormal uses
// E_min -1).
answer.append(subnormal ?
DoubleConsts.MIN_EXPONENT:
Math.getExponent(d));
}
return answer.toString();
}
}
- 1 查看这个double是否是有限的
- 2 用来判断是是正数还是负数。
- 3 添加16进制标识
- 4 将double格式化成 IEEE 754 浮点双精度格式
4.2valueOf
获取double对象
public static Double valueOf(String s) throws NumberFormatException {
return new Double(parseDouble(s));
}
public static Double valueOf(double d) {
return new Double(d);
}
4.3parseDouble(String s)
将字符串转为double类型(暂无源码)
public static double parseDouble(String s) throws NumberFormatException {
return FloatingDecimal.parseDouble(s);
}
4.4 isNaN(double v)
判断一个double值是否是NAN
public static boolean isNaN(double v) {
return (v != v);//1
}
- 1处的意思是 在java中 NAN的值不等于NAN的值,所有的无穷值也不相等,所以本身跟本身比较若不相等则是true,这里的不等指的是 值的不等。 例如代码
Double na=Double.NaN;
System.out.println(na.doubleValue()==na.doubleValue());//1
返回的是false
4.5 Infinite(有限判定)
public static boolean isInfinite(double v) {
return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
}
public static boolean isFinite(double d) {
return Math.abs(d) <= DoubleConsts.MAX_VALUE;
}
通过上面的代码可以看出 ,有限判定只要与正无穷和负无穷比较
4.6 hashCode()
public int hashCode() {
return Double.hashCode(value);
}
public static int hashCode(double value) {
long bits = doubleToLongBits(value);
return (int)(bits ^ (bits >>> 32));
}
double的hash的是 IEEE 754 码相关。
4.7equals(Object obj)
public boolean equals(Object obj) {
return (obj instanceof Double)
&& (doubleToLongBits(((Double)obj).value) ==
doubleToLongBits(value));
}
- 主要是判断 IEEE 754 码是否相等
- 两个无穷大编码
- 111111111110000000000000000000000000000000000000000000000000000 正无穷大
- 1111111111110000000000000000000000000000000000000000000000000000 负无穷大
- 这里的相等于上面的相等不同 ,这里是对象相等是Double对象相等
compare(double d1, double d2)
public static int compare(double d1, double d2) {
if (d1 < d2)
return -1; // Neither val is NaN, thisVal is smaller
if (d1 > d2)
return 1; // Neither val is NaN, thisVal is larger
// Cannot use doubleToRawLongBits because of possibility of NaNs.
long thisBits = Double.doubleToLongBits(d1);
long anotherBits = Double.doubleToLongBits(d2);
return (thisBits == anotherBits ? 0 : // Values are equal
(thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1)); // (0.0, -0.0) or (NaN, !NaN)
}
比较这个方法没有用到 IEEE 754 码来记性比较,,有可能是NAN
doubleToLongBits(double value)
将double转化为 IEEE 754 码
public static long doubleToLongBits(double value) {
long result = doubleToRawLongBits(value);
// Check for NaN based on values of bit fields, maximum
// exponent and nonzero significand.
if ( ((result & DoubleConsts.EXP_BIT_MASK) ==
DoubleConsts.EXP_BIT_MASK) &&
(result & DoubleConsts.SIGNIF_BIT_MASK) != 0L)
result = 0x7ff8000000000000L;
return result;
}
注释
IEEE 754 码
转换步骤
- 1 转化为2进制 例如转化3 为 1.1*2^1
- 2 对指数加上1023,会把符号位移除 1.1*2^1024
- 3 转化 注意没有符号位
- 1-11为指数位 就是 100 0000 0000
- 12-64为尾数位 就是 1后面 有52个0
- 4 最后值为 1000_0000_000 1_000000000000000000000000000000000000000000000000000