jaroslav@67: /* jaroslav@67: * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved. jaroslav@67: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@67: * jaroslav@67: * This code is free software; you can redistribute it and/or modify it jaroslav@67: * under the terms of the GNU General Public License version 2 only, as jaroslav@67: * published by the Free Software Foundation. Oracle designates this jaroslav@67: * particular file as subject to the "Classpath" exception as provided jaroslav@67: * by Oracle in the LICENSE file that accompanied this code. jaroslav@67: * jaroslav@67: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@67: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@67: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@67: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@67: * accompanied this code). jaroslav@67: * jaroslav@67: * You should have received a copy of the GNU General Public License version jaroslav@67: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@67: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@67: * jaroslav@67: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@67: * or visit www.oracle.com if you need additional information or have any jaroslav@67: * questions. jaroslav@67: */ jaroslav@67: jaroslav@67: package java.lang; jaroslav@67: jaroslav@116: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@116: jaroslav@67: /** jaroslav@67: * The {@code Float} class wraps a value of primitive type jaroslav@67: * {@code float} in an object. An object of type jaroslav@67: * {@code Float} contains a single field whose type is jaroslav@67: * {@code float}. jaroslav@67: * jaroslav@67: *

In addition, this class provides several methods for converting a jaroslav@67: * {@code float} to a {@code String} and a jaroslav@67: * {@code String} to a {@code float}, as well as other jaroslav@67: * constants and methods useful when dealing with a jaroslav@67: * {@code float}. jaroslav@67: * jaroslav@67: * @author Lee Boynton jaroslav@67: * @author Arthur van Hoff jaroslav@67: * @author Joseph D. Darcy jaroslav@67: * @since JDK1.0 jaroslav@67: */ jaroslav@67: public final class Float extends Number implements Comparable { jaroslav@67: /** jaroslav@67: * A constant holding the positive infinity of type jaroslav@67: * {@code float}. It is equal to the value returned by jaroslav@67: * {@code Float.intBitsToFloat(0x7f800000)}. jaroslav@67: */ jaroslav@67: public static final float POSITIVE_INFINITY = 1.0f / 0.0f; jaroslav@67: jaroslav@67: /** jaroslav@67: * A constant holding the negative infinity of type jaroslav@67: * {@code float}. It is equal to the value returned by jaroslav@67: * {@code Float.intBitsToFloat(0xff800000)}. jaroslav@67: */ jaroslav@67: public static final float NEGATIVE_INFINITY = -1.0f / 0.0f; jaroslav@67: jaroslav@67: /** jaroslav@67: * A constant holding a Not-a-Number (NaN) value of type jaroslav@67: * {@code float}. It is equivalent to the value returned by jaroslav@67: * {@code Float.intBitsToFloat(0x7fc00000)}. jaroslav@67: */ jaroslav@67: public static final float NaN = 0.0f / 0.0f; jaroslav@67: jaroslav@67: /** jaroslav@67: * A constant holding the largest positive finite value of type jaroslav@67: * {@code float}, (2-2-23)·2127. jaroslav@67: * It is equal to the hexadecimal floating-point literal jaroslav@67: * {@code 0x1.fffffeP+127f} and also equal to jaroslav@67: * {@code Float.intBitsToFloat(0x7f7fffff)}. jaroslav@67: */ jaroslav@67: public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f jaroslav@67: jaroslav@67: /** jaroslav@67: * A constant holding the smallest positive normal value of type jaroslav@67: * {@code float}, 2-126. It is equal to the jaroslav@67: * hexadecimal floating-point literal {@code 0x1.0p-126f} and also jaroslav@67: * equal to {@code Float.intBitsToFloat(0x00800000)}. jaroslav@67: * jaroslav@67: * @since 1.6 jaroslav@67: */ jaroslav@67: public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f jaroslav@67: jaroslav@67: /** jaroslav@67: * A constant holding the smallest positive nonzero value of type jaroslav@67: * {@code float}, 2-149. It is equal to the jaroslav@67: * hexadecimal floating-point literal {@code 0x0.000002P-126f} jaroslav@67: * and also equal to {@code Float.intBitsToFloat(0x1)}. jaroslav@67: */ jaroslav@67: public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f jaroslav@67: jaroslav@67: /** jaroslav@67: * Maximum exponent a finite {@code float} variable may have. It jaroslav@67: * is equal to the value returned by {@code jaroslav@67: * Math.getExponent(Float.MAX_VALUE)}. jaroslav@67: * jaroslav@67: * @since 1.6 jaroslav@67: */ jaroslav@67: public static final int MAX_EXPONENT = 127; jaroslav@67: jaroslav@67: /** jaroslav@67: * Minimum exponent a normalized {@code float} variable may have. jaroslav@67: * It is equal to the value returned by {@code jaroslav@67: * Math.getExponent(Float.MIN_NORMAL)}. jaroslav@67: * jaroslav@67: * @since 1.6 jaroslav@67: */ jaroslav@67: public static final int MIN_EXPONENT = -126; jaroslav@67: jaroslav@67: /** jaroslav@67: * The number of bits used to represent a {@code float} value. jaroslav@67: * jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static final int SIZE = 32; jaroslav@67: jaroslav@67: /** jaroslav@67: * The {@code Class} instance representing the primitive type jaroslav@67: * {@code float}. jaroslav@67: * jaroslav@67: * @since JDK1.1 jaroslav@67: */ jaroslav@67: public static final Class TYPE = Class.getPrimitiveClass("float"); jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a string representation of the {@code float} jaroslav@67: * argument. All characters mentioned below are ASCII characters. jaroslav@67: *

jaroslav@67: * How many digits must be printed for the fractional part of jaroslav@67: * m or a? There must be at least one digit jaroslav@67: * to represent the fractional part, and beyond that as many, but jaroslav@67: * only as many, more digits as are needed to uniquely distinguish jaroslav@67: * the argument value from adjacent values of type jaroslav@67: * {@code float}. That is, suppose that x is the jaroslav@67: * exact mathematical value represented by the decimal jaroslav@67: * representation produced by this method for a finite nonzero jaroslav@67: * argument f. Then f must be the {@code float} jaroslav@67: * value nearest to x; or, if two {@code float} values are jaroslav@67: * equally close to x, then f must be one of jaroslav@67: * them and the least significant bit of the significand of jaroslav@67: * f must be {@code 0}. jaroslav@67: * jaroslav@67: *

To create localized string representations of a floating-point jaroslav@67: * value, use subclasses of {@link java.text.NumberFormat}. jaroslav@67: * jaroslav@67: * @param f the float to be converted. jaroslav@67: * @return a string representation of the argument. jaroslav@67: */ jaroslav@67: public static String toString(float f) { jaroslav@187: return Double.toString(f); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a hexadecimal string representation of the jaroslav@67: * {@code float} argument. All characters mentioned below are jaroslav@67: * ASCII characters. jaroslav@67: * jaroslav@67: *

jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: *

Examples

Floating-point ValueHexadecimal String
{@code 1.0} {@code 0x1.0p0}
{@code -1.0} {@code -0x1.0p0}
{@code 2.0} {@code 0x1.0p1}
{@code 3.0} {@code 0x1.8p1}
{@code 0.5} {@code 0x1.0p-1}
{@code 0.25} {@code 0x1.0p-2}
{@code Float.MAX_VALUE}{@code 0x1.fffffep127}
{@code Minimum Normal Value}{@code 0x1.0p-126}
{@code Maximum Subnormal Value}{@code 0x0.fffffep-126}
{@code Float.MIN_VALUE}{@code 0x0.000002p-126}
jaroslav@67: * @param f the {@code float} to be converted. jaroslav@67: * @return a hex string representation of the argument. jaroslav@67: * @since 1.5 jaroslav@67: * @author Joseph D. Darcy jaroslav@67: */ jaroslav@67: public static String toHexString(float f) { jaroslav@84: throw new UnsupportedOperationException(); jaroslav@84: // if (Math.abs(f) < FloatConsts.MIN_NORMAL jaroslav@84: // && f != 0.0f ) {// float subnormal jaroslav@84: // // Adjust exponent to create subnormal double, then jaroslav@84: // // replace subnormal double exponent with subnormal float jaroslav@84: // // exponent jaroslav@84: // String s = Double.toHexString(FpUtils.scalb((double)f, jaroslav@84: // /* -1022+126 */ jaroslav@84: // DoubleConsts.MIN_EXPONENT- jaroslav@84: // FloatConsts.MIN_EXPONENT)); jaroslav@84: // return s.replaceFirst("p-1022$", "p-126"); jaroslav@84: // } jaroslav@84: // else // double string will be the same as float string jaroslav@84: // return Double.toHexString(f); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a {@code Float} object holding the jaroslav@67: * {@code float} value represented by the argument string jaroslav@67: * {@code s}. jaroslav@67: * jaroslav@67: *

If {@code s} is {@code null}, then a jaroslav@67: * {@code NullPointerException} is thrown. jaroslav@67: * jaroslav@67: *

Leading and trailing whitespace characters in {@code s} jaroslav@67: * are ignored. Whitespace is removed as if by the {@link jaroslav@67: * String#trim} method; that is, both ASCII space and control jaroslav@67: * characters are removed. The rest of {@code s} should jaroslav@67: * constitute a FloatValue as described by the lexical jaroslav@67: * syntax rules: jaroslav@67: * jaroslav@67: *

jaroslav@67: *
jaroslav@67: *
FloatValue: jaroslav@67: *
Signopt {@code NaN} jaroslav@67: *
Signopt {@code Infinity} jaroslav@67: *
Signopt FloatingPointLiteral jaroslav@67: *
Signopt HexFloatingPointLiteral jaroslav@67: *
SignedInteger jaroslav@67: *
jaroslav@67: * jaroslav@67: *

jaroslav@67: * jaroslav@67: *

jaroslav@67: *
HexFloatingPointLiteral: jaroslav@67: *
HexSignificand BinaryExponent FloatTypeSuffixopt jaroslav@67: *
jaroslav@67: * jaroslav@67: *

jaroslav@67: * jaroslav@67: *

jaroslav@67: *
HexSignificand: jaroslav@67: *
HexNumeral jaroslav@67: *
HexNumeral {@code .} jaroslav@67: *
{@code 0x} HexDigitsopt jaroslav@67: * {@code .} HexDigits jaroslav@67: *
{@code 0X} HexDigitsopt jaroslav@67: * {@code .} HexDigits jaroslav@67: *
jaroslav@67: * jaroslav@67: *

jaroslav@67: * jaroslav@67: *

jaroslav@67: *
BinaryExponent: jaroslav@67: *
BinaryExponentIndicator SignedInteger jaroslav@67: *
jaroslav@67: * jaroslav@67: *

jaroslav@67: * jaroslav@67: *

jaroslav@67: *
BinaryExponentIndicator: jaroslav@67: *
{@code p} jaroslav@67: *
{@code P} jaroslav@67: *
jaroslav@67: * jaroslav@67: *
jaroslav@67: * jaroslav@67: * where Sign, FloatingPointLiteral, jaroslav@67: * HexNumeral, HexDigits, SignedInteger and jaroslav@67: * FloatTypeSuffix are as defined in the lexical structure jaroslav@67: * sections of jaroslav@67: * The Java™ Language Specification, jaroslav@67: * except that underscores are not accepted between digits. jaroslav@67: * If {@code s} does not have the form of jaroslav@67: * a FloatValue, then a {@code NumberFormatException} jaroslav@67: * is thrown. Otherwise, {@code s} is regarded as jaroslav@67: * representing an exact decimal value in the usual jaroslav@67: * "computerized scientific notation" or as an exact jaroslav@67: * hexadecimal value; this exact numerical value is then jaroslav@67: * conceptually converted to an "infinitely precise" jaroslav@67: * binary value that is then rounded to type {@code float} jaroslav@67: * by the usual round-to-nearest rule of IEEE 754 floating-point jaroslav@67: * arithmetic, which includes preserving the sign of a zero jaroslav@67: * value. jaroslav@67: * jaroslav@67: * Note that the round-to-nearest rule also implies overflow and jaroslav@67: * underflow behaviour; if the exact value of {@code s} is large jaroslav@67: * enough in magnitude (greater than or equal to ({@link jaroslav@67: * #MAX_VALUE} + {@link Math#ulp(float) ulp(MAX_VALUE)}/2), jaroslav@67: * rounding to {@code float} will result in an infinity and if the jaroslav@67: * exact value of {@code s} is small enough in magnitude (less jaroslav@67: * than or equal to {@link #MIN_VALUE}/2), rounding to float will jaroslav@67: * result in a zero. jaroslav@67: * jaroslav@67: * Finally, after rounding a {@code Float} object representing jaroslav@67: * this {@code float} value is returned. jaroslav@67: * jaroslav@67: *

To interpret localized string representations of a jaroslav@67: * floating-point value, use subclasses of {@link jaroslav@67: * java.text.NumberFormat}. jaroslav@67: * jaroslav@67: *

Note that trailing format specifiers, specifiers that jaroslav@67: * determine the type of a floating-point literal jaroslav@67: * ({@code 1.0f} is a {@code float} value; jaroslav@67: * {@code 1.0d} is a {@code double} value), do jaroslav@67: * not influence the results of this method. In other jaroslav@67: * words, the numerical value of the input string is converted jaroslav@67: * directly to the target floating-point type. In general, the jaroslav@67: * two-step sequence of conversions, string to {@code double} jaroslav@67: * followed by {@code double} to {@code float}, is jaroslav@67: * not equivalent to converting a string directly to jaroslav@67: * {@code float}. For example, if first converted to an jaroslav@67: * intermediate {@code double} and then to jaroslav@67: * {@code float}, the string
jaroslav@67: * {@code "1.00000017881393421514957253748434595763683319091796875001d"}
jaroslav@67: * results in the {@code float} value jaroslav@67: * {@code 1.0000002f}; if the string is converted directly to jaroslav@67: * {@code float}, 1.0000001f results. jaroslav@67: * jaroslav@67: *

To avoid calling this method on an invalid string and having jaroslav@67: * a {@code NumberFormatException} be thrown, the documentation jaroslav@67: * for {@link Double#valueOf Double.valueOf} lists a regular jaroslav@67: * expression which can be used to screen the input. jaroslav@67: * jaroslav@67: * @param s the string to be parsed. jaroslav@67: * @return a {@code Float} object holding the value jaroslav@67: * represented by the {@code String} argument. jaroslav@67: * @throws NumberFormatException if the string does not contain a jaroslav@67: * parsable number. jaroslav@67: */ jaroslav@67: public static Float valueOf(String s) throws NumberFormatException { jaroslav@84: throw new UnsupportedOperationException(); jaroslav@84: // return new Float(FloatingDecimal.readJavaFormatString(s).floatValue()); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a {@code Float} instance representing the specified jaroslav@67: * {@code float} value. jaroslav@67: * If a new {@code Float} instance is not required, this method jaroslav@67: * should generally be used in preference to the constructor jaroslav@67: * {@link #Float(float)}, as this method is likely to yield jaroslav@67: * significantly better space and time performance by caching jaroslav@67: * frequently requested values. jaroslav@67: * jaroslav@67: * @param f a float value. jaroslav@67: * @return a {@code Float} instance representing {@code f}. jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static Float valueOf(float f) { jaroslav@67: return new Float(f); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a new {@code float} initialized to the value jaroslav@67: * represented by the specified {@code String}, as performed jaroslav@67: * by the {@code valueOf} method of class {@code Float}. jaroslav@67: * jaroslav@67: * @param s the string to be parsed. jaroslav@67: * @return the {@code float} value represented by the string jaroslav@67: * argument. jaroslav@67: * @throws NullPointerException if the string is null jaroslav@67: * @throws NumberFormatException if the string does not contain a jaroslav@67: * parsable {@code float}. jaroslav@67: * @see java.lang.Float#valueOf(String) jaroslav@67: * @since 1.2 jaroslav@67: */ jaroslav@67: public static float parseFloat(String s) throws NumberFormatException { jaroslav@84: throw new UnsupportedOperationException(); jaroslav@84: // return FloatingDecimal.readJavaFormatString(s).floatValue(); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns {@code true} if the specified number is a jaroslav@67: * Not-a-Number (NaN) value, {@code false} otherwise. jaroslav@67: * jaroslav@67: * @param v the value to be tested. jaroslav@67: * @return {@code true} if the argument is NaN; jaroslav@67: * {@code false} otherwise. jaroslav@67: */ jaroslav@67: static public boolean isNaN(float v) { jaroslav@67: return (v != v); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns {@code true} if the specified number is infinitely jaroslav@67: * large in magnitude, {@code false} otherwise. jaroslav@67: * jaroslav@67: * @param v the value to be tested. jaroslav@67: * @return {@code true} if the argument is positive infinity or jaroslav@67: * negative infinity; {@code false} otherwise. jaroslav@67: */ jaroslav@67: static public boolean isInfinite(float v) { jaroslav@67: return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * The value of the Float. jaroslav@67: * jaroslav@67: * @serial jaroslav@67: */ jaroslav@67: private final float value; jaroslav@67: jaroslav@67: /** jaroslav@67: * Constructs a newly allocated {@code Float} object that jaroslav@67: * represents the primitive {@code float} argument. jaroslav@67: * jaroslav@67: * @param value the value to be represented by the {@code Float}. jaroslav@67: */ jaroslav@67: public Float(float value) { jaroslav@67: this.value = value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Constructs a newly allocated {@code Float} object that jaroslav@67: * represents the argument converted to type {@code float}. jaroslav@67: * jaroslav@67: * @param value the value to be represented by the {@code Float}. jaroslav@67: */ jaroslav@67: public Float(double value) { jaroslav@67: this.value = (float)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Constructs a newly allocated {@code Float} object that jaroslav@67: * represents the floating-point value of type {@code float} jaroslav@67: * represented by the string. The string is converted to a jaroslav@67: * {@code float} value as if by the {@code valueOf} method. jaroslav@67: * jaroslav@67: * @param s a string to be converted to a {@code Float}. jaroslav@67: * @throws NumberFormatException if the string does not contain a jaroslav@67: * parsable number. jaroslav@67: * @see java.lang.Float#valueOf(java.lang.String) jaroslav@67: */ jaroslav@67: public Float(String s) throws NumberFormatException { jaroslav@67: // REMIND: this is inefficient jaroslav@67: this(valueOf(s).floatValue()); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns {@code true} if this {@code Float} value is a jaroslav@67: * Not-a-Number (NaN), {@code false} otherwise. jaroslav@67: * jaroslav@67: * @return {@code true} if the value represented by this object is jaroslav@67: * NaN; {@code false} otherwise. jaroslav@67: */ jaroslav@67: public boolean isNaN() { jaroslav@67: return isNaN(value); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns {@code true} if this {@code Float} value is jaroslav@67: * infinitely large in magnitude, {@code false} otherwise. jaroslav@67: * jaroslav@67: * @return {@code true} if the value represented by this object is jaroslav@67: * positive infinity or negative infinity; jaroslav@67: * {@code false} otherwise. jaroslav@67: */ jaroslav@67: public boolean isInfinite() { jaroslav@67: return isInfinite(value); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a string representation of this {@code Float} object. jaroslav@67: * The primitive {@code float} value represented by this object jaroslav@67: * is converted to a {@code String} exactly as if by the method jaroslav@67: * {@code toString} of one argument. jaroslav@67: * jaroslav@67: * @return a {@code String} representation of this object. jaroslav@67: * @see java.lang.Float#toString(float) jaroslav@67: */ jaroslav@67: public String toString() { jaroslav@67: return Float.toString(value); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value of this {@code Float} as a {@code byte} (by jaroslav@67: * casting to a {@code byte}). jaroslav@67: * jaroslav@67: * @return the {@code float} value represented by this object jaroslav@67: * converted to type {@code byte} jaroslav@67: */ jaroslav@67: public byte byteValue() { jaroslav@67: return (byte)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value of this {@code Float} as a {@code short} (by jaroslav@67: * casting to a {@code short}). jaroslav@67: * jaroslav@67: * @return the {@code float} value represented by this object jaroslav@67: * converted to type {@code short} jaroslav@67: * @since JDK1.1 jaroslav@67: */ jaroslav@67: public short shortValue() { jaroslav@67: return (short)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value of this {@code Float} as an {@code int} (by jaroslav@67: * casting to type {@code int}). jaroslav@67: * jaroslav@67: * @return the {@code float} value represented by this object jaroslav@67: * converted to type {@code int} jaroslav@67: */ jaroslav@67: public int intValue() { jaroslav@67: return (int)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns value of this {@code Float} as a {@code long} (by jaroslav@67: * casting to type {@code long}). jaroslav@67: * jaroslav@67: * @return the {@code float} value represented by this object jaroslav@67: * converted to type {@code long} jaroslav@67: */ jaroslav@67: public long longValue() { jaroslav@67: return (long)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the {@code float} value of this {@code Float} object. jaroslav@67: * jaroslav@67: * @return the {@code float} value represented by this object jaroslav@67: */ jaroslav@67: public float floatValue() { jaroslav@67: return value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the {@code double} value of this {@code Float} object. jaroslav@67: * jaroslav@67: * @return the {@code float} value represented by this jaroslav@67: * object is converted to type {@code double} and the jaroslav@67: * result of the conversion is returned. jaroslav@67: */ jaroslav@67: public double doubleValue() { jaroslav@67: return (double)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a hash code for this {@code Float} object. The jaroslav@67: * result is the integer bit representation, exactly as produced jaroslav@67: * by the method {@link #floatToIntBits(float)}, of the primitive jaroslav@67: * {@code float} value represented by this {@code Float} jaroslav@67: * object. jaroslav@67: * jaroslav@67: * @return a hash code value for this object. jaroslav@67: */ jaroslav@67: public int hashCode() { jaroslav@67: return floatToIntBits(value); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: jaroslav@67: * Compares this object against the specified object. The result jaroslav@67: * is {@code true} if and only if the argument is not jaroslav@67: * {@code null} and is a {@code Float} object that jaroslav@67: * represents a {@code float} with the same value as the jaroslav@67: * {@code float} represented by this object. For this jaroslav@67: * purpose, two {@code float} values are considered to be the jaroslav@67: * same if and only if the method {@link #floatToIntBits(float)} jaroslav@67: * returns the identical {@code int} value when applied to jaroslav@67: * each. jaroslav@67: * jaroslav@67: *

Note that in most cases, for two instances of class jaroslav@67: * {@code Float}, {@code f1} and {@code f2}, the value jaroslav@67: * of {@code f1.equals(f2)} is {@code true} if and only if jaroslav@67: * jaroslav@67: *

jaroslav@67:      *   f1.floatValue() == f2.floatValue()
jaroslav@67:      * 
jaroslav@67: * jaroslav@67: *

also has the value {@code true}. However, there are two exceptions: jaroslav@67: *

jaroslav@67: * jaroslav@67: * This definition allows hash tables to operate properly. jaroslav@67: * jaroslav@67: * @param obj the object to be compared jaroslav@67: * @return {@code true} if the objects are the same; jaroslav@67: * {@code false} otherwise. jaroslav@67: * @see java.lang.Float#floatToIntBits(float) jaroslav@67: */ jaroslav@67: public boolean equals(Object obj) { jaroslav@67: return (obj instanceof Float) jaroslav@67: && (floatToIntBits(((Float)obj).value) == floatToIntBits(value)); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a representation of the specified floating-point value jaroslav@67: * according to the IEEE 754 floating-point "single format" bit jaroslav@67: * layout. jaroslav@67: * jaroslav@67: *

Bit 31 (the bit that is selected by the mask jaroslav@67: * {@code 0x80000000}) represents the sign of the floating-point jaroslav@67: * number. jaroslav@67: * Bits 30-23 (the bits that are selected by the mask jaroslav@67: * {@code 0x7f800000}) represent the exponent. jaroslav@67: * Bits 22-0 (the bits that are selected by the mask jaroslav@67: * {@code 0x007fffff}) represent the significand (sometimes called jaroslav@67: * the mantissa) of the floating-point number. jaroslav@67: * jaroslav@67: *

If the argument is positive infinity, the result is jaroslav@67: * {@code 0x7f800000}. jaroslav@67: * jaroslav@67: *

If the argument is negative infinity, the result is jaroslav@67: * {@code 0xff800000}. jaroslav@67: * jaroslav@67: *

If the argument is NaN, the result is {@code 0x7fc00000}. jaroslav@67: * jaroslav@67: *

In all cases, the result is an integer that, when given to the jaroslav@67: * {@link #intBitsToFloat(int)} method, will produce a floating-point jaroslav@67: * value the same as the argument to {@code floatToIntBits} jaroslav@67: * (except all NaN values are collapsed to a single jaroslav@67: * "canonical" NaN value). jaroslav@67: * jaroslav@67: * @param value a floating-point number. jaroslav@67: * @return the bits that represent the floating-point number. jaroslav@67: */ jaroslav@67: public static int floatToIntBits(float value) { jaroslav@84: throw new UnsupportedOperationException(); jaroslav@84: // int result = floatToRawIntBits(value); jaroslav@84: // // Check for NaN based on values of bit fields, maximum jaroslav@84: // // exponent and nonzero significand. jaroslav@84: // if ( ((result & FloatConsts.EXP_BIT_MASK) == jaroslav@84: // FloatConsts.EXP_BIT_MASK) && jaroslav@84: // (result & FloatConsts.SIGNIF_BIT_MASK) != 0) jaroslav@84: // result = 0x7fc00000; jaroslav@84: // return result; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a representation of the specified floating-point value jaroslav@67: * according to the IEEE 754 floating-point "single format" bit jaroslav@67: * layout, preserving Not-a-Number (NaN) values. jaroslav@67: * jaroslav@67: *

Bit 31 (the bit that is selected by the mask jaroslav@67: * {@code 0x80000000}) represents the sign of the floating-point jaroslav@67: * number. jaroslav@67: * Bits 30-23 (the bits that are selected by the mask jaroslav@67: * {@code 0x7f800000}) represent the exponent. jaroslav@67: * Bits 22-0 (the bits that are selected by the mask jaroslav@67: * {@code 0x007fffff}) represent the significand (sometimes called jaroslav@67: * the mantissa) of the floating-point number. jaroslav@67: * jaroslav@67: *

If the argument is positive infinity, the result is jaroslav@67: * {@code 0x7f800000}. jaroslav@67: * jaroslav@67: *

If the argument is negative infinity, the result is jaroslav@67: * {@code 0xff800000}. jaroslav@67: * jaroslav@67: *

If the argument is NaN, the result is the integer representing jaroslav@67: * the actual NaN value. Unlike the {@code floatToIntBits} jaroslav@67: * method, {@code floatToRawIntBits} does not collapse all the jaroslav@67: * bit patterns encoding a NaN to a single "canonical" jaroslav@67: * NaN value. jaroslav@67: * jaroslav@67: *

In all cases, the result is an integer that, when given to the jaroslav@67: * {@link #intBitsToFloat(int)} method, will produce a jaroslav@67: * floating-point value the same as the argument to jaroslav@67: * {@code floatToRawIntBits}. jaroslav@67: * jaroslav@67: * @param value a floating-point number. jaroslav@67: * @return the bits that represent the floating-point number. jaroslav@67: * @since 1.3 jaroslav@67: */ jaroslav@67: public static native int floatToRawIntBits(float value); jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the {@code float} value corresponding to a given jaroslav@67: * bit representation. jaroslav@67: * The argument is considered to be a representation of a jaroslav@67: * floating-point value according to the IEEE 754 floating-point jaroslav@67: * "single format" bit layout. jaroslav@67: * jaroslav@67: *

If the argument is {@code 0x7f800000}, the result is positive jaroslav@67: * infinity. jaroslav@67: * jaroslav@67: *

If the argument is {@code 0xff800000}, the result is negative jaroslav@67: * infinity. jaroslav@67: * jaroslav@67: *

If the argument is any value in the range jaroslav@67: * {@code 0x7f800001} through {@code 0x7fffffff} or in jaroslav@67: * the range {@code 0xff800001} through jaroslav@67: * {@code 0xffffffff}, the result is a NaN. No IEEE 754 jaroslav@67: * floating-point operation provided by Java can distinguish jaroslav@67: * between two NaN values of the same type with different bit jaroslav@67: * patterns. Distinct values of NaN are only distinguishable by jaroslav@67: * use of the {@code Float.floatToRawIntBits} method. jaroslav@67: * jaroslav@67: *

In all other cases, let s, e, and m be three jaroslav@67: * values that can be computed from the argument: jaroslav@67: * jaroslav@67: *

jaroslav@67:      * int s = ((bits >> 31) == 0) ? 1 : -1;
jaroslav@67:      * int e = ((bits >> 23) & 0xff);
jaroslav@67:      * int m = (e == 0) ?
jaroslav@67:      *                 (bits & 0x7fffff) << 1 :
jaroslav@67:      *                 (bits & 0x7fffff) | 0x800000;
jaroslav@67:      * 
jaroslav@67: * jaroslav@67: * Then the floating-point result equals the value of the mathematical jaroslav@67: * expression s·m·2e-150. jaroslav@67: * jaroslav@67: *

Note that this method may not be able to return a jaroslav@67: * {@code float} NaN with exactly same bit pattern as the jaroslav@67: * {@code int} argument. IEEE 754 distinguishes between two jaroslav@67: * kinds of NaNs, quiet NaNs and signaling NaNs. The jaroslav@67: * differences between the two kinds of NaN are generally not jaroslav@67: * visible in Java. Arithmetic operations on signaling NaNs turn jaroslav@67: * them into quiet NaNs with a different, but often similar, bit jaroslav@67: * pattern. However, on some processors merely copying a jaroslav@67: * signaling NaN also performs that conversion. In particular, jaroslav@67: * copying a signaling NaN to return it to the calling method may jaroslav@67: * perform this conversion. So {@code intBitsToFloat} may jaroslav@67: * not be able to return a {@code float} with a signaling NaN jaroslav@67: * bit pattern. Consequently, for some {@code int} values, jaroslav@67: * {@code floatToRawIntBits(intBitsToFloat(start))} may jaroslav@67: * not equal {@code start}. Moreover, which jaroslav@67: * particular bit patterns represent signaling NaNs is platform jaroslav@67: * dependent; although all NaN bit patterns, quiet or signaling, jaroslav@67: * must be in the NaN range identified above. jaroslav@67: * jaroslav@67: * @param bits an integer. jaroslav@67: * @return the {@code float} floating-point value with the same bit jaroslav@67: * pattern. jaroslav@67: */ jaroslav@181: @JavaScriptBody(args = "bits", jaroslav@181: body = lubomir@752: "var s = ((bits >> 31) == 0) ? 1 : -1;\n" jaroslav@181: + "var e = ((bits >> 23) & 0xff);\n" lubomir@752: + "if (e === 0xff) {\n" lubomir@752: + " if ((bits & 0x7fffff) === 0) {\n" lubomir@752: + " return (s > 0) ? Number.POSITIVE_INFINITY" lubomir@752: + " : Number.NEGATIVE_INFINITY;\n" lubomir@752: + " }\n" lubomir@752: + " return Number.NaN;\n" lubomir@752: + "}\n" jaroslav@181: + "var m = (e == 0) ?\n" jaroslav@181: + " (bits & 0x7fffff) << 1 :\n" jaroslav@181: + " (bits & 0x7fffff) | 0x800000;\n" jaroslav@181: + "return s * m * Math.pow(2.0, e - 150);\n" jaroslav@181: ) jaroslav@67: public static native float intBitsToFloat(int bits); jaroslav@67: jaroslav@67: /** jaroslav@67: * Compares two {@code Float} objects numerically. There are jaroslav@67: * two ways in which comparisons performed by this method differ jaroslav@67: * from those performed by the Java language numerical comparison jaroslav@67: * operators ({@code <, <=, ==, >=, >}) when jaroslav@67: * applied to primitive {@code float} values: jaroslav@67: * jaroslav@67: *

jaroslav@67: * jaroslav@67: * This ensures that the natural ordering of {@code Float} jaroslav@67: * objects imposed by this method is consistent with equals. jaroslav@67: * jaroslav@67: * @param anotherFloat the {@code Float} to be compared. jaroslav@67: * @return the value {@code 0} if {@code anotherFloat} is jaroslav@67: * numerically equal to this {@code Float}; a value jaroslav@67: * less than {@code 0} if this {@code Float} jaroslav@67: * is numerically less than {@code anotherFloat}; jaroslav@67: * and a value greater than {@code 0} if this jaroslav@67: * {@code Float} is numerically greater than jaroslav@67: * {@code anotherFloat}. jaroslav@67: * jaroslav@67: * @since 1.2 jaroslav@67: * @see Comparable#compareTo(Object) jaroslav@67: */ jaroslav@67: public int compareTo(Float anotherFloat) { jaroslav@67: return Float.compare(value, anotherFloat.value); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Compares the two specified {@code float} values. The sign jaroslav@67: * of the integer value returned is the same as that of the jaroslav@67: * integer that would be returned by the call: jaroslav@67: *
jaroslav@67:      *    new Float(f1).compareTo(new Float(f2))
jaroslav@67:      * 
jaroslav@67: * jaroslav@67: * @param f1 the first {@code float} to compare. jaroslav@67: * @param f2 the second {@code float} to compare. jaroslav@67: * @return the value {@code 0} if {@code f1} is jaroslav@67: * numerically equal to {@code f2}; a value less than jaroslav@67: * {@code 0} if {@code f1} is numerically less than jaroslav@67: * {@code f2}; and a value greater than {@code 0} jaroslav@67: * if {@code f1} is numerically greater than jaroslav@67: * {@code f2}. jaroslav@67: * @since 1.4 jaroslav@67: */ jaroslav@67: public static int compare(float f1, float f2) { jaroslav@67: if (f1 < f2) jaroslav@67: return -1; // Neither val is NaN, thisVal is smaller jaroslav@67: if (f1 > f2) jaroslav@67: return 1; // Neither val is NaN, thisVal is larger jaroslav@67: jaroslav@67: // Cannot use floatToRawIntBits because of possibility of NaNs. jaroslav@67: int thisBits = Float.floatToIntBits(f1); jaroslav@67: int anotherBits = Float.floatToIntBits(f2); jaroslav@67: jaroslav@67: return (thisBits == anotherBits ? 0 : // Values are equal jaroslav@67: (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN) jaroslav@67: 1)); // (0.0, -0.0) or (NaN, !NaN) jaroslav@67: } jaroslav@67: jaroslav@67: /** use serialVersionUID from JDK 1.0.2 for interoperability */ jaroslav@67: private static final long serialVersionUID = -2671257302660747028L; jaroslav@67: }