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