1 /*
   2  * Copyright (c) 1994, 2024, 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.MethodHandles;
  29 import java.lang.constant.Constable;
  30 import java.lang.constant.ConstantDesc;
  31 import java.util.Optional;
  32 
  33 import jdk.internal.math.FloatConsts;
  34 import jdk.internal.math.FloatingDecimal;
  35 import jdk.internal.math.FloatToDecimal;
  36 import jdk.internal.value.DeserializeConstructor;
  37 import jdk.internal.vm.annotation.IntrinsicCandidate;
  38 
  39 /**
  40  * The {@code Float} class wraps a value of primitive type
  41  * {@code float} in an object. An object of type
  42  * {@code Float} contains a single field whose type is
  43  * {@code float}.
  44  *
  45  * <p>In addition, this class provides several methods for converting a
  46  * {@code float} to a {@code String} and a
  47  * {@code String} to a {@code float}, as well as other
  48  * constants and methods useful when dealing with a
  49  * {@code float}.
  50  *
  51  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
  52  * class; programmers should treat instances that are
  53  * {@linkplain #equals(Object) equal} as interchangeable and should not
  54  * use instances for synchronization, or unpredictable behavior may
  55  * occur. For example, in a future release, synchronization may fail.
  56  *
  57  * <h2><a id=equivalenceRelation>Floating-point Equality, Equivalence,
  58  * and Comparison</a></h2>
  59  *
  60  * The class {@code java.lang.Double} has a {@linkplain
  61  * Double##equivalenceRelation discussion of equality,
  62  * equivalence, and comparison of floating-point values} that is
  63  * equally applicable to {@code float} values.
  64  *
  65  * <h2><a id=decimalToBinaryConversion>Decimal &harr; Binary Conversion Issues</a></h2>
  66  *
  67  * The {@linkplain Double##decimalToBinaryConversion discussion of binary to
  68  * decimal conversion issues} in {@code java.lang.Double} is also
  69  * applicable to {@code float} values.
  70  *
  71  * @see <a href="https://standards.ieee.org/ieee/754/6210/">
  72  *      <cite>IEEE Standard for Floating-Point Arithmetic</cite></a>
  73  *
  74  * @author  Lee Boynton
  75  * @author  Arthur van Hoff
  76  * @author  Joseph D. Darcy
  77  * @since 1.0
  78  */
  79 @jdk.internal.MigratedValueClass
  80 @jdk.internal.ValueBased
  81 public final class Float extends Number
  82         implements Comparable<Float>, Constable, ConstantDesc {
  83     /**
  84      * A constant holding the positive infinity of type
  85      * {@code float}. It is equal to the value returned by
  86      * {@code Float.intBitsToFloat(0x7f800000)}.
  87      */
  88     public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
  89 
  90     /**
  91      * A constant holding the negative infinity of type
  92      * {@code float}. It is equal to the value returned by
  93      * {@code Float.intBitsToFloat(0xff800000)}.
  94      */
  95     public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
  96 
  97     /**
  98      * A constant holding a Not-a-Number (NaN) value of type
  99      * {@code float}.  It is equivalent to the value returned by
 100      * {@code Float.intBitsToFloat(0x7fc00000)}.
 101      */
 102     public static final float NaN = 0.0f / 0.0f;
 103 
 104     /**
 105      * A constant holding the largest positive finite value of type
 106      * {@code float}, (2-2<sup>-23</sup>)&middot;2<sup>127</sup>.
 107      * It is equal to the hexadecimal floating-point literal
 108      * {@code 0x1.fffffeP+127f} and also equal to
 109      * {@code Float.intBitsToFloat(0x7f7fffff)}.
 110      */
 111     public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f
 112 
 113     /**
 114      * A constant holding the smallest positive normal value of type
 115      * {@code float}, 2<sup>-126</sup>.  It is equal to the
 116      * hexadecimal floating-point literal {@code 0x1.0p-126f} and also
 117      * equal to {@code Float.intBitsToFloat(0x00800000)}.
 118      *
 119      * @since 1.6
 120      */
 121     public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f
 122 
 123     /**
 124      * A constant holding the smallest positive nonzero value of type
 125      * {@code float}, 2<sup>-149</sup>. It is equal to the
 126      * hexadecimal floating-point literal {@code 0x0.000002P-126f}
 127      * and also equal to {@code Float.intBitsToFloat(0x1)}.
 128      */
 129     public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f
 130 
 131     /**
 132      * The number of bits used to represent a {@code float} value,
 133      * {@value}.
 134      *
 135      * @since 1.5
 136      */
 137     public static final int SIZE = 32;
 138 
 139     /**
 140      * The number of bits in the significand of a {@code float} value,
 141      * {@value}.  This is the parameter N in section {@jls 4.2.3} of
 142      * <cite>The Java Language Specification</cite>.
 143      *
 144      * @since 19
 145      */
 146     public static final int PRECISION = 24;
 147 
 148     /**
 149      * Maximum exponent a finite {@code float} variable may have,
 150      * {@value}.  It is equal to the value returned by {@code
 151      * Math.getExponent(Float.MAX_VALUE)}.
 152      *
 153      * @since 1.6
 154      */
 155     public static final int MAX_EXPONENT = (1 << (SIZE - PRECISION - 1)) - 1; // 127
 156 
 157     /**
 158      * Minimum exponent a normalized {@code float} variable may have,
 159      * {@value}.  It is equal to the value returned by {@code
 160      * Math.getExponent(Float.MIN_NORMAL)}.
 161      *
 162      * @since 1.6
 163      */
 164     public static final int MIN_EXPONENT = 1 - MAX_EXPONENT; // -126
 165 
 166     /**
 167      * The number of bytes used to represent a {@code float} value,
 168      * {@value}.
 169      *
 170      * @since 1.8
 171      */
 172     public static final int BYTES = SIZE / Byte.SIZE;
 173 
 174     /**
 175      * The {@code Class} instance representing the primitive type
 176      * {@code float}.
 177      *
 178      * @since 1.1
 179      */
 180     @SuppressWarnings("unchecked")
 181     public static final Class<Float> TYPE = (Class<Float>) Class.getPrimitiveClass("float");
 182 
 183     /**
 184      * Returns a string representation of the {@code float}
 185      * argument. All characters mentioned below are ASCII characters.
 186      * <ul>
 187      * <li>If the argument is NaN, the result is the string
 188      * "{@code NaN}".
 189      * <li>Otherwise, the result is a string that represents the sign and
 190      *     magnitude (absolute value) of the argument. If the sign is
 191      *     negative, the first character of the result is
 192      *     '{@code -}' ({@code '\u005Cu002D'}); if the sign is
 193      *     positive, no sign character appears in the result. As for
 194      *     the magnitude <i>m</i>:
 195      * <ul>
 196      * <li>If <i>m</i> is infinity, it is represented by the characters
 197      *     {@code "Infinity"}; thus, positive infinity produces
 198      *     the result {@code "Infinity"} and negative infinity
 199      *     produces the result {@code "-Infinity"}.
 200      * <li>If <i>m</i> is zero, it is represented by the characters
 201      *     {@code "0.0"}; thus, negative zero produces the result
 202      *     {@code "-0.0"} and positive zero produces the result
 203      *     {@code "0.0"}.
 204      *
 205      * <li> Otherwise <i>m</i> is positive and finite.
 206      * It is converted to a string in two stages:
 207      * <ul>
 208      * <li> <em>Selection of a decimal</em>:
 209      * A well-defined decimal <i>d</i><sub><i>m</i></sub>
 210      * is selected to represent <i>m</i>.
 211      * This decimal is (almost always) the <em>shortest</em> one that
 212      * rounds to <i>m</i> according to the round to nearest
 213      * rounding policy of IEEE 754 floating-point arithmetic.
 214      * <li> <em>Formatting as a string</em>:
 215      * The decimal <i>d</i><sub><i>m</i></sub> is formatted as a string,
 216      * either in plain or in computerized scientific notation,
 217      * depending on its value.
 218      * </ul>
 219      * </ul>
 220      * </ul>
 221      *
 222      * <p>A <em>decimal</em> is a number of the form
 223      * <i>s</i>&times;10<sup><i>i</i></sup>
 224      * for some (unique) integers <i>s</i> &gt; 0 and <i>i</i> such that
 225      * <i>s</i> is not a multiple of 10.
 226      * These integers are the <em>significand</em> and
 227      * the <em>exponent</em>, respectively, of the decimal.
 228      * The <em>length</em> of the decimal is the (unique)
 229      * positive integer <i>n</i> meeting
 230      * 10<sup><i>n</i>-1</sup> &le; <i>s</i> &lt; 10<sup><i>n</i></sup>.
 231      *
 232      * <p>The decimal <i>d</i><sub><i>m</i></sub> for a finite positive <i>m</i>
 233      * is defined as follows:
 234      * <ul>
 235      * <li>Let <i>R</i> be the set of all decimals that round to <i>m</i>
 236      * according to the usual <em>round to nearest</em> rounding policy of
 237      * IEEE 754 floating-point arithmetic.
 238      * <li>Let <i>p</i> be the minimal length over all decimals in <i>R</i>.
 239      * <li>When <i>p</i> &ge; 2, let <i>T</i> be the set of all decimals
 240      * in <i>R</i> with length <i>p</i>.
 241      * Otherwise, let <i>T</i> be the set of all decimals
 242      * in <i>R</i> with length 1 or 2.
 243      * <li>Define <i>d</i><sub><i>m</i></sub> as the decimal in <i>T</i>
 244      * that is closest to <i>m</i>.
 245      * Or if there are two such decimals in <i>T</i>,
 246      * select the one with the even significand.
 247      * </ul>
 248      *
 249      * <p>The (uniquely) selected decimal <i>d</i><sub><i>m</i></sub>
 250      * is then formatted.
 251      * Let <i>s</i>, <i>i</i> and <i>n</i> be the significand, exponent and
 252      * length of <i>d</i><sub><i>m</i></sub>, respectively.
 253      * Further, let <i>e</i> = <i>n</i> + <i>i</i> - 1 and let
 254      * <i>s</i><sub>1</sub>&hellip;<i>s</i><sub><i>n</i></sub>
 255      * be the usual decimal expansion of <i>s</i>.
 256      * Note that <i>s</i><sub>1</sub> &ne; 0
 257      * and <i>s</i><sub><i>n</i></sub> &ne; 0.
 258      * Below, the decimal point {@code '.'} is {@code '\u005Cu002E'}
 259      * and the exponent indicator {@code 'E'} is {@code '\u005Cu0045'}.
 260      * <ul>
 261      * <li>Case -3 &le; <i>e</i> &lt; 0:
 262      * <i>d</i><sub><i>m</i></sub> is formatted as
 263      * <code>0.0</code>&hellip;<code>0</code><!--
 264      * --><i>s</i><sub>1</sub>&hellip;<i>s</i><sub><i>n</i></sub>,
 265      * where there are exactly -(<i>n</i> + <i>i</i>) zeroes between
 266      * the decimal point and <i>s</i><sub>1</sub>.
 267      * For example, 123 &times; 10<sup>-4</sup> is formatted as
 268      * {@code 0.0123}.
 269      * <li>Case 0 &le; <i>e</i> &lt; 7:
 270      * <ul>
 271      * <li>Subcase <i>i</i> &ge; 0:
 272      * <i>d</i><sub><i>m</i></sub> is formatted as
 273      * <i>s</i><sub>1</sub>&hellip;<i>s</i><sub><i>n</i></sub><!--
 274      * --><code>0</code>&hellip;<code>0.0</code>,
 275      * where there are exactly <i>i</i> zeroes
 276      * between <i>s</i><sub><i>n</i></sub> and the decimal point.
 277      * For example, 123 &times; 10<sup>2</sup> is formatted as
 278      * {@code 12300.0}.
 279      * <li>Subcase <i>i</i> &lt; 0:
 280      * <i>d</i><sub><i>m</i></sub> is formatted as
 281      * <i>s</i><sub>1</sub>&hellip;<!--
 282      * --><i>s</i><sub><i>n</i>+<i>i</i></sub><code>.</code><!--
 283      * --><i>s</i><sub><i>n</i>+<i>i</i>+1</sub>&hellip;<!--
 284      * --><i>s</i><sub><i>n</i></sub>,
 285      * where there are exactly -<i>i</i> digits to the right of
 286      * the decimal point.
 287      * For example, 123 &times; 10<sup>-1</sup> is formatted as
 288      * {@code 12.3}.
 289      * </ul>
 290      * <li>Case <i>e</i> &lt; -3 or <i>e</i> &ge; 7:
 291      * computerized scientific notation is used to format
 292      * <i>d</i><sub><i>m</i></sub>.
 293      * Here <i>e</i> is formatted as by {@link Integer#toString(int)}.
 294      * <ul>
 295      * <li>Subcase <i>n</i> = 1:
 296      * <i>d</i><sub><i>m</i></sub> is formatted as
 297      * <i>s</i><sub>1</sub><code>.0E</code><i>e</i>.
 298      * For example, 1 &times; 10<sup>23</sup> is formatted as
 299      * {@code 1.0E23}.
 300      * <li>Subcase <i>n</i> &gt; 1:
 301      * <i>d</i><sub><i>m</i></sub> is formatted as
 302      * <i>s</i><sub>1</sub><code>.</code><i>s</i><sub>2</sub><!--
 303      * -->&hellip;<i>s</i><sub><i>n</i></sub><code>E</code><i>e</i>.
 304      * For example, 123 &times; 10<sup>-21</sup> is formatted as
 305      * {@code 1.23E-19}.
 306      * </ul>
 307      * </ul>
 308      *
 309      * <p>To create localized string representations of a floating-point
 310      * value, use subclasses of {@link java.text.NumberFormat}.
 311      *
 312      * @apiNote
 313      * This method corresponds to the general functionality of the
 314      * convertToDecimalCharacter operation defined in IEEE 754;
 315      * however, that operation is defined in terms of specifying the
 316      * number of significand digits used in the conversion.
 317      * Code to do such a conversion in the Java platform includes
 318      * converting the {@code float} to a {@link java.math.BigDecimal
 319      * BigDecimal} exactly and then rounding the {@code BigDecimal} to
 320      * the desired number of digits; sample code:
 321      * {@snippet lang=java :
 322      * floatf = 0.1f;
 323      * int digits = 15;
 324      * BigDecimal bd = new BigDecimal(f);
 325      * String result = bd.round(new MathContext(digits,  RoundingMode.HALF_UP));
 326      * // 0.100000001490116
 327      * }
 328      *
 329      * @param   f   the {@code float} to be converted.
 330      * @return a string representation of the argument.
 331      */
 332     public static String toString(float f) {
 333         return FloatToDecimal.toString(f);
 334     }
 335 
 336     /**
 337      * Returns a hexadecimal string representation of the
 338      * {@code float} argument. All characters mentioned below are
 339      * ASCII characters.
 340      *
 341      * <ul>
 342      * <li>If the argument is NaN, the result is the string
 343      *     "{@code NaN}".
 344      * <li>Otherwise, the result is a string that represents the sign and
 345      * magnitude (absolute value) of the argument. If the sign is negative,
 346      * the first character of the result is '{@code -}'
 347      * ({@code '\u005Cu002D'}); if the sign is positive, no sign character
 348      * appears in the result. As for the magnitude <i>m</i>:
 349      *
 350      * <ul>
 351      * <li>If <i>m</i> is infinity, it is represented by the string
 352      * {@code "Infinity"}; thus, positive infinity produces the
 353      * result {@code "Infinity"} and negative infinity produces
 354      * the result {@code "-Infinity"}.
 355      *
 356      * <li>If <i>m</i> is zero, it is represented by the string
 357      * {@code "0x0.0p0"}; thus, negative zero produces the result
 358      * {@code "-0x0.0p0"} and positive zero produces the result
 359      * {@code "0x0.0p0"}.
 360      *
 361      * <li>If <i>m</i> is a {@code float} value with a
 362      * normalized representation, substrings are used to represent the
 363      * significand and exponent fields.  The significand is
 364      * represented by the characters {@code "0x1."}
 365      * followed by a lowercase hexadecimal representation of the rest
 366      * of the significand as a fraction.  Trailing zeros in the
 367      * hexadecimal representation are removed unless all the digits
 368      * are zero, in which case a single zero is used. Next, the
 369      * exponent is represented by {@code "p"} followed
 370      * by a decimal string of the unbiased exponent as if produced by
 371      * a call to {@link Integer#toString(int) Integer.toString} on the
 372      * exponent value.
 373      *
 374      * <li>If <i>m</i> is a {@code float} value with a subnormal
 375      * representation, the significand is represented by the
 376      * characters {@code "0x0."} followed by a
 377      * hexadecimal representation of the rest of the significand as a
 378      * fraction.  Trailing zeros in the hexadecimal representation are
 379      * removed. Next, the exponent is represented by
 380      * {@code "p-126"}.  Note that there must be at
 381      * least one nonzero digit in a subnormal significand.
 382      *
 383      * </ul>
 384      *
 385      * </ul>
 386      *
 387      * <table class="striped">
 388      * <caption>Examples</caption>
 389      * <thead>
 390      * <tr><th scope="col">Floating-point Value</th><th scope="col">Hexadecimal String</th>
 391      * </thead>
 392      * <tbody>
 393      * <tr><th scope="row">{@code 1.0}</th> <td>{@code 0x1.0p0}</td>
 394      * <tr><th scope="row">{@code -1.0}</th>        <td>{@code -0x1.0p0}</td>
 395      * <tr><th scope="row">{@code 2.0}</th> <td>{@code 0x1.0p1}</td>
 396      * <tr><th scope="row">{@code 3.0}</th> <td>{@code 0x1.8p1}</td>
 397      * <tr><th scope="row">{@code 0.5}</th> <td>{@code 0x1.0p-1}</td>
 398      * <tr><th scope="row">{@code 0.25}</th>        <td>{@code 0x1.0p-2}</td>
 399      * <tr><th scope="row">{@code Float.MAX_VALUE}</th>
 400      *     <td>{@code 0x1.fffffep127}</td>
 401      * <tr><th scope="row">{@code Minimum Normal Value}</th>
 402      *     <td>{@code 0x1.0p-126}</td>
 403      * <tr><th scope="row">{@code Maximum Subnormal Value}</th>
 404      *     <td>{@code 0x0.fffffep-126}</td>
 405      * <tr><th scope="row">{@code Float.MIN_VALUE}</th>
 406      *     <td>{@code 0x0.000002p-126}</td>
 407      * </tbody>
 408      * </table>
 409      *
 410      * @apiNote
 411      * This method corresponds to the convertToHexCharacter operation
 412      * defined in IEEE 754.
 413      *
 414      * @param   f   the {@code float} to be converted.
 415      * @return a hex string representation of the argument.
 416      * @since 1.5
 417      * @author Joseph D. Darcy
 418      */
 419     public static String toHexString(float f) {
 420         if (Math.abs(f) < Float.MIN_NORMAL
 421             &&  f != 0.0f ) {// float subnormal
 422             // Adjust exponent to create subnormal double, then
 423             // replace subnormal double exponent with subnormal float
 424             // exponent
 425             String s = Double.toHexString(Math.scalb((double)f,
 426                                                      /* -1022+126 */
 427                                                      Double.MIN_EXPONENT-
 428                                                      Float.MIN_EXPONENT));
 429             return s.replaceFirst("p-1022$", "p-126");
 430         }
 431         else // double string will be the same as float string
 432             return Double.toHexString(f);
 433     }
 434 
 435     /**
 436      * Returns a {@code Float} object holding the
 437      * {@code float} value represented by the argument string
 438      * {@code s}.
 439      *
 440      * <p>If {@code s} is {@code null}, then a
 441      * {@code NullPointerException} is thrown.
 442      *
 443      * <p>Leading and trailing whitespace characters in {@code s}
 444      * are ignored.  Whitespace is removed as if by the {@link
 445      * String#trim} method; that is, both ASCII space and control
 446      * characters are removed. The rest of {@code s} should
 447      * constitute a <i>FloatValue</i> as described by the lexical
 448      * syntax rules:
 449      *
 450      * <blockquote>
 451      * <dl>
 452      * <dt><i>FloatValue:</i>
 453      * <dd><i>Sign<sub>opt</sub></i> {@code NaN}
 454      * <dd><i>Sign<sub>opt</sub></i> {@code Infinity}
 455      * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
 456      * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
 457      * <dd><i>SignedInteger</i>
 458      * </dl>
 459      *
 460      * <dl>
 461      * <dt><i>HexFloatingPointLiteral</i>:
 462      * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
 463      * </dl>
 464      *
 465      * <dl>
 466      * <dt><i>HexSignificand:</i>
 467      * <dd><i>HexNumeral</i>
 468      * <dd><i>HexNumeral</i> {@code .}
 469      * <dd>{@code 0x} <i>HexDigits<sub>opt</sub>
 470      *     </i>{@code .}<i> HexDigits</i>
 471      * <dd>{@code 0X}<i> HexDigits<sub>opt</sub>
 472      *     </i>{@code .} <i>HexDigits</i>
 473      * </dl>
 474      *
 475      * <dl>
 476      * <dt><i>BinaryExponent:</i>
 477      * <dd><i>BinaryExponentIndicator SignedInteger</i>
 478      * </dl>
 479      *
 480      * <dl>
 481      * <dt><i>BinaryExponentIndicator:</i>
 482      * <dd>{@code p}
 483      * <dd>{@code P}
 484      * </dl>
 485      *
 486      * </blockquote>
 487      *
 488      * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
 489      * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
 490      * <i>FloatTypeSuffix</i> are as defined in the lexical structure
 491      * sections of
 492      * <cite>The Java Language Specification</cite>,
 493      * except that underscores are not accepted between digits.
 494      * If {@code s} does not have the form of
 495      * a <i>FloatValue</i>, then a {@code NumberFormatException}
 496      * is thrown. Otherwise, {@code s} is regarded as
 497      * representing an exact decimal value in the usual
 498      * "computerized scientific notation" or as an exact
 499      * hexadecimal value; this exact numerical value is then
 500      * conceptually converted to an "infinitely precise"
 501      * binary value that is then rounded to type {@code float}
 502      * by the usual round-to-nearest rule of IEEE 754 floating-point
 503      * arithmetic, which includes preserving the sign of a zero
 504      * value.
 505      *
 506      * Note that the round-to-nearest rule also implies overflow and
 507      * underflow behaviour; if the exact value of {@code s} is large
 508      * enough in magnitude (greater than or equal to ({@link
 509      * #MAX_VALUE} + {@link Math#ulp(float) ulp(MAX_VALUE)}/2),
 510      * rounding to {@code float} will result in an infinity and if the
 511      * exact value of {@code s} is small enough in magnitude (less
 512      * than or equal to {@link #MIN_VALUE}/2), rounding to float will
 513      * result in a zero.
 514      *
 515      * Finally, after rounding a {@code Float} object representing
 516      * this {@code float} value is returned.
 517      *
 518      * <p>Note that trailing format specifiers, specifiers that
 519      * determine the type of a floating-point literal
 520      * ({@code 1.0f} is a {@code float} value;
 521      * {@code 1.0d} is a {@code double} value), do
 522      * <em>not</em> influence the results of this method.  In other
 523      * words, the numerical value of the input string is converted
 524      * directly to the target floating-point type.  In general, the
 525      * two-step sequence of conversions, string to {@code double}
 526      * followed by {@code double} to {@code float}, is
 527      * <em>not</em> equivalent to converting a string directly to
 528      * {@code float}.  For example, if first converted to an
 529      * intermediate {@code double} and then to
 530      * {@code float}, the string<br>
 531      * {@code "1.00000017881393421514957253748434595763683319091796875001d"}<br>
 532      * results in the {@code float} value
 533      * {@code 1.0000002f}; if the string is converted directly to
 534      * {@code float}, <code>1.000000<b>1</b>f</code> results.
 535      *
 536      * <p>To avoid calling this method on an invalid string and having
 537      * a {@code NumberFormatException} be thrown, the documentation
 538      * for {@link Double#valueOf Double.valueOf} lists a regular
 539      * expression which can be used to screen the input.
 540      *
 541      * @apiNote To interpret localized string representations of a
 542      * floating-point value, or string representations that have
 543      * non-ASCII digits, use {@link java.text.NumberFormat}. For
 544      * example,
 545      * {@snippet lang="java" :
 546      *     NumberFormat.getInstance(l).parse(s).floatValue();
 547      * }
 548      * where {@code l} is the desired locale, or
 549      * {@link java.util.Locale#ROOT} if locale insensitive.
 550      *
 551      * @apiNote
 552      * This method corresponds to the convertFromDecimalCharacter and
 553      * convertFromHexCharacter operations defined in IEEE 754.
 554      *
 555      * @param   s   the string to be parsed.
 556      * @return  a {@code Float} object holding the value
 557      *          represented by the {@code String} argument.
 558      * @throws  NumberFormatException  if the string does not contain a
 559      *          parsable number.
 560      * @see Double##decimalToBinaryConversion Decimal &harr; Binary Conversion Issues
 561      */
 562     public static Float valueOf(String s) throws NumberFormatException {
 563         return new Float(parseFloat(s));
 564     }
 565 
 566     /**
 567      * Returns a {@code Float} instance representing the specified
 568      * {@code float} value.
 569      * If a new {@code Float} instance is not required, this method
 570      * should generally be used in preference to the constructor
 571      * {@link #Float(float)}, as this method is likely to yield
 572      * significantly better space and time performance by caching
 573      * frequently requested values.
 574      *
 575      * @param  f a float value.
 576      * @return a {@code Float} instance representing {@code f}.
 577      * @since  1.5
 578      */
 579     @IntrinsicCandidate
 580     @DeserializeConstructor
 581     public static Float valueOf(float f) {
 582         return new Float(f);
 583     }
 584 
 585     /**
 586      * Returns a new {@code float} initialized to the value
 587      * represented by the specified {@code String}, as performed
 588      * by the {@code valueOf} method of class {@code Float}.
 589      *
 590      * @param  s the string to be parsed.
 591      * @return the {@code float} value represented by the string
 592      *         argument.
 593      * @throws NullPointerException  if the string is null
 594      * @throws NumberFormatException if the string does not contain a
 595      *               parsable {@code float}.
 596      * @see    java.lang.Float#valueOf(String)
 597      * @see    Double##decimalToBinaryConversion Decimal &harr; Binary Conversion Issues
 598      * @since 1.2
 599      */
 600     public static float parseFloat(String s) throws NumberFormatException {
 601         return FloatingDecimal.parseFloat(s);
 602     }
 603 
 604     /**
 605      * Returns {@code true} if the specified number is a
 606      * Not-a-Number (NaN) value, {@code false} otherwise.
 607      *
 608      * @apiNote
 609      * This method corresponds to the isNaN operation defined in IEEE
 610      * 754.
 611      *
 612      * @param   v   the value to be tested.
 613      * @return  {@code true} if the argument is NaN;
 614      *          {@code false} otherwise.
 615      */
 616     public static boolean isNaN(float v) {
 617         return (v != v);
 618     }
 619 
 620     /**
 621      * Returns {@code true} if the specified number is infinitely
 622      * large in magnitude, {@code false} otherwise.
 623      *
 624      * @apiNote
 625      * This method corresponds to the isInfinite operation defined in
 626      * IEEE 754.
 627      *
 628      * @param   v   the value to be tested.
 629      * @return  {@code true} if the argument is positive infinity or
 630      *          negative infinity; {@code false} otherwise.
 631      */
 632     @IntrinsicCandidate
 633     public static boolean isInfinite(float v) {
 634         return Math.abs(v) > MAX_VALUE;
 635     }
 636 
 637 
 638     /**
 639      * Returns {@code true} if the argument is a finite floating-point
 640      * value; returns {@code false} otherwise (for NaN and infinity
 641      * arguments).
 642      *
 643      * @apiNote
 644      * This method corresponds to the isFinite operation defined in
 645      * IEEE 754.
 646      *
 647      * @param f the {@code float} value to be tested
 648      * @return {@code true} if the argument is a finite
 649      * floating-point value, {@code false} otherwise.
 650      * @since 1.8
 651      */
 652      @IntrinsicCandidate
 653      public static boolean isFinite(float f) {
 654         return Math.abs(f) <= Float.MAX_VALUE;
 655     }
 656 
 657     /**
 658      * The value of the Float.
 659      *
 660      * @serial
 661      */
 662     private final float value;
 663 
 664     /**
 665      * Constructs a newly allocated {@code Float} object that
 666      * represents the primitive {@code float} argument.
 667      *
 668      * @param   value   the value to be represented by the {@code Float}.
 669      *
 670      * @deprecated
 671      * It is rarely appropriate to use this constructor. The static factory
 672      * {@link #valueOf(float)} is generally a better choice, as it is
 673      * likely to yield significantly better space and time performance.
 674      */
 675     @Deprecated(since="9", forRemoval = true)
 676     public Float(float value) {
 677         this.value = value;
 678     }
 679 
 680     /**
 681      * Constructs a newly allocated {@code Float} object that
 682      * represents the argument converted to type {@code float}.
 683      *
 684      * @param   value   the value to be represented by the {@code Float}.
 685      *
 686      * @deprecated
 687      * It is rarely appropriate to use this constructor. Instead, use the
 688      * static factory method {@link #valueOf(float)} method as follows:
 689      * {@code Float.valueOf((float)value)}.
 690      */
 691     @Deprecated(since="9", forRemoval = true)
 692     public Float(double value) {
 693         this.value = (float)value;
 694     }
 695 
 696     /**
 697      * Constructs a newly allocated {@code Float} object that
 698      * represents the floating-point value of type {@code float}
 699      * represented by the string. The string is converted to a
 700      * {@code float} value as if by the {@code valueOf} method.
 701      *
 702      * @param   s   a string to be converted to a {@code Float}.
 703      * @throws      NumberFormatException if the string does not contain a
 704      *              parsable number.
 705      *
 706      * @deprecated
 707      * It is rarely appropriate to use this constructor.
 708      * Use {@link #parseFloat(String)} to convert a string to a
 709      * {@code float} primitive, or use {@link #valueOf(String)}
 710      * to convert a string to a {@code Float} object.
 711      */
 712     @Deprecated(since="9", forRemoval = true)
 713     public Float(String s) throws NumberFormatException {
 714         value = parseFloat(s);
 715     }
 716 
 717     /**
 718      * Returns {@code true} if this {@code Float} value is a
 719      * Not-a-Number (NaN), {@code false} otherwise.
 720      *
 721      * @return  {@code true} if the value represented by this object is
 722      *          NaN; {@code false} otherwise.
 723      */
 724     public boolean isNaN() {
 725         return isNaN(value);
 726     }
 727 
 728     /**
 729      * Returns {@code true} if this {@code Float} value is
 730      * infinitely large in magnitude, {@code false} otherwise.
 731      *
 732      * @return  {@code true} if the value represented by this object is
 733      *          positive infinity or negative infinity;
 734      *          {@code false} otherwise.
 735      */
 736     public boolean isInfinite() {
 737         return isInfinite(value);
 738     }
 739 
 740     /**
 741      * Returns a string representation of this {@code Float} object.
 742      * The primitive {@code float} value represented by this object
 743      * is converted to a {@code String} exactly as if by the method
 744      * {@code toString} of one argument.
 745      *
 746      * @return  a {@code String} representation of this object.
 747      * @see java.lang.Float#toString(float)
 748      */
 749     public String toString() {
 750         return Float.toString(value);
 751     }
 752 
 753     /**
 754      * Returns the value of this {@code Float} as a {@code byte} after
 755      * a narrowing primitive conversion.
 756      *
 757      * @return  the {@code float} value represented by this object
 758      *          converted to type {@code byte}
 759      * @jls 5.1.3 Narrowing Primitive Conversion
 760      */
 761     @Override
 762     public byte byteValue() {
 763         return (byte)value;
 764     }
 765 
 766     /**
 767      * Returns the value of this {@code Float} as a {@code short}
 768      * after a narrowing primitive conversion.
 769      *
 770      * @return  the {@code float} value represented by this object
 771      *          converted to type {@code short}
 772      * @jls 5.1.3 Narrowing Primitive Conversion
 773      * @since 1.1
 774      */
 775     @Override
 776     public short shortValue() {
 777         return (short)value;
 778     }
 779 
 780     /**
 781      * Returns the value of this {@code Float} as an {@code int} after
 782      * a narrowing primitive conversion.
 783      *
 784      * @apiNote
 785      * This method corresponds to the convertToIntegerTowardZero
 786      * operation defined in IEEE 754.
 787      *
 788      * @return  the {@code float} value represented by this object
 789      *          converted to type {@code int}
 790      * @jls 5.1.3 Narrowing Primitive Conversion
 791      */
 792     @Override
 793     public int intValue() {
 794         return (int)value;
 795     }
 796 
 797     /**
 798      * Returns value of this {@code Float} as a {@code long} after a
 799      * narrowing primitive conversion.
 800      *
 801      * @apiNote
 802      * This method corresponds to the convertToIntegerTowardZero
 803      * operation defined in IEEE 754.
 804      *
 805      * @return  the {@code float} value represented by this object
 806      *          converted to type {@code long}
 807      * @jls 5.1.3 Narrowing Primitive Conversion
 808      */
 809     @Override
 810     public long longValue() {
 811         return (long)value;
 812     }
 813 
 814     /**
 815      * Returns the {@code float} value of this {@code Float} object.
 816      *
 817      * @return the {@code float} value represented by this object
 818      */
 819     @Override
 820     @IntrinsicCandidate
 821     public float floatValue() {
 822         return value;
 823     }
 824 
 825     /**
 826      * Returns the value of this {@code Float} as a {@code double}
 827      * after a widening primitive conversion.
 828      *
 829      * @apiNote
 830      * This method corresponds to the convertFormat operation defined
 831      * in IEEE 754.
 832      *
 833      * @return the {@code float} value represented by this
 834      *         object converted to type {@code double}
 835      * @jls 5.1.2 Widening Primitive Conversion
 836      */
 837     @Override
 838     public double doubleValue() {
 839         return (double)value;
 840     }
 841 
 842     /**
 843      * Returns a hash code for this {@code Float} object. The
 844      * result is the integer bit representation, exactly as produced
 845      * by the method {@link #floatToIntBits(float)}, of the primitive
 846      * {@code float} value represented by this {@code Float}
 847      * object.
 848      *
 849      * @return a hash code value for this object.
 850      */
 851     @Override
 852     public int hashCode() {
 853         return Float.hashCode(value);
 854     }
 855 
 856     /**
 857      * Returns a hash code for a {@code float} value; compatible with
 858      * {@code Float.hashCode()}.
 859      *
 860      * @param value the value to hash
 861      * @return a hash code value for a {@code float} value.
 862      * @since 1.8
 863      */
 864     public static int hashCode(float value) {
 865         return floatToIntBits(value);
 866     }
 867 
 868     /**
 869      * Compares this object against the specified object.  The result
 870      * is {@code true} if and only if the argument is not
 871      * {@code null} and is a {@code Float} object that
 872      * represents a {@code float} with the same value as the
 873      * {@code float} represented by this object. For this
 874      * purpose, two {@code float} values are considered to be the
 875      * same if and only if the method {@link #floatToIntBits(float)}
 876      * returns the identical {@code int} value when applied to
 877      * each.
 878      *
 879      * @apiNote
 880      * This method is defined in terms of {@link
 881      * #floatToIntBits(float)} rather than the {@code ==} operator on
 882      * {@code float} values since the {@code ==} operator does
 883      * <em>not</em> define an equivalence relation and to satisfy the
 884      * {@linkplain Object#equals equals contract} an equivalence
 885      * relation must be implemented; see {@linkplain Double##equivalenceRelation
 886      * this discussion for details of floating-point equality and equivalence}.
 887      *
 888      * @param obj the object to be compared
 889      * @return  {@code true} if the objects are the same;
 890      *          {@code false} otherwise.
 891      * @see java.lang.Float#floatToIntBits(float)
 892      * @jls 15.21.1 Numerical Equality Operators == and !=
 893      */
 894     public boolean equals(Object obj) {
 895         return (obj instanceof Float)
 896                && (floatToIntBits(((Float)obj).value) == floatToIntBits(value));
 897     }
 898 
 899     /**
 900      * Returns a representation of the specified floating-point value
 901      * according to the IEEE 754 floating-point "single format" bit
 902      * layout.
 903      *
 904      * <p>Bit 31 (the bit that is selected by the mask
 905      * {@code 0x80000000}) represents the sign of the floating-point
 906      * number.
 907      * Bits 30-23 (the bits that are selected by the mask
 908      * {@code 0x7f800000}) represent the exponent.
 909      * Bits 22-0 (the bits that are selected by the mask
 910      * {@code 0x007fffff}) represent the significand (sometimes called
 911      * the mantissa) of the floating-point number.
 912      *
 913      * <p>If the argument is positive infinity, the result is
 914      * {@code 0x7f800000}.
 915      *
 916      * <p>If the argument is negative infinity, the result is
 917      * {@code 0xff800000}.
 918      *
 919      * <p>If the argument is NaN, the result is {@code 0x7fc00000}.
 920      *
 921      * <p>In all cases, the result is an integer that, when given to the
 922      * {@link #intBitsToFloat(int)} method, will produce a floating-point
 923      * value the same as the argument to {@code floatToIntBits}
 924      * (except all NaN values are collapsed to a single
 925      * "canonical" NaN value).
 926      *
 927      * @param   value   a floating-point number.
 928      * @return the bits that represent the floating-point number.
 929      */
 930     @IntrinsicCandidate
 931     public static int floatToIntBits(float value) {
 932         if (!isNaN(value)) {
 933             return floatToRawIntBits(value);
 934         }
 935         return 0x7fc00000;
 936     }
 937 
 938     /**
 939      * Returns a representation of the specified floating-point value
 940      * according to the IEEE 754 floating-point "single format" bit
 941      * layout, preserving Not-a-Number (NaN) values.
 942      *
 943      * <p>Bit 31 (the bit that is selected by the mask
 944      * {@code 0x80000000}) represents the sign of the floating-point
 945      * number.
 946      * Bits 30-23 (the bits that are selected by the mask
 947      * {@code 0x7f800000}) represent the exponent.
 948      * Bits 22-0 (the bits that are selected by the mask
 949      * {@code 0x007fffff}) represent the significand (sometimes called
 950      * the mantissa) of the floating-point number.
 951      *
 952      * <p>If the argument is positive infinity, the result is
 953      * {@code 0x7f800000}.
 954      *
 955      * <p>If the argument is negative infinity, the result is
 956      * {@code 0xff800000}.
 957      *
 958      * <p>If the argument is NaN, the result is the integer representing
 959      * the actual NaN value.  Unlike the {@code floatToIntBits}
 960      * method, {@code floatToRawIntBits} does not collapse all the
 961      * bit patterns encoding a NaN to a single "canonical"
 962      * NaN value.
 963      *
 964      * <p>In all cases, the result is an integer that, when given to the
 965      * {@link #intBitsToFloat(int)} method, will produce a
 966      * floating-point value the same as the argument to
 967      * {@code floatToRawIntBits}.
 968      *
 969      * @param   value   a floating-point number.
 970      * @return the bits that represent the floating-point number.
 971      * @since 1.3
 972      */
 973     @IntrinsicCandidate
 974     public static native int floatToRawIntBits(float value);
 975 
 976     /**
 977      * Returns the {@code float} value corresponding to a given
 978      * bit representation.
 979      * The argument is considered to be a representation of a
 980      * floating-point value according to the IEEE 754 floating-point
 981      * "single format" bit layout.
 982      *
 983      * <p>If the argument is {@code 0x7f800000}, the result is positive
 984      * infinity.
 985      *
 986      * <p>If the argument is {@code 0xff800000}, the result is negative
 987      * infinity.
 988      *
 989      * <p>If the argument is any value in the range
 990      * {@code 0x7f800001} through {@code 0x7fffffff} or in
 991      * the range {@code 0xff800001} through
 992      * {@code 0xffffffff}, the result is a NaN.  No IEEE 754
 993      * floating-point operation provided by Java can distinguish
 994      * between two NaN values of the same type with different bit
 995      * patterns.  Distinct values of NaN are only distinguishable by
 996      * use of the {@code Float.floatToRawIntBits} method.
 997      *
 998      * <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
 999      * values that can be computed from the argument:
1000      *
1001      * {@snippet lang="java" :
1002      * int s = ((bits >> 31) == 0) ? 1 : -1;
1003      * int e = ((bits >> 23) & 0xff);
1004      * int m = (e == 0) ?
1005      *                 (bits & 0x7fffff) << 1 :
1006      *                 (bits & 0x7fffff) | 0x800000;
1007      * }
1008      *
1009      * Then the floating-point result equals the value of the mathematical
1010      * expression <i>s</i>&middot;<i>m</i>&middot;2<sup><i>e</i>-150</sup>.
1011      *
1012      * <p>Note that this method may not be able to return a
1013      * {@code float} NaN with exactly same bit pattern as the
1014      * {@code int} argument.  IEEE 754 distinguishes between two
1015      * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>.  The
1016      * differences between the two kinds of NaN are generally not
1017      * visible in Java.  Arithmetic operations on signaling NaNs turn
1018      * them into quiet NaNs with a different, but often similar, bit
1019      * pattern.  However, on some processors merely copying a
1020      * signaling NaN also performs that conversion.  In particular,
1021      * copying a signaling NaN to return it to the calling method may
1022      * perform this conversion.  So {@code intBitsToFloat} may
1023      * not be able to return a {@code float} with a signaling NaN
1024      * bit pattern.  Consequently, for some {@code int} values,
1025      * {@code floatToRawIntBits(intBitsToFloat(start))} may
1026      * <i>not</i> equal {@code start}.  Moreover, which
1027      * particular bit patterns represent signaling NaNs is platform
1028      * dependent; although all NaN bit patterns, quiet or signaling,
1029      * must be in the NaN range identified above.
1030      *
1031      * @param   bits   an integer.
1032      * @return  the {@code float} floating-point value with the same bit
1033      *          pattern.
1034      */
1035     @IntrinsicCandidate
1036     public static native float intBitsToFloat(int bits);
1037 
1038     /**
1039      * {@return the {@code float} value closest to the numerical value
1040      * of the argument, a floating-point binary16 value encoded in a
1041      * {@code short}} The conversion is exact; all binary16 values can
1042      * be exactly represented in {@code float}.
1043      *
1044      * Special cases:
1045      * <ul>
1046      * <li> If the argument is zero, the result is a zero with the
1047      * same sign as the argument.
1048      * <li> If the argument is infinite, the result is an infinity
1049      * with the same sign as the argument.
1050      * <li> If the argument is a NaN, the result is a NaN.
1051      * </ul>
1052      *
1053      * <h4><a id=binary16Format>IEEE 754 binary16 format</a></h4>
1054      * The IEEE 754 standard defines binary16 as a 16-bit format, along
1055      * with the 32-bit binary32 format (corresponding to the {@code
1056      * float} type) and the 64-bit binary64 format (corresponding to
1057      * the {@code double} type). The binary16 format is similar to the
1058      * other IEEE 754 formats, except smaller, having all the usual
1059      * IEEE 754 values such as NaN, signed infinities, signed zeros,
1060      * and subnormals. The parameters (JLS {@jls 4.2.3}) for the
1061      * binary16 format are N = 11 precision bits, K = 5 exponent bits,
1062      * <i>E</i><sub><i>max</i></sub> = 15, and
1063      * <i>E</i><sub><i>min</i></sub> = -14.
1064      *
1065      * @apiNote
1066      * This method corresponds to the convertFormat operation defined
1067      * in IEEE 754 from the binary16 format to the binary32 format.
1068      * The operation of this method is analogous to a primitive
1069      * widening conversion (JLS {@jls 5.1.2}).
1070      *
1071      * @param floatBinary16 the binary16 value to convert to {@code float}
1072      * @since 20
1073      */
1074     @IntrinsicCandidate
1075     public static float float16ToFloat(short floatBinary16) {
1076         /*
1077          * The binary16 format has 1 sign bit, 5 exponent bits, and 10
1078          * significand bits. The exponent bias is 15.
1079          */
1080         int bin16arg = (int)floatBinary16;
1081         int bin16SignBit     = 0x8000 & bin16arg;
1082         int bin16ExpBits     = 0x7c00 & bin16arg;
1083         int bin16SignifBits  = 0x03FF & bin16arg;
1084 
1085         // Shift left difference in the number of significand bits in
1086         // the float and binary16 formats
1087         final int SIGNIF_SHIFT = (FloatConsts.SIGNIFICAND_WIDTH - 11);
1088 
1089         float sign = (bin16SignBit != 0) ? -1.0f : 1.0f;
1090 
1091         // Extract binary16 exponent, remove its bias, add in the bias
1092         // of a float exponent and shift to correct bit location
1093         // (significand width includes the implicit bit so shift one
1094         // less).
1095         int bin16Exp = (bin16ExpBits >> 10) - 15;
1096         if (bin16Exp == -15) {
1097             // For subnormal binary16 values and 0, the numerical
1098             // value is 2^24 * the significand as an integer (no
1099             // implicit bit).
1100             return sign * (0x1p-24f * bin16SignifBits);
1101         } else if (bin16Exp == 16) {
1102             return (bin16SignifBits == 0) ?
1103                 sign * Float.POSITIVE_INFINITY :
1104                 Float.intBitsToFloat((bin16SignBit << 16) |
1105                                      0x7f80_0000 |
1106                                      // Preserve NaN signif bits
1107                                      ( bin16SignifBits << SIGNIF_SHIFT ));
1108         }
1109 
1110         assert -15 < bin16Exp  && bin16Exp < 16;
1111 
1112         int floatExpBits = (bin16Exp + FloatConsts.EXP_BIAS)
1113             << (FloatConsts.SIGNIFICAND_WIDTH - 1);
1114 
1115         // Compute and combine result sign, exponent, and significand bits.
1116         return Float.intBitsToFloat((bin16SignBit << 16) |
1117                                     floatExpBits |
1118                                     (bin16SignifBits << SIGNIF_SHIFT));
1119     }
1120 
1121     /**
1122      * {@return the floating-point binary16 value, encoded in a {@code
1123      * short}, closest in value to the argument}
1124      * The conversion is computed under the {@linkplain
1125      * java.math.RoundingMode#HALF_EVEN round to nearest even rounding
1126      * mode}.
1127      *
1128      * Special cases:
1129      * <ul>
1130      * <li> If the argument is zero, the result is a zero with the
1131      * same sign as the argument.
1132      * <li> If the argument is infinite, the result is an infinity
1133      * with the same sign as the argument.
1134      * <li> If the argument is a NaN, the result is a NaN.
1135      * </ul>
1136      *
1137      * The {@linkplain ##binary16Format binary16 format} is discussed in
1138      * more detail in the {@link #float16ToFloat} method.
1139      *
1140      * @apiNote
1141      * This method corresponds to the convertFormat operation defined
1142      * in IEEE 754 from the binary32 format to the binary16 format.
1143      * The operation of this method is analogous to a primitive
1144      * narrowing conversion (JLS {@jls 5.1.3}).
1145      *
1146      * @param f the {@code float} value to convert to binary16
1147      * @since 20
1148      */
1149     @IntrinsicCandidate
1150     public static short floatToFloat16(float f) {
1151         int doppel = Float.floatToRawIntBits(f);
1152         short sign_bit = (short)((doppel & 0x8000_0000) >> 16);
1153 
1154         if (Float.isNaN(f)) {
1155             // Preserve sign and attempt to preserve significand bits
1156             return (short)(sign_bit
1157                     | 0x7c00 // max exponent + 1
1158                     // Preserve high order bit of float NaN in the
1159                     // binary16 result NaN (tenth bit); OR in remaining
1160                     // bits into lower 9 bits of binary 16 significand.
1161                     | (doppel & 0x007f_e000) >> 13 // 10 bits
1162                     | (doppel & 0x0000_1ff0) >> 4  //  9 bits
1163                     | (doppel & 0x0000_000f));     //  4 bits
1164         }
1165 
1166         float abs_f = Math.abs(f);
1167 
1168         // The overflow threshold is binary16 MAX_VALUE + 1/2 ulp
1169         if (abs_f >= (0x1.ffcp15f + 0x0.002p15f) ) {
1170             return (short)(sign_bit | 0x7c00); // Positive or negative infinity
1171         }
1172 
1173         // Smallest magnitude nonzero representable binary16 value
1174         // is equal to 0x1.0p-24; half-way and smaller rounds to zero.
1175         if (abs_f <= 0x1.0p-24f * 0.5f) { // Covers float zeros and subnormals.
1176             return sign_bit; // Positive or negative zero
1177         }
1178 
1179         // Dealing with finite values in exponent range of binary16
1180         // (when rounding is done, could still round up)
1181         int exp = Math.getExponent(f);
1182         assert -25 <= exp && exp <= 15;
1183 
1184         // For binary16 subnormals, beside forcing exp to -15, retain
1185         // the difference expdelta = E_min - exp.  This is the excess
1186         // shift value, in addition to 13, to be used in the
1187         // computations below.  Further the (hidden) msb with value 1
1188         // in f must be involved as well.
1189         int expdelta = 0;
1190         int msb = 0x0000_0000;
1191         if (exp < -14) {
1192             expdelta = -14 - exp;
1193             exp = -15;
1194             msb = 0x0080_0000;
1195         }
1196         int f_signif_bits = doppel & 0x007f_ffff | msb;
1197 
1198         // Significand bits as if using rounding to zero (truncation).
1199         short signif_bits = (short)(f_signif_bits >> (13 + expdelta));
1200 
1201         // For round to nearest even, determining whether or not to
1202         // round up (in magnitude) is a function of the least
1203         // significant bit (LSB), the next bit position (the round
1204         // position), and the sticky bit (whether there are any
1205         // nonzero bits in the exact result to the right of the round
1206         // digit). An increment occurs in three cases:
1207         //
1208         // LSB  Round Sticky
1209         // 0    1     1
1210         // 1    1     0
1211         // 1    1     1
1212         // See "Computer Arithmetic Algorithms," Koren, Table 4.9
1213 
1214         int lsb    = f_signif_bits & (1 << 13 + expdelta);
1215         int round  = f_signif_bits & (1 << 12 + expdelta);
1216         int sticky = f_signif_bits & ((1 << 12 + expdelta) - 1);
1217 
1218         if (round != 0 && ((lsb | sticky) != 0 )) {
1219             signif_bits++;
1220         }
1221 
1222         // No bits set in significand beyond the *first* exponent bit,
1223         // not just the significand; quantity is added to the exponent
1224         // to implement a carry out from rounding the significand.
1225         assert (0xf800 & signif_bits) == 0x0;
1226 
1227         return (short)(sign_bit | ( ((exp + 15) << 10) + signif_bits ) );
1228     }
1229 
1230     /**
1231      * Compares two {@code Float} objects numerically.
1232      *
1233      * This method imposes a total order on {@code Float} objects
1234      * with two differences compared to the incomplete order defined by
1235      * the Java language numerical comparison operators ({@code <, <=,
1236      * ==, >=, >}) on {@code float} values.
1237      *
1238      * <ul><li> A NaN is <em>unordered</em> with respect to other
1239      *          values and unequal to itself under the comparison
1240      *          operators.  This method chooses to define {@code
1241      *          Float.NaN} to be equal to itself and greater than all
1242      *          other {@code double} values (including {@code
1243      *          Float.POSITIVE_INFINITY}).
1244      *
1245      *      <li> Positive zero and negative zero compare equal
1246      *      numerically, but are distinct and distinguishable values.
1247      *      This method chooses to define positive zero ({@code +0.0f}),
1248      *      to be greater than negative zero ({@code -0.0f}).
1249      * </ul>
1250      *
1251      * This ensures that the <i>natural ordering</i> of {@code Float}
1252      * objects imposed by this method is <i>consistent with
1253      * equals</i>; see {@linkplain Double##equivalenceRelation this
1254      * discussion for details of floating-point comparison and
1255      * ordering}.
1256      *
1257      *
1258      * @param   anotherFloat   the {@code Float} to be compared.
1259      * @return  the value {@code 0} if {@code anotherFloat} is
1260      *          numerically equal to this {@code Float}; a value
1261      *          less than {@code 0} if this {@code Float}
1262      *          is numerically less than {@code anotherFloat};
1263      *          and a value greater than {@code 0} if this
1264      *          {@code Float} is numerically greater than
1265      *          {@code anotherFloat}.
1266      *
1267      * @jls 15.20.1 Numerical Comparison Operators {@code <}, {@code <=}, {@code >}, and {@code >=}
1268      * @since   1.2
1269      */
1270     @Override
1271     public int compareTo(Float anotherFloat) {
1272         return Float.compare(value, anotherFloat.value);
1273     }
1274 
1275     /**
1276      * Compares the two specified {@code float} values. The sign
1277      * of the integer value returned is the same as that of the
1278      * integer that would be returned by the call:
1279      * <pre>
1280      *    Float.valueOf(f1).compareTo(Float.valueOf(f2))
1281      * </pre>
1282      *
1283      * @param   f1        the first {@code float} to compare.
1284      * @param   f2        the second {@code float} to compare.
1285      * @return  the value {@code 0} if {@code f1} is
1286      *          numerically equal to {@code f2}; a value less than
1287      *          {@code 0} if {@code f1} is numerically less than
1288      *          {@code f2}; and a value greater than {@code 0}
1289      *          if {@code f1} is numerically greater than
1290      *          {@code f2}.
1291      * @since 1.4
1292      */
1293     public static int compare(float f1, float f2) {
1294         if (f1 < f2)
1295             return -1;           // Neither val is NaN, thisVal is smaller
1296         if (f1 > f2)
1297             return 1;            // Neither val is NaN, thisVal is larger
1298 
1299         // Cannot use floatToRawIntBits because of possibility of NaNs.
1300         int thisBits    = Float.floatToIntBits(f1);
1301         int anotherBits = Float.floatToIntBits(f2);
1302 
1303         return (thisBits == anotherBits ?  0 : // Values are equal
1304                 (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1305                  1));                          // (0.0, -0.0) or (NaN, !NaN)
1306     }
1307 
1308     /**
1309      * Adds two {@code float} values together as per the + operator.
1310      *
1311      * @apiNote This method corresponds to the addition operation
1312      * defined in IEEE 754.
1313      *
1314      * @param a the first operand
1315      * @param b the second operand
1316      * @return the sum of {@code a} and {@code b}
1317      * @jls 4.2.4 Floating-Point Operations
1318      * @see java.util.function.BinaryOperator
1319      * @since 1.8
1320      */
1321     public static float sum(float a, float b) {
1322         return a + b;
1323     }
1324 
1325     /**
1326      * Returns the greater of two {@code float} values
1327      * as if by calling {@link Math#max(float, float) Math.max}.
1328      *
1329      * @apiNote
1330      * This method corresponds to the maximum operation defined in
1331      * IEEE 754.
1332      *
1333      * @param a the first operand
1334      * @param b the second operand
1335      * @return the greater of {@code a} and {@code b}
1336      * @see java.util.function.BinaryOperator
1337      * @since 1.8
1338      */
1339     public static float max(float a, float b) {
1340         return Math.max(a, b);
1341     }
1342 
1343     /**
1344      * Returns the smaller of two {@code float} values
1345      * as if by calling {@link Math#min(float, float) Math.min}.
1346      *
1347      * @apiNote
1348      * This method corresponds to the minimum operation defined in
1349      * IEEE 754.
1350      *
1351      * @param a the first operand
1352      * @param b the second operand
1353      * @return the smaller of {@code a} and {@code b}
1354      * @see java.util.function.BinaryOperator
1355      * @since 1.8
1356      */
1357     public static float min(float a, float b) {
1358         return Math.min(a, b);
1359     }
1360 
1361     /**
1362      * Returns an {@link Optional} containing the nominal descriptor for this
1363      * instance, which is the instance itself.
1364      *
1365      * @return an {@link Optional} describing the {@linkplain Float} instance
1366      * @since 12
1367      */
1368     @Override
1369     public Optional<Float> describeConstable() {
1370         return Optional.of(this);
1371     }
1372 
1373     /**
1374      * Resolves this instance as a {@link ConstantDesc}, the result of which is
1375      * the instance itself.
1376      *
1377      * @param lookup ignored
1378      * @return the {@linkplain Float} instance
1379      * @since 12
1380      */
1381     @Override
1382     public Float resolveConstantDesc(MethodHandles.Lookup lookup) {
1383         return this;
1384     }
1385 
1386     /** use serialVersionUID from JDK 1.0.2 for interoperability */
1387     @java.io.Serial
1388     private static final long serialVersionUID = -2671257302660747028L;
1389 }