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