Home | History | Annotate | Download | only in lang
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
      4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      5  *
      6  * This code is free software; you can redistribute it and/or modify it
      7  * under the terms of the GNU General Public License version 2 only, as
      8  * published by the Free Software Foundation.  Oracle designates this
      9  * particular file as subject to the "Classpath" exception as provided
     10  * by Oracle in the LICENSE file that accompanied this code.
     11  *
     12  * This code is distributed in the hope that it will be useful, but WITHOUT
     13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     15  * version 2 for more details (a copy is included in the LICENSE file that
     16  * accompanied this code).
     17  *
     18  * You should have received a copy of the GNU General Public License version
     19  * 2 along with this work; if not, write to the Free Software Foundation,
     20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     21  *
     22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     23  * or visit www.oracle.com if you need additional information or have any
     24  * questions.
     25  */
     26 
     27 package java.lang;
     28 
     29 import sun.misc.FpUtils;
     30 import sun.misc.FloatConsts;
     31 import sun.misc.DoubleConsts;
     32 
     33 /**
     34  * The {@code Float} class wraps a value of primitive type
     35  * {@code float} in an object. An object of type
     36  * {@code Float} contains a single field whose type is
     37  * {@code float}.
     38  *
     39  * <p>In addition, this class provides several methods for converting a
     40  * {@code float} to a {@code String} and a
     41  * {@code String} to a {@code float}, as well as other
     42  * constants and methods useful when dealing with a
     43  * {@code float}.
     44  *
     45  * @author  Lee Boynton
     46  * @author  Arthur van Hoff
     47  * @author  Joseph D. Darcy
     48  * @since JDK1.0
     49  */
     50 public final class Float extends Number implements Comparable<Float> {
     51     /**
     52      * A constant holding the positive infinity of type
     53      * {@code float}. It is equal to the value returned by
     54      * {@code Float.intBitsToFloat(0x7f800000)}.
     55      */
     56     public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
     57 
     58     /**
     59      * A constant holding the negative infinity of type
     60      * {@code float}. It is equal to the value returned by
     61      * {@code Float.intBitsToFloat(0xff800000)}.
     62      */
     63     public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
     64 
     65     /**
     66      * A constant holding a Not-a-Number (NaN) value of type
     67      * {@code float}.  It is equivalent to the value returned by
     68      * {@code Float.intBitsToFloat(0x7fc00000)}.
     69      */
     70     public static final float NaN = 0.0f / 0.0f;
     71 
     72     /**
     73      * A constant holding the largest positive finite value of type
     74      * {@code float}, (2-2<sup>-23</sup>)&middot;2<sup>127</sup>.
     75      * It is equal to the hexadecimal floating-point literal
     76      * {@code 0x1.fffffeP+127f} and also equal to
     77      * {@code Float.intBitsToFloat(0x7f7fffff)}.
     78      */
     79     public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f
     80 
     81     /**
     82      * A constant holding the smallest positive normal value of type
     83      * {@code float}, 2<sup>-126</sup>.  It is equal to the
     84      * hexadecimal floating-point literal {@code 0x1.0p-126f} and also
     85      * equal to {@code Float.intBitsToFloat(0x00800000)}.
     86      *
     87      * @since 1.6
     88      */
     89     public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f
     90 
     91     /**
     92      * A constant holding the smallest positive nonzero value of type
     93      * {@code float}, 2<sup>-149</sup>. It is equal to the
     94      * hexadecimal floating-point literal {@code 0x0.000002P-126f}
     95      * and also equal to {@code Float.intBitsToFloat(0x1)}.
     96      */
     97     public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f
     98 
     99     /**
    100      * Maximum exponent a finite {@code float} variable may have.  It
    101      * is equal to the value returned by {@code
    102      * Math.getExponent(Float.MAX_VALUE)}.
    103      *
    104      * @since 1.6
    105      */
    106     public static final int MAX_EXPONENT = 127;
    107 
    108     /**
    109      * Minimum exponent a normalized {@code float} variable may have.
    110      * It is equal to the value returned by {@code
    111      * Math.getExponent(Float.MIN_NORMAL)}.
    112      *
    113      * @since 1.6
    114      */
    115     public static final int MIN_EXPONENT = -126;
    116 
    117     /**
    118      * The number of bits used to represent a {@code float} value.
    119      *
    120      * @since 1.5
    121      */
    122     public static final int SIZE = 32;
    123 
    124     /**
    125      * The number of bytes used to represent a {@code float} value.
    126      *
    127      * @since 1.8
    128      */
    129     public static final int BYTES = SIZE / Byte.SIZE;
    130 
    131     /**
    132      * The {@code Class} instance representing the primitive type
    133      * {@code float}.
    134      *
    135      * @since JDK1.1
    136      */
    137     public static final Class<Float> TYPE = (Class<Float>) float[].class.getComponentType();
    138 
    139     /**
    140      * Returns a string representation of the {@code float}
    141      * argument. All characters mentioned below are ASCII characters.
    142      * <ul>
    143      * <li>If the argument is NaN, the result is the string
    144      * "{@code NaN}".
    145      * <li>Otherwise, the result is a string that represents the sign and
    146      *     magnitude (absolute value) of the argument. If the sign is
    147      *     negative, the first character of the result is
    148      *     '{@code -}' (<code>'&#92;u002D'</code>); if the sign is
    149      *     positive, no sign character appears in the result. As for
    150      *     the magnitude <i>m</i>:
    151      * <ul>
    152      * <li>If <i>m</i> is infinity, it is represented by the characters
    153      *     {@code "Infinity"}; thus, positive infinity produces
    154      *     the result {@code "Infinity"} and negative infinity
    155      *     produces the result {@code "-Infinity"}.
    156      * <li>If <i>m</i> is zero, it is represented by the characters
    157      *     {@code "0.0"}; thus, negative zero produces the result
    158      *     {@code "-0.0"} and positive zero produces the result
    159      *     {@code "0.0"}.
    160      * <li> If <i>m</i> is greater than or equal to 10<sup>-3</sup> but
    161      *      less than 10<sup>7</sup>, then it is represented as the
    162      *      integer part of <i>m</i>, in decimal form with no leading
    163      *      zeroes, followed by '{@code .}'
    164      *      (<code>'&#92;u002E'</code>), followed by one or more
    165      *      decimal digits representing the fractional part of
    166      *      <i>m</i>.
    167      * <li> If <i>m</i> is less than 10<sup>-3</sup> or greater than or
    168      *      equal to 10<sup>7</sup>, then it is represented in
    169      *      so-called "computerized scientific notation." Let <i>n</i>
    170      *      be the unique integer such that 10<sup><i>n</i> </sup>&le;
    171      *      <i>m</i> {@literal <} 10<sup><i>n</i>+1</sup>; then let <i>a</i>
    172      *      be the mathematically exact quotient of <i>m</i> and
    173      *      10<sup><i>n</i></sup> so that 1 &le; <i>a</i> {@literal <} 10.
    174      *      The magnitude is then represented as the integer part of
    175      *      <i>a</i>, as a single decimal digit, followed by
    176      *      '{@code .}' (<code>'&#92;u002E'</code>), followed by
    177      *      decimal digits representing the fractional part of
    178      *      <i>a</i>, followed by the letter '{@code E}'
    179      *      (<code>'&#92;u0045'</code>), followed by a representation
    180      *      of <i>n</i> as a decimal integer, as produced by the
    181      *      method {@link java.lang.Integer#toString(int)}.
    182      *
    183      * </ul>
    184      * </ul>
    185      * How many digits must be printed for the fractional part of
    186      * <i>m</i> or <i>a</i>? There must be at least one digit
    187      * to represent the fractional part, and beyond that as many, but
    188      * only as many, more digits as are needed to uniquely distinguish
    189      * the argument value from adjacent values of type
    190      * {@code float}. That is, suppose that <i>x</i> is the
    191      * exact mathematical value represented by the decimal
    192      * representation produced by this method for a finite nonzero
    193      * argument <i>f</i>. Then <i>f</i> must be the {@code float}
    194      * value nearest to <i>x</i>; or, if two {@code float} values are
    195      * equally close to <i>x</i>, then <i>f</i> must be one of
    196      * them and the least significant bit of the significand of
    197      * <i>f</i> must be {@code 0}.
    198      *
    199      * <p>To create localized string representations of a floating-point
    200      * value, use subclasses of {@link java.text.NumberFormat}.
    201      *
    202      * @param   f   the float to be converted.
    203      * @return a string representation of the argument.
    204      */
    205     public static String toString(float f) {
    206         return FloatingDecimal.getThreadLocalInstance().loadFloat(f).toJavaFormatString();
    207     }
    208 
    209     /**
    210      * Returns a hexadecimal string representation of the
    211      * {@code float} argument. All characters mentioned below are
    212      * ASCII characters.
    213      *
    214      * <ul>
    215      * <li>If the argument is NaN, the result is the string
    216      *     "{@code NaN}".
    217      * <li>Otherwise, the result is a string that represents the sign and
    218      * magnitude (absolute value) of the argument. If the sign is negative,
    219      * the first character of the result is '{@code -}'
    220      * (<code>'&#92;u002D'</code>); if the sign is positive, no sign character
    221      * appears in the result. As for the magnitude <i>m</i>:
    222      *
    223      * <ul>
    224      * <li>If <i>m</i> is infinity, it is represented by the string
    225      * {@code "Infinity"}; thus, positive infinity produces the
    226      * result {@code "Infinity"} and negative infinity produces
    227      * the result {@code "-Infinity"}.
    228      *
    229      * <li>If <i>m</i> is zero, it is represented by the string
    230      * {@code "0x0.0p0"}; thus, negative zero produces the result
    231      * {@code "-0x0.0p0"} and positive zero produces the result
    232      * {@code "0x0.0p0"}.
    233      *
    234      * <li>If <i>m</i> is a {@code float} value with a
    235      * normalized representation, substrings are used to represent the
    236      * significand and exponent fields.  The significand is
    237      * represented by the characters {@code "0x1."}
    238      * followed by a lowercase hexadecimal representation of the rest
    239      * of the significand as a fraction.  Trailing zeros in the
    240      * hexadecimal representation are removed unless all the digits
    241      * are zero, in which case a single zero is used. Next, the
    242      * exponent is represented by {@code "p"} followed
    243      * by a decimal string of the unbiased exponent as if produced by
    244      * a call to {@link Integer#toString(int) Integer.toString} on the
    245      * exponent value.
    246      *
    247      * <li>If <i>m</i> is a {@code float} value with a subnormal
    248      * representation, the significand is represented by the
    249      * characters {@code "0x0."} followed by a
    250      * hexadecimal representation of the rest of the significand as a
    251      * fraction.  Trailing zeros in the hexadecimal representation are
    252      * removed. Next, the exponent is represented by
    253      * {@code "p-126"}.  Note that there must be at
    254      * least one nonzero digit in a subnormal significand.
    255      *
    256      * </ul>
    257      *
    258      * </ul>
    259      *
    260      * <table border>
    261      * <caption><h3>Examples</h3></caption>
    262      * <tr><th>Floating-point Value</th><th>Hexadecimal String</th>
    263      * <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td>
    264      * <tr><td>{@code -1.0}</td>        <td>{@code -0x1.0p0}</td>
    265      * <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>
    266      * <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>
    267      * <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>
    268      * <tr><td>{@code 0.25}</td>        <td>{@code 0x1.0p-2}</td>
    269      * <tr><td>{@code Float.MAX_VALUE}</td>
    270      *     <td>{@code 0x1.fffffep127}</td>
    271      * <tr><td>{@code Minimum Normal Value}</td>
    272      *     <td>{@code 0x1.0p-126}</td>
    273      * <tr><td>{@code Maximum Subnormal Value}</td>
    274      *     <td>{@code 0x0.fffffep-126}</td>
    275      * <tr><td>{@code Float.MIN_VALUE}</td>
    276      *     <td>{@code 0x0.000002p-126}</td>
    277      * </table>
    278      * @param   f   the {@code float} to be converted.
    279      * @return a hex string representation of the argument.
    280      * @since 1.5
    281      * @author Joseph D. Darcy
    282      */
    283     public static String toHexString(float f) {
    284         if (Math.abs(f) < FloatConsts.MIN_NORMAL
    285             &&  f != 0.0f ) {// float subnormal
    286             // Adjust exponent to create subnormal double, then
    287             // replace subnormal double exponent with subnormal float
    288             // exponent
    289             String s = Double.toHexString(FpUtils.scalb((double)f,
    290                                                         /* -1022+126 */
    291                                                         DoubleConsts.MIN_EXPONENT-
    292                                                         FloatConsts.MIN_EXPONENT));
    293             return s.replaceFirst("p-1022$", "p-126");
    294         }
    295         else // double string will be the same as float string
    296             return Double.toHexString(f);
    297     }
    298 
    299     /**
    300      * Returns a {@code Float} object holding the
    301      * {@code float} value represented by the argument string
    302      * {@code s}.
    303      *
    304      * <p>If {@code s} is {@code null}, then a
    305      * {@code NullPointerException} is thrown.
    306      *
    307      * <p>Leading and trailing whitespace characters in {@code s}
    308      * are ignored.  Whitespace is removed as if by the {@link
    309      * String#trim} method; that is, both ASCII space and control
    310      * characters are removed. The rest of {@code s} should
    311      * constitute a <i>FloatValue</i> as described by the lexical
    312      * syntax rules:
    313      *
    314      * <blockquote>
    315      * <dl>
    316      * <dt><i>FloatValue:</i>
    317      * <dd><i>Sign<sub>opt</sub></i> {@code NaN}
    318      * <dd><i>Sign<sub>opt</sub></i> {@code Infinity}
    319      * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
    320      * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
    321      * <dd><i>SignedInteger</i>
    322      * </dl>
    323      *
    324      * <p>
    325      *
    326      * <dl>
    327      * <dt><i>HexFloatingPointLiteral</i>:
    328      * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
    329      * </dl>
    330      *
    331      * <p>
    332      *
    333      * <dl>
    334      * <dt><i>HexSignificand:</i>
    335      * <dd><i>HexNumeral</i>
    336      * <dd><i>HexNumeral</i> {@code .}
    337      * <dd>{@code 0x} <i>HexDigits<sub>opt</sub>
    338      *     </i>{@code .}<i> HexDigits</i>
    339      * <dd>{@code 0X}<i> HexDigits<sub>opt</sub>
    340      *     </i>{@code .} <i>HexDigits</i>
    341      * </dl>
    342      *
    343      * <p>
    344      *
    345      * <dl>
    346      * <dt><i>BinaryExponent:</i>
    347      * <dd><i>BinaryExponentIndicator SignedInteger</i>
    348      * </dl>
    349      *
    350      * <p>
    351      *
    352      * <dl>
    353      * <dt><i>BinaryExponentIndicator:</i>
    354      * <dd>{@code p}
    355      * <dd>{@code P}
    356      * </dl>
    357      *
    358      * </blockquote>
    359      *
    360      * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
    361      * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
    362      * <i>FloatTypeSuffix</i> are as defined in the lexical structure
    363      * sections of
    364      * <cite>The Java&trade; Language Specification</cite>,
    365      * except that underscores are not accepted between digits.
    366      * If {@code s} does not have the form of
    367      * a <i>FloatValue</i>, then a {@code NumberFormatException}
    368      * is thrown. Otherwise, {@code s} is regarded as
    369      * representing an exact decimal value in the usual
    370      * "computerized scientific notation" or as an exact
    371      * hexadecimal value; this exact numerical value is then
    372      * conceptually converted to an "infinitely precise"
    373      * binary value that is then rounded to type {@code float}
    374      * by the usual round-to-nearest rule of IEEE 754 floating-point
    375      * arithmetic, which includes preserving the sign of a zero
    376      * value.
    377      *
    378      * Note that the round-to-nearest rule also implies overflow and
    379      * underflow behaviour; if the exact value of {@code s} is large
    380      * enough in magnitude (greater than or equal to ({@link
    381      * #MAX_VALUE} + {@link Math#ulp(float) ulp(MAX_VALUE)}/2),
    382      * rounding to {@code float} will result in an infinity and if the
    383      * exact value of {@code s} is small enough in magnitude (less
    384      * than or equal to {@link #MIN_VALUE}/2), rounding to float will
    385      * result in a zero.
    386      *
    387      * Finally, after rounding a {@code Float} object representing
    388      * this {@code float} value is returned.
    389      *
    390      * <p>To interpret localized string representations of a
    391      * floating-point value, use subclasses of {@link
    392      * java.text.NumberFormat}.
    393      *
    394      * <p>Note that trailing format specifiers, specifiers that
    395      * determine the type of a floating-point literal
    396      * ({@code 1.0f} is a {@code float} value;
    397      * {@code 1.0d} is a {@code double} value), do
    398      * <em>not</em> influence the results of this method.  In other
    399      * words, the numerical value of the input string is converted
    400      * directly to the target floating-point type.  In general, the
    401      * two-step sequence of conversions, string to {@code double}
    402      * followed by {@code double} to {@code float}, is
    403      * <em>not</em> equivalent to converting a string directly to
    404      * {@code float}.  For example, if first converted to an
    405      * intermediate {@code double} and then to
    406      * {@code float}, the string<br>
    407      * {@code "1.00000017881393421514957253748434595763683319091796875001d"}<br>
    408      * results in the {@code float} value
    409      * {@code 1.0000002f}; if the string is converted directly to
    410      * {@code float}, <code>1.000000<b>1</b>f</code> results.
    411      *
    412      * <p>To avoid calling this method on an invalid string and having
    413      * a {@code NumberFormatException} be thrown, the documentation
    414      * for {@link Double#valueOf Double.valueOf} lists a regular
    415      * expression which can be used to screen the input.
    416      *
    417      * @param   s   the string to be parsed.
    418      * @return  a {@code Float} object holding the value
    419      *          represented by the {@code String} argument.
    420      * @throws  NumberFormatException  if the string does not contain a
    421      *          parsable number.
    422      */
    423     public static Float valueOf(String s) throws NumberFormatException {
    424         return new Float(FloatingDecimal.getThreadLocalInstance().readJavaFormatString(s).floatValue());
    425     }
    426 
    427     /**
    428      * Returns a {@code Float} instance representing the specified
    429      * {@code float} value.
    430      * If a new {@code Float} instance is not required, this method
    431      * should generally be used in preference to the constructor
    432      * {@link #Float(float)}, as this method is likely to yield
    433      * significantly better space and time performance by caching
    434      * frequently requested values.
    435      *
    436      * @param  f a float value.
    437      * @return a {@code Float} instance representing {@code f}.
    438      * @since  1.5
    439      */
    440     public static Float valueOf(float f) {
    441         return new Float(f);
    442     }
    443 
    444     /**
    445      * Returns a new {@code float} initialized to the value
    446      * represented by the specified {@code String}, as performed
    447      * by the {@code valueOf} method of class {@code Float}.
    448      *
    449      * @param  s the string to be parsed.
    450      * @return the {@code float} value represented by the string
    451      *         argument.
    452      * @throws NullPointerException  if the string is null
    453      * @throws NumberFormatException if the string does not contain a
    454      *               parsable {@code float}.
    455      * @see    java.lang.Float#valueOf(String)
    456      * @since 1.2
    457      */
    458     public static float parseFloat(String s) throws NumberFormatException {
    459         return FloatingDecimal.getThreadLocalInstance().readJavaFormatString(s).floatValue();
    460     }
    461 
    462     /**
    463      * Returns {@code true} if the specified number is a
    464      * Not-a-Number (NaN) value, {@code false} otherwise.
    465      *
    466      * @param   v   the value to be tested.
    467      * @return  {@code true} if the argument is NaN;
    468      *          {@code false} otherwise.
    469      */
    470     static public boolean isNaN(float v) {
    471         return (v != v);
    472     }
    473 
    474     /**
    475      * Returns {@code true} if the specified number is infinitely
    476      * large in magnitude, {@code false} otherwise.
    477      *
    478      * @param   v   the value to be tested.
    479      * @return  {@code true} if the argument is positive infinity or
    480      *          negative infinity; {@code false} otherwise.
    481      */
    482     static public boolean isInfinite(float v) {
    483         return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
    484     }
    485 
    486     /**
    487      * Returns {@code true} if the argument is a finite floating-point
    488      * value; returns {@code false} otherwise (for NaN and infinity
    489      * arguments).
    490      *
    491      * @param f the {@code float} value to be tested
    492      * @return {@code true} if the argument is a finite
    493      * floating-point value, {@code false} otherwise.
    494      * @since 1.8
    495      */
    496      public static boolean isFinite(float f) {
    497         return Math.abs(f) <= FloatConsts.MAX_VALUE;
    498     }
    499 
    500     /**
    501      * The value of the Float.
    502      *
    503      * @serial
    504      */
    505     private final float value;
    506 
    507     /**
    508      * Constructs a newly allocated {@code Float} object that
    509      * represents the primitive {@code float} argument.
    510      *
    511      * @param   value   the value to be represented by the {@code Float}.
    512      */
    513     public Float(float value) {
    514         this.value = value;
    515     }
    516 
    517     /**
    518      * Constructs a newly allocated {@code Float} object that
    519      * represents the argument converted to type {@code float}.
    520      *
    521      * @param   value   the value to be represented by the {@code Float}.
    522      */
    523     public Float(double value) {
    524         this.value = (float)value;
    525     }
    526 
    527     /**
    528      * Constructs a newly allocated {@code Float} object that
    529      * represents the floating-point value of type {@code float}
    530      * represented by the string. The string is converted to a
    531      * {@code float} value as if by the {@code valueOf} method.
    532      *
    533      * @param      s   a string to be converted to a {@code Float}.
    534      * @throws  NumberFormatException  if the string does not contain a
    535      *               parsable number.
    536      * @see        java.lang.Float#valueOf(java.lang.String)
    537      */
    538     public Float(String s) throws NumberFormatException {
    539         // REMIND: this is inefficient
    540         this(valueOf(s).floatValue());
    541     }
    542 
    543     /**
    544      * Returns {@code true} if this {@code Float} value is a
    545      * Not-a-Number (NaN), {@code false} otherwise.
    546      *
    547      * @return  {@code true} if the value represented by this object is
    548      *          NaN; {@code false} otherwise.
    549      */
    550     public boolean isNaN() {
    551         return isNaN(value);
    552     }
    553 
    554     /**
    555      * Returns {@code true} if this {@code Float} value is
    556      * infinitely large in magnitude, {@code false} otherwise.
    557      *
    558      * @return  {@code true} if the value represented by this object is
    559      *          positive infinity or negative infinity;
    560      *          {@code false} otherwise.
    561      */
    562     public boolean isInfinite() {
    563         return isInfinite(value);
    564     }
    565 
    566     /**
    567      * Returns a string representation of this {@code Float} object.
    568      * The primitive {@code float} value represented by this object
    569      * is converted to a {@code String} exactly as if by the method
    570      * {@code toString} of one argument.
    571      *
    572      * @return  a {@code String} representation of this object.
    573      * @see java.lang.Float#toString(float)
    574      */
    575     public String toString() {
    576         return Float.toString(value);
    577     }
    578 
    579     /**
    580      * Returns the value of this {@code Float} as a {@code byte} (by
    581      * casting to a {@code byte}).
    582      *
    583      * @return  the {@code float} value represented by this object
    584      *          converted to type {@code byte}
    585      */
    586     public byte byteValue() {
    587         return (byte)value;
    588     }
    589 
    590     /**
    591      * Returns the value of this {@code Float} as a {@code short} (by
    592      * casting to a {@code short}).
    593      *
    594      * @return  the {@code float} value represented by this object
    595      *          converted to type {@code short}
    596      * @since JDK1.1
    597      */
    598     public short shortValue() {
    599         return (short)value;
    600     }
    601 
    602     /**
    603      * Returns the value of this {@code Float} as an {@code int} (by
    604      * casting to type {@code int}).
    605      *
    606      * @return  the {@code float} value represented by this object
    607      *          converted to type {@code int}
    608      */
    609     public int intValue() {
    610         return (int)value;
    611     }
    612 
    613     /**
    614      * Returns value of this {@code Float} as a {@code long} (by
    615      * casting to type {@code long}).
    616      *
    617      * @return  the {@code float} value represented by this object
    618      *          converted to type {@code long}
    619      */
    620     public long longValue() {
    621         return (long)value;
    622     }
    623 
    624     /**
    625      * Returns the {@code float} value of this {@code Float} object.
    626      *
    627      * @return the {@code float} value represented by this object
    628      */
    629     public float floatValue() {
    630         return value;
    631     }
    632 
    633     /**
    634      * Returns the {@code double} value of this {@code Float} object.
    635      *
    636      * @return the {@code float} value represented by this
    637      *         object is converted to type {@code double} and the
    638      *         result of the conversion is returned.
    639      */
    640     public double doubleValue() {
    641         return (double)value;
    642     }
    643 
    644     /**
    645      * Returns a hash code for this {@code Float} object. The
    646      * result is the integer bit representation, exactly as produced
    647      * by the method {@link #floatToIntBits(float)}, of the primitive
    648      * {@code float} value represented by this {@code Float}
    649      * object.
    650      *
    651      * @return a hash code value for this object.
    652      */
    653     public int hashCode() {
    654         return floatToIntBits(value);
    655     }
    656 
    657     /**
    658      * Returns a hash code for a {@code float} value; compatible with
    659      * {@code Float.hashCode()}.
    660      *
    661      * @param value the value to hash
    662      * @return a hash code value for a {@code float} value.
    663      * @since 1.8
    664      */
    665     public static int hashCode(float value) {
    666         return floatToIntBits(value);
    667     }
    668 
    669     /**
    670 
    671      * Compares this object against the specified object.  The result
    672      * is {@code true} if and only if the argument is not
    673      * {@code null} and is a {@code Float} object that
    674      * represents a {@code float} with the same value as the
    675      * {@code float} represented by this object. For this
    676      * purpose, two {@code float} values are considered to be the
    677      * same if and only if the method {@link #floatToIntBits(float)}
    678      * returns the identical {@code int} value when applied to
    679      * each.
    680      *
    681      * <p>Note that in most cases, for two instances of class
    682      * {@code Float}, {@code f1} and {@code f2}, the value
    683      * of {@code f1.equals(f2)} is {@code true} if and only if
    684      *
    685      * <blockquote><pre>
    686      *   f1.floatValue() == f2.floatValue()
    687      * </pre></blockquote>
    688      *
    689      * <p>also has the value {@code true}. However, there are two exceptions:
    690      * <ul>
    691      * <li>If {@code f1} and {@code f2} both represent
    692      *     {@code Float.NaN}, then the {@code equals} method returns
    693      *     {@code true}, even though {@code Float.NaN==Float.NaN}
    694      *     has the value {@code false}.
    695      * <li>If {@code f1} represents {@code +0.0f} while
    696      *     {@code f2} represents {@code -0.0f}, or vice
    697      *     versa, the {@code equal} test has the value
    698      *     {@code false}, even though {@code 0.0f==-0.0f}
    699      *     has the value {@code true}.
    700      * </ul>
    701      *
    702      * This definition allows hash tables to operate properly.
    703      *
    704      * @param obj the object to be compared
    705      * @return  {@code true} if the objects are the same;
    706      *          {@code false} otherwise.
    707      * @see java.lang.Float#floatToIntBits(float)
    708      */
    709     public boolean equals(Object obj) {
    710         return (obj instanceof Float)
    711                && (floatToIntBits(((Float)obj).value) == floatToIntBits(value));
    712     }
    713 
    714     /**
    715      * Returns a representation of the specified floating-point value
    716      * according to the IEEE 754 floating-point "single format" bit
    717      * layout.
    718      *
    719      * <p>Bit 31 (the bit that is selected by the mask
    720      * {@code 0x80000000}) represents the sign of the floating-point
    721      * number.
    722      * Bits 30-23 (the bits that are selected by the mask
    723      * {@code 0x7f800000}) represent the exponent.
    724      * Bits 22-0 (the bits that are selected by the mask
    725      * {@code 0x007fffff}) represent the significand (sometimes called
    726      * the mantissa) of the floating-point number.
    727      *
    728      * <p>If the argument is positive infinity, the result is
    729      * {@code 0x7f800000}.
    730      *
    731      * <p>If the argument is negative infinity, the result is
    732      * {@code 0xff800000}.
    733      *
    734      * <p>If the argument is NaN, the result is {@code 0x7fc00000}.
    735      *
    736      * <p>In all cases, the result is an integer that, when given to the
    737      * {@link #intBitsToFloat(int)} method, will produce a floating-point
    738      * value the same as the argument to {@code floatToIntBits}
    739      * (except all NaN values are collapsed to a single
    740      * "canonical" NaN value).
    741      *
    742      * @param   value   a floating-point number.
    743      * @return the bits that represent the floating-point number.
    744      */
    745     public static int floatToIntBits(float value) {
    746         int result = floatToRawIntBits(value);
    747         // Check for NaN based on values of bit fields, maximum
    748         // exponent and nonzero significand.
    749         if ( ((result & FloatConsts.EXP_BIT_MASK) ==
    750               FloatConsts.EXP_BIT_MASK) &&
    751              (result & FloatConsts.SIGNIF_BIT_MASK) != 0)
    752             result = 0x7fc00000;
    753         return result;
    754     }
    755 
    756     /**
    757      * Returns a representation of the specified floating-point value
    758      * according to the IEEE 754 floating-point "single format" bit
    759      * layout, preserving Not-a-Number (NaN) values.
    760      *
    761      * <p>Bit 31 (the bit that is selected by the mask
    762      * {@code 0x80000000}) represents the sign of the floating-point
    763      * number.
    764      * Bits 30-23 (the bits that are selected by the mask
    765      * {@code 0x7f800000}) represent the exponent.
    766      * Bits 22-0 (the bits that are selected by the mask
    767      * {@code 0x007fffff}) represent the significand (sometimes called
    768      * the mantissa) of the floating-point number.
    769      *
    770      * <p>If the argument is positive infinity, the result is
    771      * {@code 0x7f800000}.
    772      *
    773      * <p>If the argument is negative infinity, the result is
    774      * {@code 0xff800000}.
    775      *
    776      * <p>If the argument is NaN, the result is the integer representing
    777      * the actual NaN value.  Unlike the {@code floatToIntBits}
    778      * method, {@code floatToRawIntBits} does not collapse all the
    779      * bit patterns encoding a NaN to a single "canonical"
    780      * NaN value.
    781      *
    782      * <p>In all cases, the result is an integer that, when given to the
    783      * {@link #intBitsToFloat(int)} method, will produce a
    784      * floating-point value the same as the argument to
    785      * {@code floatToRawIntBits}.
    786      *
    787      * @param   value   a floating-point number.
    788      * @return the bits that represent the floating-point number.
    789      * @since 1.3
    790      */
    791     public static native int floatToRawIntBits(float value);
    792 
    793     /**
    794      * Returns the {@code float} value corresponding to a given
    795      * bit representation.
    796      * The argument is considered to be a representation of a
    797      * floating-point value according to the IEEE 754 floating-point
    798      * "single format" bit layout.
    799      *
    800      * <p>If the argument is {@code 0x7f800000}, the result is positive
    801      * infinity.
    802      *
    803      * <p>If the argument is {@code 0xff800000}, the result is negative
    804      * infinity.
    805      *
    806      * <p>If the argument is any value in the range
    807      * {@code 0x7f800001} through {@code 0x7fffffff} or in
    808      * the range {@code 0xff800001} through
    809      * {@code 0xffffffff}, the result is a NaN.  No IEEE 754
    810      * floating-point operation provided by Java can distinguish
    811      * between two NaN values of the same type with different bit
    812      * patterns.  Distinct values of NaN are only distinguishable by
    813      * use of the {@code Float.floatToRawIntBits} method.
    814      *
    815      * <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
    816      * values that can be computed from the argument:
    817      *
    818      * <blockquote><pre>
    819      * int s = ((bits &gt;&gt; 31) == 0) ? 1 : -1;
    820      * int e = ((bits &gt;&gt; 23) & 0xff);
    821      * int m = (e == 0) ?
    822      *                 (bits & 0x7fffff) &lt;&lt; 1 :
    823      *                 (bits & 0x7fffff) | 0x800000;
    824      * </pre></blockquote>
    825      *
    826      * Then the floating-point result equals the value of the mathematical
    827      * expression <i>s</i>&middot;<i>m</i>&middot;2<sup><i>e</i>-150</sup>.
    828      *
    829      * <p>Note that this method may not be able to return a
    830      * {@code float} NaN with exactly same bit pattern as the
    831      * {@code int} argument.  IEEE 754 distinguishes between two
    832      * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>.  The
    833      * differences between the two kinds of NaN are generally not
    834      * visible in Java.  Arithmetic operations on signaling NaNs turn
    835      * them into quiet NaNs with a different, but often similar, bit
    836      * pattern.  However, on some processors merely copying a
    837      * signaling NaN also performs that conversion.  In particular,
    838      * copying a signaling NaN to return it to the calling method may
    839      * perform this conversion.  So {@code intBitsToFloat} may
    840      * not be able to return a {@code float} with a signaling NaN
    841      * bit pattern.  Consequently, for some {@code int} values,
    842      * {@code floatToRawIntBits(intBitsToFloat(start))} may
    843      * <i>not</i> equal {@code start}.  Moreover, which
    844      * particular bit patterns represent signaling NaNs is platform
    845      * dependent; although all NaN bit patterns, quiet or signaling,
    846      * must be in the NaN range identified above.
    847      *
    848      * @param   bits   an integer.
    849      * @return  the {@code float} floating-point value with the same bit
    850      *          pattern.
    851      */
    852     public static native float intBitsToFloat(int bits);
    853 
    854     /**
    855      * Compares two {@code Float} objects numerically.  There are
    856      * two ways in which comparisons performed by this method differ
    857      * from those performed by the Java language numerical comparison
    858      * operators ({@code <, <=, ==, >=, >}) when
    859      * applied to primitive {@code float} values:
    860      *
    861      * <ul><li>
    862      *          {@code Float.NaN} is considered by this method to
    863      *          be equal to itself and greater than all other
    864      *          {@code float} values
    865      *          (including {@code Float.POSITIVE_INFINITY}).
    866      * <li>
    867      *          {@code 0.0f} is considered by this method to be greater
    868      *          than {@code -0.0f}.
    869      * </ul>
    870      *
    871      * This ensures that the <i>natural ordering</i> of {@code Float}
    872      * objects imposed by this method is <i>consistent with equals</i>.
    873      *
    874      * @param   anotherFloat   the {@code Float} to be compared.
    875      * @return  the value {@code 0} if {@code anotherFloat} is
    876      *          numerically equal to this {@code Float}; a value
    877      *          less than {@code 0} if this {@code Float}
    878      *          is numerically less than {@code anotherFloat};
    879      *          and a value greater than {@code 0} if this
    880      *          {@code Float} is numerically greater than
    881      *          {@code anotherFloat}.
    882      *
    883      * @since   1.2
    884      * @see Comparable#compareTo(Object)
    885      */
    886     public int compareTo(Float anotherFloat) {
    887         return Float.compare(value, anotherFloat.value);
    888     }
    889 
    890     /**
    891      * Compares the two specified {@code float} values. The sign
    892      * of the integer value returned is the same as that of the
    893      * integer that would be returned by the call:
    894      * <pre>
    895      *    new Float(f1).compareTo(new Float(f2))
    896      * </pre>
    897      *
    898      * @param   f1        the first {@code float} to compare.
    899      * @param   f2        the second {@code float} to compare.
    900      * @return  the value {@code 0} if {@code f1} is
    901      *          numerically equal to {@code f2}; a value less than
    902      *          {@code 0} if {@code f1} is numerically less than
    903      *          {@code f2}; and a value greater than {@code 0}
    904      *          if {@code f1} is numerically greater than
    905      *          {@code f2}.
    906      * @since 1.4
    907      */
    908     public static int compare(float f1, float f2) {
    909         if (f1 < f2)
    910             return -1;           // Neither val is NaN, thisVal is smaller
    911         if (f1 > f2)
    912             return 1;            // Neither val is NaN, thisVal is larger
    913 
    914         // Cannot use floatToRawIntBits because of possibility of NaNs.
    915         int thisBits    = Float.floatToIntBits(f1);
    916         int anotherBits = Float.floatToIntBits(f2);
    917 
    918         return (thisBits == anotherBits ?  0 : // Values are equal
    919                 (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
    920                  1));                          // (0.0, -0.0) or (NaN, !NaN)
    921     }
    922 
    923     /**
    924      * Adds two {@code float} values together as per the + operator.
    925      *
    926      * @param a the first operand
    927      * @param b the second operand
    928      * @return the sum of {@code a} and {@code b}
    929      * @jls 4.2.4 Floating-Point Operations
    930      * @see java.util.function.BinaryOperator
    931      * @since 1.8
    932      */
    933     public static float sum(float a, float b) {
    934         return a + b;
    935     }
    936 
    937     /**
    938      * Returns the greater of two {@code float} values
    939      * as if by calling {@link Math#max(float, float) Math.max}.
    940      *
    941      * @param a the first operand
    942      * @param b the second operand
    943      * @return the greater of {@code a} and {@code b}
    944      * @see java.util.function.BinaryOperator
    945      * @since 1.8
    946      */
    947     public static float max(float a, float b) {
    948         return Math.max(a, b);
    949     }
    950 
    951     /**
    952      * Returns the smaller of two {@code float} values
    953      * as if by calling {@link Math#min(float, float) Math.min}.
    954      *
    955      * @param a the first operand
    956      * @param b the second operand
    957      * @return the smaller of {@code a} and {@code b}
    958      * @see java.util.function.BinaryOperator
    959      * @since 1.8
    960      */
    961     public static float min(float a, float b) {
    962         return Math.min(a, b);
    963     }
    964 
    965     /** use serialVersionUID from JDK 1.0.2 for interoperability */
    966     private static final long serialVersionUID = -2671257302660747028L;
    967 }
    968