0N/A/*
3202N/A * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.lang;
0N/A
0N/Aimport sun.misc.FloatingDecimal;
0N/Aimport sun.misc.FpUtils;
0N/Aimport sun.misc.FloatConsts;
0N/Aimport sun.misc.DoubleConsts;
0N/A
0N/A/**
0N/A * The {@code Float} class wraps a value of primitive type
0N/A * {@code float} in an object. An object of type
0N/A * {@code Float} contains a single field whose type is
0N/A * {@code float}.
0N/A *
0N/A * <p>In addition, this class provides several methods for converting a
0N/A * {@code float} to a {@code String} and a
0N/A * {@code String} to a {@code float}, as well as other
0N/A * constants and methods useful when dealing with a
0N/A * {@code float}.
0N/A *
0N/A * @author Lee Boynton
0N/A * @author Arthur van Hoff
0N/A * @author Joseph D. Darcy
0N/A * @since JDK1.0
0N/A */
0N/Apublic final class Float extends Number implements Comparable<Float> {
0N/A /**
0N/A * A constant holding the positive infinity of type
0N/A * {@code float}. It is equal to the value returned by
0N/A * {@code Float.intBitsToFloat(0x7f800000)}.
0N/A */
0N/A public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
0N/A
0N/A /**
0N/A * A constant holding the negative infinity of type
0N/A * {@code float}. It is equal to the value returned by
0N/A * {@code Float.intBitsToFloat(0xff800000)}.
0N/A */
0N/A public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
0N/A
0N/A /**
0N/A * A constant holding a Not-a-Number (NaN) value of type
0N/A * {@code float}. It is equivalent to the value returned by
0N/A * {@code Float.intBitsToFloat(0x7fc00000)}.
0N/A */
0N/A public static final float NaN = 0.0f / 0.0f;
0N/A
0N/A /**
0N/A * A constant holding the largest positive finite value of type
0N/A * {@code float}, (2-2<sup>-23</sup>)&middot;2<sup>127</sup>.
0N/A * It is equal to the hexadecimal floating-point literal
0N/A * {@code 0x1.fffffeP+127f} and also equal to
0N/A * {@code Float.intBitsToFloat(0x7f7fffff)}.
0N/A */
0N/A public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f
0N/A
0N/A /**
0N/A * A constant holding the smallest positive normal value of type
0N/A * {@code float}, 2<sup>-126</sup>. It is equal to the
0N/A * hexadecimal floating-point literal {@code 0x1.0p-126f} and also
0N/A * equal to {@code Float.intBitsToFloat(0x00800000)}.
0N/A *
0N/A * @since 1.6
0N/A */
0N/A public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f
0N/A
0N/A /**
0N/A * A constant holding the smallest positive nonzero value of type
0N/A * {@code float}, 2<sup>-149</sup>. It is equal to the
0N/A * hexadecimal floating-point literal {@code 0x0.000002P-126f}
0N/A * and also equal to {@code Float.intBitsToFloat(0x1)}.
0N/A */
0N/A public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f
0N/A
0N/A /**
0N/A * Maximum exponent a finite {@code float} variable may have. It
0N/A * is equal to the value returned by {@code
0N/A * Math.getExponent(Float.MAX_VALUE)}.
0N/A *
0N/A * @since 1.6
0N/A */
0N/A public static final int MAX_EXPONENT = 127;
0N/A
0N/A /**
0N/A * Minimum exponent a normalized {@code float} variable may have.
0N/A * It is equal to the value returned by {@code
0N/A * Math.getExponent(Float.MIN_NORMAL)}.
0N/A *
0N/A * @since 1.6
0N/A */
0N/A public static final int MIN_EXPONENT = -126;
0N/A
0N/A /**
0N/A * The number of bits used to represent a {@code float} value.
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public static final int SIZE = 32;
0N/A
0N/A /**
0N/A * The {@code Class} instance representing the primitive type
0N/A * {@code float}.
0N/A *
0N/A * @since JDK1.1
0N/A */
0N/A public static final Class<Float> TYPE = Class.getPrimitiveClass("float");
0N/A
0N/A /**
0N/A * Returns a string representation of the {@code float}
0N/A * argument. All characters mentioned below are ASCII characters.
0N/A * <ul>
0N/A * <li>If the argument is NaN, the result is the string
0N/A * "{@code NaN}".
0N/A * <li>Otherwise, the result is a string that represents the sign and
0N/A * magnitude (absolute value) of the argument. If the sign is
0N/A * negative, the first character of the result is
0N/A * '{@code -}' (<code>'&#92;u002D'</code>); if the sign is
0N/A * positive, no sign character appears in the result. As for
0N/A * the magnitude <i>m</i>:
0N/A * <ul>
0N/A * <li>If <i>m</i> is infinity, it is represented by the characters
0N/A * {@code "Infinity"}; thus, positive infinity produces
0N/A * the result {@code "Infinity"} and negative infinity
0N/A * produces the result {@code "-Infinity"}.
0N/A * <li>If <i>m</i> is zero, it is represented by the characters
0N/A * {@code "0.0"}; thus, negative zero produces the result
0N/A * {@code "-0.0"} and positive zero produces the result
0N/A * {@code "0.0"}.
0N/A * <li> If <i>m</i> is greater than or equal to 10<sup>-3</sup> but
0N/A * less than 10<sup>7</sup>, then it is represented as the
0N/A * integer part of <i>m</i>, in decimal form with no leading
0N/A * zeroes, followed by '{@code .}'
0N/A * (<code>'&#92;u002E'</code>), followed by one or more
0N/A * decimal digits representing the fractional part of
0N/A * <i>m</i>.
0N/A * <li> If <i>m</i> is less than 10<sup>-3</sup> or greater than or
0N/A * equal to 10<sup>7</sup>, then it is represented in
0N/A * so-called "computerized scientific notation." Let <i>n</i>
0N/A * be the unique integer such that 10<sup><i>n</i> </sup>&le;
0N/A * <i>m</i> {@literal <} 10<sup><i>n</i>+1</sup>; then let <i>a</i>
0N/A * be the mathematically exact quotient of <i>m</i> and
0N/A * 10<sup><i>n</i></sup> so that 1 &le; <i>a</i> {@literal <} 10.
0N/A * The magnitude is then represented as the integer part of
0N/A * <i>a</i>, as a single decimal digit, followed by
0N/A * '{@code .}' (<code>'&#92;u002E'</code>), followed by
0N/A * decimal digits representing the fractional part of
0N/A * <i>a</i>, followed by the letter '{@code E}'
0N/A * (<code>'&#92;u0045'</code>), followed by a representation
0N/A * of <i>n</i> as a decimal integer, as produced by the
0N/A * method {@link java.lang.Integer#toString(int)}.
0N/A *
0N/A * </ul>
0N/A * </ul>
0N/A * How many digits must be printed for the fractional part of
0N/A * <i>m</i> or <i>a</i>? There must be at least one digit
0N/A * to represent the fractional part, and beyond that as many, but
0N/A * only as many, more digits as are needed to uniquely distinguish
0N/A * the argument value from adjacent values of type
0N/A * {@code float}. That is, suppose that <i>x</i> is the
0N/A * exact mathematical value represented by the decimal
0N/A * representation produced by this method for a finite nonzero
0N/A * argument <i>f</i>. Then <i>f</i> must be the {@code float}
0N/A * value nearest to <i>x</i>; or, if two {@code float} values are
0N/A * equally close to <i>x</i>, then <i>f</i> must be one of
0N/A * them and the least significant bit of the significand of
0N/A * <i>f</i> must be {@code 0}.
0N/A *
0N/A * <p>To create localized string representations of a floating-point
0N/A * value, use subclasses of {@link java.text.NumberFormat}.
0N/A *
0N/A * @param f the float to be converted.
0N/A * @return a string representation of the argument.
0N/A */
0N/A public static String toString(float f) {
0N/A return new FloatingDecimal(f).toJavaFormatString();
0N/A }
0N/A
0N/A /**
0N/A * Returns a hexadecimal string representation of the
0N/A * {@code float} argument. All characters mentioned below are
0N/A * ASCII characters.
0N/A *
0N/A * <ul>
0N/A * <li>If the argument is NaN, the result is the string
0N/A * "{@code NaN}".
0N/A * <li>Otherwise, the result is a string that represents the sign and
0N/A * magnitude (absolute value) of the argument. If the sign is negative,
0N/A * the first character of the result is '{@code -}'
0N/A * (<code>'&#92;u002D'</code>); if the sign is positive, no sign character
0N/A * appears in the result. As for the magnitude <i>m</i>:
0N/A *
0N/A * <ul>
0N/A * <li>If <i>m</i> is infinity, it is represented by the string
0N/A * {@code "Infinity"}; thus, positive infinity produces the
0N/A * result {@code "Infinity"} and negative infinity produces
0N/A * the result {@code "-Infinity"}.
0N/A *
0N/A * <li>If <i>m</i> is zero, it is represented by the string
0N/A * {@code "0x0.0p0"}; thus, negative zero produces the result
0N/A * {@code "-0x0.0p0"} and positive zero produces the result
0N/A * {@code "0x0.0p0"}.
0N/A *
0N/A * <li>If <i>m</i> is a {@code float} value with a
0N/A * normalized representation, substrings are used to represent the
0N/A * significand and exponent fields. The significand is
0N/A * represented by the characters {@code "0x1."}
0N/A * followed by a lowercase hexadecimal representation of the rest
0N/A * of the significand as a fraction. Trailing zeros in the
0N/A * hexadecimal representation are removed unless all the digits
0N/A * are zero, in which case a single zero is used. Next, the
0N/A * exponent is represented by {@code "p"} followed
0N/A * by a decimal string of the unbiased exponent as if produced by
0N/A * a call to {@link Integer#toString(int) Integer.toString} on the
0N/A * exponent value.
0N/A *
0N/A * <li>If <i>m</i> is a {@code float} value with a subnormal
0N/A * representation, the significand is represented by the
0N/A * characters {@code "0x0."} followed by a
0N/A * hexadecimal representation of the rest of the significand as a
0N/A * fraction. Trailing zeros in the hexadecimal representation are
0N/A * removed. Next, the exponent is represented by
0N/A * {@code "p-126"}. Note that there must be at
0N/A * least one nonzero digit in a subnormal significand.
0N/A *
0N/A * </ul>
0N/A *
0N/A * </ul>
0N/A *
0N/A * <table border>
0N/A * <caption><h3>Examples</h3></caption>
0N/A * <tr><th>Floating-point Value</th><th>Hexadecimal String</th>
0N/A * <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td>
0N/A * <tr><td>{@code -1.0}</td> <td>{@code -0x1.0p0}</td>
0N/A * <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>
0N/A * <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>
0N/A * <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>
0N/A * <tr><td>{@code 0.25}</td> <td>{@code 0x1.0p-2}</td>
0N/A * <tr><td>{@code Float.MAX_VALUE}</td>
0N/A * <td>{@code 0x1.fffffep127}</td>
0N/A * <tr><td>{@code Minimum Normal Value}</td>
0N/A * <td>{@code 0x1.0p-126}</td>
0N/A * <tr><td>{@code Maximum Subnormal Value}</td>
0N/A * <td>{@code 0x0.fffffep-126}</td>
0N/A * <tr><td>{@code Float.MIN_VALUE}</td>
0N/A * <td>{@code 0x0.000002p-126}</td>
0N/A * </table>
0N/A * @param f the {@code float} to be converted.
0N/A * @return a hex string representation of the argument.
0N/A * @since 1.5
0N/A * @author Joseph D. Darcy
0N/A */
0N/A public static String toHexString(float f) {
0N/A if (Math.abs(f) < FloatConsts.MIN_NORMAL
0N/A && f != 0.0f ) {// float subnormal
0N/A // Adjust exponent to create subnormal double, then
0N/A // replace subnormal double exponent with subnormal float
0N/A // exponent
0N/A String s = Double.toHexString(FpUtils.scalb((double)f,
0N/A /* -1022+126 */
0N/A DoubleConsts.MIN_EXPONENT-
0N/A FloatConsts.MIN_EXPONENT));
0N/A return s.replaceFirst("p-1022$", "p-126");
0N/A }
0N/A else // double string will be the same as float string
0N/A return Double.toHexString(f);
0N/A }
0N/A
0N/A /**
0N/A * Returns a {@code Float} object holding the
0N/A * {@code float} value represented by the argument string
0N/A * {@code s}.
0N/A *
0N/A * <p>If {@code s} is {@code null}, then a
0N/A * {@code NullPointerException} is thrown.
0N/A *
0N/A * <p>Leading and trailing whitespace characters in {@code s}
0N/A * are ignored. Whitespace is removed as if by the {@link
0N/A * String#trim} method; that is, both ASCII space and control
0N/A * characters are removed. The rest of {@code s} should
0N/A * constitute a <i>FloatValue</i> as described by the lexical
0N/A * syntax rules:
0N/A *
0N/A * <blockquote>
0N/A * <dl>
0N/A * <dt><i>FloatValue:</i>
0N/A * <dd><i>Sign<sub>opt</sub></i> {@code NaN}
0N/A * <dd><i>Sign<sub>opt</sub></i> {@code Infinity}
0N/A * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
0N/A * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
0N/A * <dd><i>SignedInteger</i>
0N/A * </dl>
0N/A *
0N/A * <p>
0N/A *
0N/A * <dl>
0N/A * <dt><i>HexFloatingPointLiteral</i>:
0N/A * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
0N/A * </dl>
0N/A *
0N/A * <p>
0N/A *
0N/A * <dl>
0N/A * <dt><i>HexSignificand:</i>
0N/A * <dd><i>HexNumeral</i>
0N/A * <dd><i>HexNumeral</i> {@code .}
0N/A * <dd>{@code 0x} <i>HexDigits<sub>opt</sub>
0N/A * </i>{@code .}<i> HexDigits</i>
0N/A * <dd>{@code 0X}<i> HexDigits<sub>opt</sub>
0N/A * </i>{@code .} <i>HexDigits</i>
0N/A * </dl>
0N/A *
0N/A * <p>
0N/A *
0N/A * <dl>
0N/A * <dt><i>BinaryExponent:</i>
0N/A * <dd><i>BinaryExponentIndicator SignedInteger</i>
0N/A * </dl>
0N/A *
0N/A * <p>
0N/A *
0N/A * <dl>
0N/A * <dt><i>BinaryExponentIndicator:</i>
0N/A * <dd>{@code p}
0N/A * <dd>{@code P}
0N/A * </dl>
0N/A *
0N/A * </blockquote>
0N/A *
0N/A * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
0N/A * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
0N/A * <i>FloatTypeSuffix</i> are as defined in the lexical structure
4008N/A * sections of
4008N/A * <cite>The Java&trade; Language Specification</cite>,
4008N/A * except that underscores are not accepted between digits.
4008N/A * If {@code s} does not have the form of
0N/A * a <i>FloatValue</i>, then a {@code NumberFormatException}
0N/A * is thrown. Otherwise, {@code s} is regarded as
0N/A * representing an exact decimal value in the usual
0N/A * "computerized scientific notation" or as an exact
0N/A * hexadecimal value; this exact numerical value is then
0N/A * conceptually converted to an "infinitely precise"
0N/A * binary value that is then rounded to type {@code float}
0N/A * by the usual round-to-nearest rule of IEEE 754 floating-point
0N/A * arithmetic, which includes preserving the sign of a zero
807N/A * value.
807N/A *
807N/A * Note that the round-to-nearest rule also implies overflow and
807N/A * underflow behaviour; if the exact value of {@code s} is large
807N/A * enough in magnitude (greater than or equal to ({@link
807N/A * #MAX_VALUE} + {@link Math#ulp(float) ulp(MAX_VALUE)}/2),
807N/A * rounding to {@code float} will result in an infinity and if the
807N/A * exact value of {@code s} is small enough in magnitude (less
807N/A * than or equal to {@link #MIN_VALUE}/2), rounding to float will
807N/A * result in a zero.
807N/A *
807N/A * Finally, after rounding a {@code Float} object representing
807N/A * this {@code float} value is returned.
0N/A *
0N/A * <p>To interpret localized string representations of a
0N/A * floating-point value, use subclasses of {@link
0N/A * java.text.NumberFormat}.
0N/A *
0N/A * <p>Note that trailing format specifiers, specifiers that
0N/A * determine the type of a floating-point literal
0N/A * ({@code 1.0f} is a {@code float} value;
0N/A * {@code 1.0d} is a {@code double} value), do
0N/A * <em>not</em> influence the results of this method. In other
0N/A * words, the numerical value of the input string is converted
0N/A * directly to the target floating-point type. In general, the
0N/A * two-step sequence of conversions, string to {@code double}
0N/A * followed by {@code double} to {@code float}, is
0N/A * <em>not</em> equivalent to converting a string directly to
0N/A * {@code float}. For example, if first converted to an
0N/A * intermediate {@code double} and then to
0N/A * {@code float}, the string<br>
0N/A * {@code "1.00000017881393421514957253748434595763683319091796875001d"}<br>
0N/A * results in the {@code float} value
0N/A * {@code 1.0000002f}; if the string is converted directly to
0N/A * {@code float}, <code>1.000000<b>1</b>f</code> results.
0N/A *
0N/A * <p>To avoid calling this method on an invalid string and having
0N/A * a {@code NumberFormatException} be thrown, the documentation
0N/A * for {@link Double#valueOf Double.valueOf} lists a regular
0N/A * expression which can be used to screen the input.
0N/A *
0N/A * @param s the string to be parsed.
0N/A * @return a {@code Float} object holding the value
0N/A * represented by the {@code String} argument.
0N/A * @throws NumberFormatException if the string does not contain a
0N/A * parsable number.
0N/A */
0N/A public static Float valueOf(String s) throws NumberFormatException {
0N/A return new Float(FloatingDecimal.readJavaFormatString(s).floatValue());
0N/A }
0N/A
0N/A /**
0N/A * Returns a {@code Float} instance representing the specified
0N/A * {@code float} value.
0N/A * If a new {@code Float} instance is not required, this method
0N/A * should generally be used in preference to the constructor
0N/A * {@link #Float(float)}, as this method is likely to yield
0N/A * significantly better space and time performance by caching
0N/A * frequently requested values.
0N/A *
0N/A * @param f a float value.
0N/A * @return a {@code Float} instance representing {@code f}.
0N/A * @since 1.5
0N/A */
0N/A public static Float valueOf(float f) {
0N/A return new Float(f);
0N/A }
0N/A
0N/A /**
0N/A * Returns a new {@code float} initialized to the value
0N/A * represented by the specified {@code String}, as performed
0N/A * by the {@code valueOf} method of class {@code Float}.
0N/A *
1419N/A * @param s the string to be parsed.
0N/A * @return the {@code float} value represented by the string
0N/A * argument.
1419N/A * @throws NullPointerException if the string is null
1419N/A * @throws NumberFormatException if the string does not contain a
0N/A * parsable {@code float}.
1419N/A * @see java.lang.Float#valueOf(String)
0N/A * @since 1.2
0N/A */
0N/A public static float parseFloat(String s) throws NumberFormatException {
0N/A return FloatingDecimal.readJavaFormatString(s).floatValue();
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if the specified number is a
0N/A * Not-a-Number (NaN) value, {@code false} otherwise.
0N/A *
0N/A * @param v the value to be tested.
0N/A * @return {@code true} if the argument is NaN;
0N/A * {@code false} otherwise.
0N/A */
0N/A static public boolean isNaN(float v) {
0N/A return (v != v);
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if the specified number is infinitely
0N/A * large in magnitude, {@code false} otherwise.
0N/A *
0N/A * @param v the value to be tested.
0N/A * @return {@code true} if the argument is positive infinity or
0N/A * negative infinity; {@code false} otherwise.
0N/A */
0N/A static public boolean isInfinite(float v) {
0N/A return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
0N/A }
0N/A
0N/A /**
0N/A * The value of the Float.
0N/A *
0N/A * @serial
0N/A */
0N/A private final float value;
0N/A
0N/A /**
0N/A * Constructs a newly allocated {@code Float} object that
0N/A * represents the primitive {@code float} argument.
0N/A *
0N/A * @param value the value to be represented by the {@code Float}.
0N/A */
0N/A public Float(float value) {
0N/A this.value = value;
0N/A }
0N/A
0N/A /**
0N/A * Constructs a newly allocated {@code Float} object that
0N/A * represents the argument converted to type {@code float}.
0N/A *
0N/A * @param value the value to be represented by the {@code Float}.
0N/A */
0N/A public Float(double value) {
0N/A this.value = (float)value;
0N/A }
0N/A
0N/A /**
0N/A * Constructs a newly allocated {@code Float} object that
0N/A * represents the floating-point value of type {@code float}
0N/A * represented by the string. The string is converted to a
0N/A * {@code float} value as if by the {@code valueOf} method.
0N/A *
0N/A * @param s a string to be converted to a {@code Float}.
0N/A * @throws NumberFormatException if the string does not contain a
0N/A * parsable number.
0N/A * @see java.lang.Float#valueOf(java.lang.String)
0N/A */
0N/A public Float(String s) throws NumberFormatException {
0N/A // REMIND: this is inefficient
0N/A this(valueOf(s).floatValue());
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if this {@code Float} value is a
0N/A * Not-a-Number (NaN), {@code false} otherwise.
0N/A *
0N/A * @return {@code true} if the value represented by this object is
0N/A * NaN; {@code false} otherwise.
0N/A */
0N/A public boolean isNaN() {
0N/A return isNaN(value);
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if this {@code Float} value is
0N/A * infinitely large in magnitude, {@code false} otherwise.
0N/A *
0N/A * @return {@code true} if the value represented by this object is
0N/A * positive infinity or negative infinity;
0N/A * {@code false} otherwise.
0N/A */
0N/A public boolean isInfinite() {
0N/A return isInfinite(value);
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of this {@code Float} object.
0N/A * The primitive {@code float} value represented by this object
0N/A * is converted to a {@code String} exactly as if by the method
0N/A * {@code toString} of one argument.
0N/A *
0N/A * @return a {@code String} representation of this object.
0N/A * @see java.lang.Float#toString(float)
0N/A */
0N/A public String toString() {
1722N/A return Float.toString(value);
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Float} as a {@code byte} (by
0N/A * casting to a {@code byte}).
0N/A *
0N/A * @return the {@code float} value represented by this object
0N/A * converted to type {@code byte}
0N/A */
0N/A public byte byteValue() {
0N/A return (byte)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Float} as a {@code short} (by
0N/A * casting to a {@code short}).
0N/A *
0N/A * @return the {@code float} value represented by this object
0N/A * converted to type {@code short}
0N/A * @since JDK1.1
0N/A */
0N/A public short shortValue() {
0N/A return (short)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Float} as an {@code int} (by
0N/A * casting to type {@code int}).
0N/A *
0N/A * @return the {@code float} value represented by this object
0N/A * converted to type {@code int}
0N/A */
0N/A public int intValue() {
0N/A return (int)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns value of this {@code Float} as a {@code long} (by
0N/A * casting to type {@code long}).
0N/A *
0N/A * @return the {@code float} value represented by this object
0N/A * converted to type {@code long}
0N/A */
0N/A public long longValue() {
0N/A return (long)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the {@code float} value of this {@code Float} object.
0N/A *
0N/A * @return the {@code float} value represented by this object
0N/A */
0N/A public float floatValue() {
0N/A return value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the {@code double} value of this {@code Float} object.
0N/A *
0N/A * @return the {@code float} value represented by this
0N/A * object is converted to type {@code double} and the
0N/A * result of the conversion is returned.
0N/A */
0N/A public double doubleValue() {
0N/A return (double)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns a hash code for this {@code Float} object. The
0N/A * result is the integer bit representation, exactly as produced
0N/A * by the method {@link #floatToIntBits(float)}, of the primitive
0N/A * {@code float} value represented by this {@code Float}
0N/A * object.
0N/A *
0N/A * @return a hash code value for this object.
0N/A */
0N/A public int hashCode() {
0N/A return floatToIntBits(value);
0N/A }
0N/A
0N/A /**
0N/A
0N/A * Compares this object against the specified object. The result
0N/A * is {@code true} if and only if the argument is not
0N/A * {@code null} and is a {@code Float} object that
0N/A * represents a {@code float} with the same value as the
0N/A * {@code float} represented by this object. For this
0N/A * purpose, two {@code float} values are considered to be the
0N/A * same if and only if the method {@link #floatToIntBits(float)}
0N/A * returns the identical {@code int} value when applied to
0N/A * each.
0N/A *
0N/A * <p>Note that in most cases, for two instances of class
0N/A * {@code Float}, {@code f1} and {@code f2}, the value
0N/A * of {@code f1.equals(f2)} is {@code true} if and only if
0N/A *
0N/A * <blockquote><pre>
0N/A * f1.floatValue() == f2.floatValue()
0N/A * </pre></blockquote>
0N/A *
0N/A * <p>also has the value {@code true}. However, there are two exceptions:
0N/A * <ul>
0N/A * <li>If {@code f1} and {@code f2} both represent
0N/A * {@code Float.NaN}, then the {@code equals} method returns
0N/A * {@code true}, even though {@code Float.NaN==Float.NaN}
0N/A * has the value {@code false}.
0N/A * <li>If {@code f1} represents {@code +0.0f} while
0N/A * {@code f2} represents {@code -0.0f}, or vice
0N/A * versa, the {@code equal} test has the value
0N/A * {@code false}, even though {@code 0.0f==-0.0f}
0N/A * has the value {@code true}.
0N/A * </ul>
0N/A *
0N/A * This definition allows hash tables to operate properly.
0N/A *
0N/A * @param obj the object to be compared
0N/A * @return {@code true} if the objects are the same;
0N/A * {@code false} otherwise.
0N/A * @see java.lang.Float#floatToIntBits(float)
0N/A */
0N/A public boolean equals(Object obj) {
0N/A return (obj instanceof Float)
0N/A && (floatToIntBits(((Float)obj).value) == floatToIntBits(value));
0N/A }
0N/A
0N/A /**
0N/A * Returns a representation of the specified floating-point value
0N/A * according to the IEEE 754 floating-point "single format" bit
0N/A * layout.
0N/A *
0N/A * <p>Bit 31 (the bit that is selected by the mask
0N/A * {@code 0x80000000}) represents the sign of the floating-point
0N/A * number.
0N/A * Bits 30-23 (the bits that are selected by the mask
0N/A * {@code 0x7f800000}) represent the exponent.
0N/A * Bits 22-0 (the bits that are selected by the mask
0N/A * {@code 0x007fffff}) represent the significand (sometimes called
0N/A * the mantissa) of the floating-point number.
0N/A *
0N/A * <p>If the argument is positive infinity, the result is
0N/A * {@code 0x7f800000}.
0N/A *
0N/A * <p>If the argument is negative infinity, the result is
0N/A * {@code 0xff800000}.
0N/A *
0N/A * <p>If the argument is NaN, the result is {@code 0x7fc00000}.
0N/A *
0N/A * <p>In all cases, the result is an integer that, when given to the
0N/A * {@link #intBitsToFloat(int)} method, will produce a floating-point
0N/A * value the same as the argument to {@code floatToIntBits}
0N/A * (except all NaN values are collapsed to a single
0N/A * "canonical" NaN value).
0N/A *
0N/A * @param value a floating-point number.
0N/A * @return the bits that represent the floating-point number.
0N/A */
0N/A public static int floatToIntBits(float value) {
0N/A int result = floatToRawIntBits(value);
0N/A // Check for NaN based on values of bit fields, maximum
0N/A // exponent and nonzero significand.
0N/A if ( ((result & FloatConsts.EXP_BIT_MASK) ==
0N/A FloatConsts.EXP_BIT_MASK) &&
0N/A (result & FloatConsts.SIGNIF_BIT_MASK) != 0)
0N/A result = 0x7fc00000;
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Returns a representation of the specified floating-point value
0N/A * according to the IEEE 754 floating-point "single format" bit
0N/A * layout, preserving Not-a-Number (NaN) values.
0N/A *
0N/A * <p>Bit 31 (the bit that is selected by the mask
0N/A * {@code 0x80000000}) represents the sign of the floating-point
0N/A * number.
0N/A * Bits 30-23 (the bits that are selected by the mask
0N/A * {@code 0x7f800000}) represent the exponent.
0N/A * Bits 22-0 (the bits that are selected by the mask
0N/A * {@code 0x007fffff}) represent the significand (sometimes called
0N/A * the mantissa) of the floating-point number.
0N/A *
0N/A * <p>If the argument is positive infinity, the result is
0N/A * {@code 0x7f800000}.
0N/A *
0N/A * <p>If the argument is negative infinity, the result is
0N/A * {@code 0xff800000}.
0N/A *
0N/A * <p>If the argument is NaN, the result is the integer representing
0N/A * the actual NaN value. Unlike the {@code floatToIntBits}
0N/A * method, {@code floatToRawIntBits} does not collapse all the
0N/A * bit patterns encoding a NaN to a single "canonical"
0N/A * NaN value.
0N/A *
0N/A * <p>In all cases, the result is an integer that, when given to the
0N/A * {@link #intBitsToFloat(int)} method, will produce a
0N/A * floating-point value the same as the argument to
0N/A * {@code floatToRawIntBits}.
0N/A *
0N/A * @param value a floating-point number.
0N/A * @return the bits that represent the floating-point number.
0N/A * @since 1.3
0N/A */
0N/A public static native int floatToRawIntBits(float value);
0N/A
0N/A /**
0N/A * Returns the {@code float} value corresponding to a given
0N/A * bit representation.
0N/A * The argument is considered to be a representation of a
0N/A * floating-point value according to the IEEE 754 floating-point
0N/A * "single format" bit layout.
0N/A *
0N/A * <p>If the argument is {@code 0x7f800000}, the result is positive
0N/A * infinity.
0N/A *
0N/A * <p>If the argument is {@code 0xff800000}, the result is negative
0N/A * infinity.
0N/A *
0N/A * <p>If the argument is any value in the range
0N/A * {@code 0x7f800001} through {@code 0x7fffffff} or in
0N/A * the range {@code 0xff800001} through
0N/A * {@code 0xffffffff}, the result is a NaN. No IEEE 754
0N/A * floating-point operation provided by Java can distinguish
0N/A * between two NaN values of the same type with different bit
0N/A * patterns. Distinct values of NaN are only distinguishable by
0N/A * use of the {@code Float.floatToRawIntBits} method.
0N/A *
0N/A * <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
0N/A * values that can be computed from the argument:
0N/A *
0N/A * <blockquote><pre>
0N/A * int s = ((bits &gt;&gt; 31) == 0) ? 1 : -1;
0N/A * int e = ((bits &gt;&gt; 23) & 0xff);
0N/A * int m = (e == 0) ?
0N/A * (bits & 0x7fffff) &lt;&lt; 1 :
0N/A * (bits & 0x7fffff) | 0x800000;
0N/A * </pre></blockquote>
0N/A *
0N/A * Then the floating-point result equals the value of the mathematical
0N/A * expression <i>s</i>&middot;<i>m</i>&middot;2<sup><i>e</i>-150</sup>.
0N/A *
0N/A * <p>Note that this method may not be able to return a
0N/A * {@code float} NaN with exactly same bit pattern as the
0N/A * {@code int} argument. IEEE 754 distinguishes between two
0N/A * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The
0N/A * differences between the two kinds of NaN are generally not
0N/A * visible in Java. Arithmetic operations on signaling NaNs turn
0N/A * them into quiet NaNs with a different, but often similar, bit
0N/A * pattern. However, on some processors merely copying a
0N/A * signaling NaN also performs that conversion. In particular,
0N/A * copying a signaling NaN to return it to the calling method may
0N/A * perform this conversion. So {@code intBitsToFloat} may
0N/A * not be able to return a {@code float} with a signaling NaN
0N/A * bit pattern. Consequently, for some {@code int} values,
0N/A * {@code floatToRawIntBits(intBitsToFloat(start))} may
0N/A * <i>not</i> equal {@code start}. Moreover, which
0N/A * particular bit patterns represent signaling NaNs is platform
0N/A * dependent; although all NaN bit patterns, quiet or signaling,
0N/A * must be in the NaN range identified above.
0N/A *
0N/A * @param bits an integer.
0N/A * @return the {@code float} floating-point value with the same bit
0N/A * pattern.
0N/A */
0N/A public static native float intBitsToFloat(int bits);
0N/A
0N/A /**
0N/A * Compares two {@code Float} objects numerically. There are
0N/A * two ways in which comparisons performed by this method differ
0N/A * from those performed by the Java language numerical comparison
0N/A * operators ({@code <, <=, ==, >=, >}) when
0N/A * applied to primitive {@code float} values:
0N/A *
0N/A * <ul><li>
0N/A * {@code Float.NaN} is considered by this method to
0N/A * be equal to itself and greater than all other
0N/A * {@code float} values
0N/A * (including {@code Float.POSITIVE_INFINITY}).
0N/A * <li>
0N/A * {@code 0.0f} is considered by this method to be greater
0N/A * than {@code -0.0f}.
0N/A * </ul>
0N/A *
0N/A * This ensures that the <i>natural ordering</i> of {@code Float}
0N/A * objects imposed by this method is <i>consistent with equals</i>.
0N/A *
0N/A * @param anotherFloat the {@code Float} to be compared.
0N/A * @return the value {@code 0} if {@code anotherFloat} is
0N/A * numerically equal to this {@code Float}; a value
0N/A * less than {@code 0} if this {@code Float}
0N/A * is numerically less than {@code anotherFloat};
0N/A * and a value greater than {@code 0} if this
0N/A * {@code Float} is numerically greater than
0N/A * {@code anotherFloat}.
0N/A *
0N/A * @since 1.2
0N/A * @see Comparable#compareTo(Object)
0N/A */
0N/A public int compareTo(Float anotherFloat) {
0N/A return Float.compare(value, anotherFloat.value);
0N/A }
0N/A
0N/A /**
0N/A * Compares the two specified {@code float} values. The sign
0N/A * of the integer value returned is the same as that of the
0N/A * integer that would be returned by the call:
0N/A * <pre>
0N/A * new Float(f1).compareTo(new Float(f2))
0N/A * </pre>
0N/A *
0N/A * @param f1 the first {@code float} to compare.
0N/A * @param f2 the second {@code float} to compare.
0N/A * @return the value {@code 0} if {@code f1} is
0N/A * numerically equal to {@code f2}; a value less than
0N/A * {@code 0} if {@code f1} is numerically less than
0N/A * {@code f2}; and a value greater than {@code 0}
0N/A * if {@code f1} is numerically greater than
0N/A * {@code f2}.
0N/A * @since 1.4
0N/A */
0N/A public static int compare(float f1, float f2) {
3202N/A if (f1 < f2)
0N/A return -1; // Neither val is NaN, thisVal is smaller
0N/A if (f1 > f2)
0N/A return 1; // Neither val is NaN, thisVal is larger
0N/A
3202N/A // Cannot use floatToRawIntBits because of possibility of NaNs.
3202N/A int thisBits = Float.floatToIntBits(f1);
0N/A int anotherBits = Float.floatToIntBits(f2);
0N/A
0N/A return (thisBits == anotherBits ? 0 : // Values are equal
0N/A (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
0N/A 1)); // (0.0, -0.0) or (NaN, !NaN)
0N/A }
0N/A
0N/A /** use serialVersionUID from JDK 1.0.2 for interoperability */
0N/A private static final long serialVersionUID = -2671257302660747028L;
0N/A}