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