Double
非可变类
实现对比接口
继承于Number
成员变量
double POSITIVE_INFINITY 正无穷大的表示方式
double NEGATIVE_INFINITY 负无穷大的表示方式
double NaN 无穷大的表示方式
double MAX_VALUE 最大表示数的十六进制方式
double MIN_NORMAL 浮点数的最小正数
double MIN_VALUE 最小数值表示方式
int MAX_EXPONENT 有限数里面最大可表示的指数
int MIN_EXPONENT 有限数里面最小可表示的指数
int SIZE 浮点型的bit位数
int BYTES 浮点型的字节数
Class<Double> TYPE Class.getPrimitiveClass("double")从虚拟机拿出合适的类型
final double value 初始化值
构造方法
public Double(String s) throws NumberFormatException {
value = parseDouble(s);
}
函数
String toString(double d) 返回字符串形式
public static String toString(double d) {
return FloatingDecimal.toJavaFormatString(d);
}
- 输入为负无穷大,输出为-Infinity
- 输入为正无穷大,输出为Infinity
- 输入为无穷大,输出为NAN
- 输入为正常数,输出为正常数
String toHexString(double d) 返回十六进制字符串形式
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) )
// 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) // value is negative,
answer.append("-"); // so append sign info
answer.append("0x");
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)
& 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');
// 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. 首先判断数值是否超过正常数的范围(趋向于无穷)
2. 初始化字符串长度为24,判断是否为负数
3. 添加十六进制头部0x,取绝对值,如果为0,则添加0.0p0
4. 判断是否小于最小的标准数,添加0.或者1.
5. (Double.doubleToLongBits(d)& DoubleConsts.SIGNIF_BIT_MASK) | 0x1000000000000000L) 根据IEEE 754浮点双精度格式,返回指定数值的浮点值 & SIGNIF_BIT_MASK | 0x1000000000000000L,具体意思楼主也没懂,可能是优化之类的运算
6. 截取返回指定数字signifBits 的 16 进制的无符号整数的字符串
7. 过滤多余的0元素,添加字符p
8. 添加最后的指数
double parseDouble(String s) 字符串转换成浮点型
boolean isNaN(double v) 判断是否无穷大
boolean isInfinite(double v) 是否为正无穷或者负无穷
boolean isFinite(double d) 绝对值是否超过范围
int hashCode(double value) 获取hash码
public static int hashCode(double value) {
long bits = doubleToLongBits(value);
return (int)(bits ^ (bits >>> 32));
}
根据IEEE 754浮点双精度格式,返回指定数值的浮点值,改变低32位
boolean equals(Object obj) 判断对象是否相等
public boolean equals(Object obj) {
return (obj instanceof Double)
&& (doubleToLongBits(((Double)obj).value) ==
doubleToLongBits(value));
}
根据IEEE 754浮点双精度格式,返回指定数值的浮点值进行判断
long doubleToLongBits(double value) 获取数值的IEEE754浮点“双精度格式
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浮点双精度格式,返回指定数值的浮点值,判断是否为无穷。
native long doubleToRawLongBits(double value) 获取数值的IEEE754浮点“双精度格式
native double longBitsToDouble(long bits) 获取浮点双精度格式的double数值
int 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)
}
1.判断大小
2.获取双精度格式进行判断
如果有讲解错误,请留言联系作者及时删除,避免引导错误。