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