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