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