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