float.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 555 行 · 第 1/2 页
JAVA
555 行
* @see #MAX_VALUE
* @see #POSITIVE_INFINITY
* @see #NEGATIVE_INFINITY
* @since 1.2
*/
public static float parseFloat(String s) {
// XXX Rounding parseDouble() causes some errors greater than 1 ulp from
// the infinitely precise decimal.
return (float)Double.parseDouble(s);
}
/**
* Return <code>true</code> if the <code>float</code> has the same
* value as <code>NaN</code>, otherwise return <code>false</code>.
*
* @param v the <code>float</code> to compare
* @return whether the argument is <code>NaN</code>
*/
public static boolean isNaN(float v) {
return isNaN(floatToRawIntBits(v));
}
private static boolean isNaN(int b) {
return ((b & EXPONENT_MASK) == EXPONENT_MASK && (b & MANTISSA_MASK) != 0);
}
/**
* Return <code>true</code> if the <code>float</code> has a value
* equal to either <code>NEGATIVE_INFINITY</code> or
* <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
*
* @param v the <code>float</code> to compare
* @return whether the argument is (-/+) infinity
*/
public static boolean isInfinite(float v) {
return v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY;
}
/**
* Return <code>true</code> if the value of this <code>Float</code>
* is the same as <code>NaN</code>, otherwise return <code>false</code>.
*
* @return whether this <code>Float</code> is <code>NaN</code>
*/
public boolean isNaN() {
return isNaN(value);
}
/**
* Return <code>true</code> if the value of this <code>Float</code>
* is the same as <code>NEGATIVE_INFINITY</code> or
* <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
*
* @return whether this <code>Float</code> is (-/+) infinity
*/
public boolean isInfinite() {
return isInfinite(value);
}
/**
* Convert the <code>float</code> value of this <code>Float</code>
* to a <code>String</code>. This method calls
* <code>Float.toString(float)</code> to do its dirty work.
*
* @return the <code>String</code> representation
* @see #toString(float)
*/
public String toString() {
return toString(value);
}
/**
* Return the value of this <code>Float</code> as a <code>byte</code>.
*
* @return the byte value
* @since 1.1
*/
public byte byteValue() {
return (byte)value;
}
/**
* Return the value of this <code>Float</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>Integer</code> as an <code>int</code>.
*
* @return the int value
*/
public int intValue() {
return (int)value;
}
/**
* Return the value of this <code>Integer</code> as a <code>long</code>.
*
* @return the long value
*/
public long longValue() {
return (long)value;
}
/**
* Return the value of this <code>Float</code>.
*
* @return the float value
*/
public float floatValue() {
return value;
}
/**
* Return the value of this <code>Float</code> as a <code>double</code>
*
* @return the double value
*/
public double doubleValue() {
return value;
}
/**
* Return a hashcode representing this Object. <code>Float</code>'s hash
* code is calculated by calling <code>floatToIntBits(floatValue())</code>.
*
* @return this Object's hash code
* @see #floatToIntBits(float)
*/
public int hashCode() {
return floatToIntBits(value);
}
/**
* Returns <code>true</code> if <code>obj</code> is an instance of
* <code>Float</code> and represents the same float value. Unlike comparing
* two floats with <code>==</code>, this treats two instances of
* <code>Float.NaN</code> as equal, but treats <code>0.0</code> and
* <code>-0.0</code> as unequal.
*
* <p>Note that <code>f1.equals(f2)<code> is identical to
* <code>floatToIntBits(f1.floatValue()) ==
* floatToIntBits(f2.floatValue())<code>.
*
* @param obj the object to compare
* @return whether the objects are semantically equal
*/
public boolean equals(Object obj) {
if (!(obj instanceof Float))
return false;
float f = ((Float)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 == f)
return (value != 0) || (1 / value == 1 / f);
return isNaN(value) && isNaN(f);
}
/**
* Convert the float to the IEEE 754 floating-point "single format" bit
* layout. Bit 31 (the most significant) is the sign bit, bits 30-23
* (masked by 0x7f800000) represent the exponent, and bits 22-0
* (masked by 0x007fffff) are the mantissa. This function collapses all
* versions of NaN to 0x7fc00000. The result of this function can be used
* as the argument to <code>Float.intBitsToFloat(int)</code> to obtain the
* original <code>float</code> value.
*
* @param value the <code>float</code> to convert
* @return the bits of the <code>float</code>
* @see #intBitsToFloat(int)
*/
public static int floatToIntBits(float value) {
if (isNaN(value)) {
return 0x7fc00000;
} else {
return Unsafe.floatToRawIntBits(value);
}
}
/**
* Convert the float to the IEEE 754 floating-point "single format" bit
* layout. Bit 31 (the most significant) is the sign bit, bits 30-23
* (masked by 0x7f800000) represent the exponent, and bits 22-0
* (masked by 0x007fffff) 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>Float.intBitsToFloat(int)</code> to
* obtain the original <code>float</code> value.
*
* @param value the <code>float</code> to convert
* @return the bits of the <code>float</code>
* @see #intBitsToFloat(int)
*/
public static int floatToRawIntBits(float value) {
return Unsafe.floatToRawIntBits(value);
}
/**
* Convert the argument in IEEE 754 floating-point "single format" bit
* layout to the corresponding float. Bit 31 (the most significant) is the
* sign bit, bits 30-23 (masked by 0x7f800000) represent the exponent, and
* bits 22-0 (masked by 0x007fffff) are the mantissa. This function leaves
* NaN alone, so that you can recover the bit pattern with
* <code>Float.floatToRawIntBits(float)</code>.
*
* @param bits the bits to convert
* @return the <code>float</code> represented by the bits
* @see #floatToIntBits(float)
* @see #floatToRawIntBits(float)
*/
public static float intBitsToFloat(int bits) {
return Unsafe.intBitsToFloat(bits);
}
/**
* Compare two Floats numerically by comparing their <code>float</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 floats, including <code>POSITIVE_INFINITY</code>, and positive
* zero is considered greater than negative zero.
*
* @param f the Float to compare
* @return the comparison
* @since 1.2
*/
public int compareTo(Float f) {
return compare(value, f.value);
}
/**
* Behaves like <code>compareTo(Float)</code> unless the Object
* is not an <code>Float</code>.
*
* @param o the object to compare
* @return the comparison
* @throws ClassCastException if the argument is not a <code>Float</code>
* @see #compareTo(Float)
* @see Comparable
* @since 1.2
*/
public int compareTo(Object o) {
return compare(value, ((Float)o).value);
}
/**
* Behaves like <code>new Float(x).compareTo(new Float(y))</code>; in
* other words this compares two floats, special casing NaN and zero,
* without the overhead of objects.
*
* @param x the first float to compare
* @param y the second float to compare
* @return the comparison
* @since 1.4
*/
public static int compare(float x, float y) {
if (isNaN(x))
return isNaN(y) ? 0 : 1;
if (isNaN(y))
return -1;
// recall that 0.0 == -0.0, so we convert to infinities and try again
if (x == 0 && y == 0)
return (int) (1 / x - 1 / y);
if (x == y)
return 0;
return x > y ? 1 : -1;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?