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