double.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 697 行 · 第 1/2 页
JAVA
697 行
}
/**
* Return the value of this <code>Double</code> as a <code>short</code>.
*
* @return the short value
* @since 1.1
*/
public short shortValue() {
return (short)value;
}
/**
* Return the value of this <code>Double</code> as an <code>int</code>.
*
* @return the int value
*/
public int intValue() {
return (int)value;
}
/**
* Return the value of this <code>Double</code> as a <code>long</code>.
*
* @return the long value
*/
public long longValue() {
return (long)value;
}
/**
* Return the value of this <code>Double</code> as a <code>float</code>.
*
* @return the float value
*/
public float floatValue() {
return (float)value;
}
/**
* Return the value of this <code>Double</code>.
*
* @return the double value
*/
public double doubleValue() {
return value;
}
/**
* Return a hashcode representing this Object. <code>Double</code>'s hash
* code is calculated by:<br>
* <code>long v = Double.doubleToLongBits(doubleValue());<br>
* int hash = (int)(v^(v>>32))</code>.
*
* @return this Object's hash code
* @see #doubleToLongBits(double)
*/
public int hashCode() {
long v = doubleToLongBits(value);
return (int) (v ^ (v >>> 32));
}
/**
* Returns <code>true</code> if <code>obj</code> is an instance of
* <code>Double</code> and represents the same double value. Unlike comparing
* two doubles with <code>==</code>, this treats two instances of
* <code>Double.NaN</code> as equal, but treats <code>0.0</code> and
* <code>-0.0</code> as unequal.
*
* <p>Note that <code>d1.equals(d2)<code> is identical to
* <code>doubleToLongBits(d1.doubleValue()) ==
* doubleToLongBits(d2.doubleValue())<code>.
*
* @param obj the object to compare
* @return whether the objects are semantically equal
*/
public boolean equals(Object obj) {
if (!(obj instanceof Double))
return false;
double d = ((Double)obj).value;
// Avoid call to native method. However, some implementations, like gcj,
// are better off using floatToIntBits(value) == floatToIntBits(f).
// Check common case first, then check NaN and 0.
if (value == d)
return (value != 0) || (1 / value == 1 / d);
return isNaN(value) && isNaN(d);
}
/**
* Convert the double to the IEEE 754 floating-point "double format" bit
* layout. Bit 63 (the most significant) is the sign bit, bits 62-52
* (masked by 0x7ff0000000000000L) represent the exponent, and bits 51-0
* (masked by 0x000fffffffffffffL) are the mantissa. This function
* collapses all versions of NaN to 0x7ff8000000000000L. The result of this
* function can be used as the argument to
* <code>Double.longBitsToDouble(long)</code> to obtain the original
* <code>double</code> value.
*
* @param value the <code>double</code> to convert
* @return the bits of the <code>double</code>
* @see #longBitsToDouble(long)
*/
public static long doubleToLongBits(double value) {
if (isNaN(value)) {
return 0x7ff8000000000000L;
} else {
return Unsafe.doubleToRawLongBits(value);
}
}
/**
* Convert the double to the IEEE 754 floating-point "double format" bit
* layout. Bit 63 (the most significant) is the sign bit, bits 62-52
* (masked by 0x7ff0000000000000L) represent the exponent, and bits 51-0
* (masked by 0x000fffffffffffffL) are the mantissa. This function
* leaves NaN alone, rather than collapsing to a canonical value. The
* result of this function can be used as the argument to
* <code>Double.longBitsToDouble(long)</code> to obtain the original
* <code>double</code> value.
*
* @param value the <code>double</code> to convert
* @return the bits of the <code>double</code>
* @see #longBitsToDouble(long)
*/
public static long doubleToRawLongBits(double value) {
return Unsafe.doubleToRawLongBits(value);
}
/**
* Convert the argument in IEEE 754 floating-point "double format" bit
* layout to the corresponding float. Bit 63 (the most significant) is the
* sign bit, bits 62-52 (masked by 0x7ff0000000000000L) represent the
* exponent, and bits 51-0 (masked by 0x000fffffffffffffL) are the mantissa.
* This function leaves NaN alone, so that you can recover the bit pattern
* with <code>Double.doubleToRawLongBits(double)</code>.
*
* @param bits the bits to convert
* @return the <code>double</code> represented by the bits
* @see #doubleToLongBits(double)
* @see #doubleToRawLongBits(double)
*/
public static double longBitsToDouble(long bits) {
return Unsafe.longBitsToDouble(bits);
}
/**
* Compare two Doubles numerically by comparing their <code>double</code>
* values. The result is positive if the first is greater, negative if the
* second is greater, and 0 if the two are equal. However, this special
* cases NaN and signed zero as follows: NaN is considered greater than
* all other doubles, including <code>POSITIVE_INFINITY</code>, and positive
* zero is considered greater than negative zero.
*
* @param d the Double to compare
* @return the comparison
* @since 1.2
*/
public int compareTo(Double d) {
return compare(value, d.value);
}
/**
* Behaves like <code>compareTo(Double)</code> unless the Object
* is not an <code>Double</code>.
*
* @param o the object to compare
* @return the comparison
* @throws ClassCastException if the argument is not a <code>Double</code>
* @see #compareTo(Double)
* @see Comparable
* @since 1.2
*/
public int compareTo(Object o) {
return compare(value, ((Double)o).value);
}
/**
* Behaves like <code>new Double(x).compareTo(new Double(y))</code>; in
* other words this compares two doubles, special casing NaN and zero,
* without the overhead of objects.
*
* @param x the first double to compare
* @param y the second double to compare
* @return the comparison
* @since 1.4
*/
public static int compare(double x, double y) {
if (isNaN(x))
return isNaN(y) ? 0 : 1;
if (isNaN(y))
return -1;
// recall that 0.0 == -0.0, so we convert to infinites and try again
if (x == 0 && y == 0)
return (int) (1 / x - 1 / y);
if (x == y)
return 0;
return x > y ? 1 : -1;
}
final static class DoubleParser {
private char[] chars;
private int length;
private int index;
private int parseUnsignedInt() throws NumberFormatException {
if (index >= length)
throw new NumberFormatException();
int start = index;
int value = 0;
for (; index < length; index++) {
int d = Character.digit(chars[index], 10);
if (d < 0)
break;
value *= 10;
value += d;
}
if (index == start)
throw new NumberFormatException();
return value;
}
private int parseSignedInt() throws NumberFormatException {
if (index >= length)
throw new NumberFormatException();
char sign = ' ';
switch (chars[index]) {
case '-' :
sign = '-';
index++;
break;
case '+' :
sign = '+';
index++;
break;
}
int value = parseUnsignedInt();
return (sign == '-') ? -value : value;
}
private double parseFractionalPart(boolean nonEmpty)
throws NumberFormatException {
if (index >= length)
throw new NumberFormatException();
int start = index;
double value = 0.0d;
for (; index < length; index++) {
int d = Character.digit(chars[index], 10);
if (d < 0)
break;
value += d;
value /= 10;
}
if (nonEmpty && (index == start))
throw new NumberFormatException();
return value;
}
private double parseExponent(double value)
throws NumberFormatException {
if (index >= chars.length)
return value;
switch (chars[index]) {
case 'e' :
case 'E' :
index++;
break;
default :
throw new NumberFormatException();
}
int exponent = parseSignedInt();
if (index < chars.length)
throw new NumberFormatException();
return value * Math.pow(10.0, (double)exponent);
}
public double parse() throws NumberFormatException {
if (index >= chars.length)
throw new NumberFormatException();
char sign = '+';
switch (chars[index]) {
case '-' :
sign = '-';
index++;
break;
case '+' :
sign = '+';
index++;
break;
}
if (index >= chars.length)
throw new NumberFormatException();
double value;
if (chars[index] == '.') {
index++;
value = parseFractionalPart(true);
value = parseExponent(value);
} else {
value = parseUnsignedInt();
if (index >= chars.length)
throw new NumberFormatException();
if (chars[index] != '.')
throw new NumberFormatException();
index++;
value += parseFractionalPart(false);
value = parseExponent(value);
}
return (sign == '-') ? -value : value;
}
public DoubleParser(String s) {
chars = s.toCharArray();
length = chars.length;
index = 0;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?