1 /* 2 * Copyright (c) 1994, 2021, 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 // BEGIN Android-removed: dynamic constants not supported on Android. 29 /* 30 import java.lang.invoke.MethodHandles; 31 import java.lang.constant.Constable; 32 import java.lang.constant.ConstantDesc; 33 import java.util.Optional; 34 */ 35 // END Android-removed: dynamic constants not supported on Android. 36 37 import jdk.internal.math.FloatingDecimal; 38 import jdk.internal.vm.annotation.IntrinsicCandidate; 39 40 /** 41 * The {@code Float} class wraps a value of primitive type 42 * {@code float} in an object. An object of type 43 * {@code Float} contains a single field whose type is 44 * {@code float}. 45 * 46 * <p>In addition, this class provides several methods for converting a 47 * {@code float} to a {@code String} and a 48 * {@code String} to a {@code float}, as well as other 49 * constants and methods useful when dealing with a 50 * {@code float}. 51 * 52 * <!-- Android-removed: paragraph on ValueBased 53 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a> 54 * class; programmers should treat instances that are 55 * {@linkplain #equals(Object) equal} as interchangeable and should not 56 * use instances for synchronization, or unpredictable behavior may 57 * occur. For example, in a future release, synchronization may fail. 58 * --> 59 * 60 * <h2><a id=equivalenceRelation>Floating-point Equality, Equivalence, 61 * and Comparison</a></h2> 62 * 63 * The class {@code java.lang.Double} has a <a 64 * href="Double.html#equivalenceRelation">discussion of equality, 65 * equivalence, and comparison of floating-point values</a> that is 66 * equality applicable to {@code float} values. 67 * 68 * @author Lee Boynton 69 * @author Arthur van Hoff 70 * @author Joseph D. Darcy 71 * @since 1.0 72 */ 73 @jdk.internal.ValueBased 74 public final class Float extends Number 75 implements Comparable<Float> 76 // Android-removed: no Constable support. 77 // , Constable, ConstantDesc 78 { 79 /** 80 * A constant holding the positive infinity of type 81 * {@code float}. It is equal to the value returned by 82 * {@code Float.intBitsToFloat(0x7f800000)}. 83 */ 84 public static final float POSITIVE_INFINITY = 1.0f / 0.0f; 85 86 /** 87 * A constant holding the negative infinity of type 88 * {@code float}. It is equal to the value returned by 89 * {@code Float.intBitsToFloat(0xff800000)}. 90 */ 91 public static final float NEGATIVE_INFINITY = -1.0f / 0.0f; 92 93 /** 94 * A constant holding a Not-a-Number (NaN) value of type 95 * {@code float}. It is equivalent to the value returned by 96 * {@code Float.intBitsToFloat(0x7fc00000)}. 97 */ 98 public static final float NaN = 0.0f / 0.0f; 99 100 /** 101 * A constant holding the largest positive finite value of type 102 * {@code float}, (2-2<sup>-23</sup>)·2<sup>127</sup>. 103 * It is equal to the hexadecimal floating-point literal 104 * {@code 0x1.fffffeP+127f} and also equal to 105 * {@code Float.intBitsToFloat(0x7f7fffff)}. 106 */ 107 public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f 108 109 /** 110 * A constant holding the smallest positive normal value of type 111 * {@code float}, 2<sup>-126</sup>. It is equal to the 112 * hexadecimal floating-point literal {@code 0x1.0p-126f} and also 113 * equal to {@code Float.intBitsToFloat(0x00800000)}. 114 * 115 * @since 1.6 116 */ 117 public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f 118 119 /** 120 * A constant holding the smallest positive nonzero value of type 121 * {@code float}, 2<sup>-149</sup>. It is equal to the 122 * hexadecimal floating-point literal {@code 0x0.000002P-126f} 123 * and also equal to {@code Float.intBitsToFloat(0x1)}. 124 */ 125 public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f 126 127 /** 128 * Maximum exponent a finite {@code float} variable may have. It 129 * is equal to the value returned by {@code 130 * Math.getExponent(Float.MAX_VALUE)}. 131 * 132 * @since 1.6 133 */ 134 public static final int MAX_EXPONENT = 127; 135 136 /** 137 * Minimum exponent a normalized {@code float} variable may have. 138 * It is equal to the value returned by {@code 139 * Math.getExponent(Float.MIN_NORMAL)}. 140 * 141 * @since 1.6 142 */ 143 public static final int MIN_EXPONENT = -126; 144 145 /** 146 * The number of bits used to represent a {@code float} value. 147 * 148 * @since 1.5 149 */ 150 public static final int SIZE = 32; 151 152 /** 153 * The number of bytes used to represent a {@code float} value. 154 * 155 * @since 1.8 156 */ 157 public static final int BYTES = SIZE / Byte.SIZE; 158 159 /** 160 * The {@code Class} instance representing the primitive type 161 * {@code float}. 162 * 163 * @since 1.1 164 */ 165 @SuppressWarnings("unchecked") 166 public static final Class<Float> TYPE = (Class<Float>) Class.getPrimitiveClass("float"); 167 168 /** 169 * Returns a string representation of the {@code float} 170 * argument. All characters mentioned below are ASCII characters. 171 * <ul> 172 * <li>If the argument is NaN, the result is the string 173 * "{@code NaN}". 174 * <li>Otherwise, the result is a string that represents the sign and 175 * magnitude (absolute value) of the argument. If the sign is 176 * negative, the first character of the result is 177 * '{@code -}' ({@code '\u005Cu002D'}); if the sign is 178 * positive, no sign character appears in the result. As for 179 * the magnitude <i>m</i>: 180 * <ul> 181 * <li>If <i>m</i> is infinity, it is represented by the characters 182 * {@code "Infinity"}; thus, positive infinity produces 183 * the result {@code "Infinity"} and negative infinity 184 * produces the result {@code "-Infinity"}. 185 * <li>If <i>m</i> is zero, it is represented by the characters 186 * {@code "0.0"}; thus, negative zero produces the result 187 * {@code "-0.0"} and positive zero produces the result 188 * {@code "0.0"}. 189 * <li> If <i>m</i> is greater than or equal to 10<sup>-3</sup> but 190 * less than 10<sup>7</sup>, then it is represented as the 191 * integer part of <i>m</i>, in decimal form with no leading 192 * zeroes, followed by '{@code .}' 193 * ({@code '\u005Cu002E'}), followed by one or more 194 * decimal digits representing the fractional part of 195 * <i>m</i>. 196 * <li> If <i>m</i> is less than 10<sup>-3</sup> or greater than or 197 * equal to 10<sup>7</sup>, then it is represented in 198 * so-called "computerized scientific notation." Let <i>n</i> 199 * be the unique integer such that 10<sup><i>n</i> </sup>≤ 200 * <i>m</i> {@literal <} 10<sup><i>n</i>+1</sup>; then let <i>a</i> 201 * be the mathematically exact quotient of <i>m</i> and 202 * 10<sup><i>n</i></sup> so that 1 ≤ <i>a</i> {@literal <} 10. 203 * The magnitude is then represented as the integer part of 204 * <i>a</i>, as a single decimal digit, followed by 205 * '{@code .}' ({@code '\u005Cu002E'}), followed by 206 * decimal digits representing the fractional part of 207 * <i>a</i>, followed by the letter '{@code E}' 208 * ({@code '\u005Cu0045'}), followed by a representation 209 * of <i>n</i> as a decimal integer, as produced by the 210 * method {@link java.lang.Integer#toString(int)}. 211 * 212 * </ul> 213 * </ul> 214 * How many digits must be printed for the fractional part of 215 * <i>m</i> or <i>a</i>? There must be at least one digit 216 * to represent the fractional part, and beyond that as many, but 217 * only as many, more digits as are needed to uniquely distinguish 218 * the argument value from adjacent values of type 219 * {@code float}. That is, suppose that <i>x</i> is the 220 * exact mathematical value represented by the decimal 221 * representation produced by this method for a finite nonzero 222 * argument <i>f</i>. Then <i>f</i> must be the {@code float} 223 * value nearest to <i>x</i>; or, if two {@code float} values are 224 * equally close to <i>x</i>, then <i>f</i> must be one of 225 * them and the least significant bit of the significand of 226 * <i>f</i> must be {@code 0}. 227 * 228 * <p>To create localized string representations of a floating-point 229 * value, use subclasses of {@link java.text.NumberFormat}. 230 * 231 * @param f the float to be converted. 232 * @return a string representation of the argument. 233 */ toString(float f)234 public static String toString(float f) { 235 return FloatingDecimal.toJavaFormatString(f); 236 } 237 238 /** 239 * Returns a hexadecimal string representation of the 240 * {@code float} argument. All characters mentioned below are 241 * ASCII characters. 242 * 243 * <ul> 244 * <li>If the argument is NaN, the result is the string 245 * "{@code NaN}". 246 * <li>Otherwise, the result is a string that represents the sign and 247 * magnitude (absolute value) of the argument. If the sign is negative, 248 * the first character of the result is '{@code -}' 249 * ({@code '\u005Cu002D'}); if the sign is positive, no sign character 250 * appears in the result. As for the magnitude <i>m</i>: 251 * 252 * <ul> 253 * <li>If <i>m</i> is infinity, it is represented by the string 254 * {@code "Infinity"}; thus, positive infinity produces the 255 * result {@code "Infinity"} and negative infinity produces 256 * the result {@code "-Infinity"}. 257 * 258 * <li>If <i>m</i> is zero, it is represented by the string 259 * {@code "0x0.0p0"}; thus, negative zero produces the result 260 * {@code "-0x0.0p0"} and positive zero produces the result 261 * {@code "0x0.0p0"}. 262 * 263 * <li>If <i>m</i> is a {@code float} value with a 264 * normalized representation, substrings are used to represent the 265 * significand and exponent fields. The significand is 266 * represented by the characters {@code "0x1."} 267 * followed by a lowercase hexadecimal representation of the rest 268 * of the significand as a fraction. Trailing zeros in the 269 * hexadecimal representation are removed unless all the digits 270 * are zero, in which case a single zero is used. Next, the 271 * exponent is represented by {@code "p"} followed 272 * by a decimal string of the unbiased exponent as if produced by 273 * a call to {@link Integer#toString(int) Integer.toString} on the 274 * exponent value. 275 * 276 * <li>If <i>m</i> is a {@code float} value with a subnormal 277 * representation, the significand is represented by the 278 * characters {@code "0x0."} followed by a 279 * hexadecimal representation of the rest of the significand as a 280 * fraction. Trailing zeros in the hexadecimal representation are 281 * removed. Next, the exponent is represented by 282 * {@code "p-126"}. Note that there must be at 283 * least one nonzero digit in a subnormal significand. 284 * 285 * </ul> 286 * 287 * </ul> 288 * 289 * <table class="striped"> 290 * <caption>Examples</caption> 291 * <thead> 292 * <tr><th scope="col">Floating-point Value</th><th scope="col">Hexadecimal String</th> 293 * </thead> 294 * <tbody> 295 * <tr><th scope="row">{@code 1.0}</th> <td>{@code 0x1.0p0}</td> 296 * <tr><th scope="row">{@code -1.0}</th> <td>{@code -0x1.0p0}</td> 297 * <tr><th scope="row">{@code 2.0}</th> <td>{@code 0x1.0p1}</td> 298 * <tr><th scope="row">{@code 3.0}</th> <td>{@code 0x1.8p1}</td> 299 * <tr><th scope="row">{@code 0.5}</th> <td>{@code 0x1.0p-1}</td> 300 * <tr><th scope="row">{@code 0.25}</th> <td>{@code 0x1.0p-2}</td> 301 * <tr><th scope="row">{@code Float.MAX_VALUE}</th> 302 * <td>{@code 0x1.fffffep127}</td> 303 * <tr><th scope="row">{@code Minimum Normal Value}</th> 304 * <td>{@code 0x1.0p-126}</td> 305 * <tr><th scope="row">{@code Maximum Subnormal Value}</th> 306 * <td>{@code 0x0.fffffep-126}</td> 307 * <tr><th scope="row">{@code Float.MIN_VALUE}</th> 308 * <td>{@code 0x0.000002p-126}</td> 309 * </tbody> 310 * </table> 311 * @param f the {@code float} to be converted. 312 * @return a hex string representation of the argument. 313 * @since 1.5 314 * @author Joseph D. Darcy 315 */ toHexString(float f)316 public static String toHexString(float f) { 317 if (Math.abs(f) < Float.MIN_NORMAL 318 && f != 0.0f ) {// float subnormal 319 // Adjust exponent to create subnormal double, then 320 // replace subnormal double exponent with subnormal float 321 // exponent 322 String s = Double.toHexString(Math.scalb((double)f, 323 /* -1022+126 */ 324 Double.MIN_EXPONENT- 325 Float.MIN_EXPONENT)); 326 return s.replaceFirst("p-1022$", "p-126"); 327 } 328 else // double string will be the same as float string 329 return Double.toHexString(f); 330 } 331 332 /** 333 * Returns a {@code Float} object holding the 334 * {@code float} value represented by the argument string 335 * {@code s}. 336 * 337 * <p>If {@code s} is {@code null}, then a 338 * {@code NullPointerException} is thrown. 339 * 340 * <p>Leading and trailing whitespace characters in {@code s} 341 * are ignored. Whitespace is removed as if by the {@link 342 * String#trim} method; that is, both ASCII space and control 343 * characters are removed. The rest of {@code s} should 344 * constitute a <i>FloatValue</i> as described by the lexical 345 * syntax rules: 346 * 347 * <blockquote> 348 * <dl> 349 * <dt><i>FloatValue:</i> 350 * <dd><i>Sign<sub>opt</sub></i> {@code NaN} 351 * <dd><i>Sign<sub>opt</sub></i> {@code Infinity} 352 * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i> 353 * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i> 354 * <dd><i>SignedInteger</i> 355 * </dl> 356 * 357 * <dl> 358 * <dt><i>HexFloatingPointLiteral</i>: 359 * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i> 360 * </dl> 361 * 362 * <dl> 363 * <dt><i>HexSignificand:</i> 364 * <dd><i>HexNumeral</i> 365 * <dd><i>HexNumeral</i> {@code .} 366 * <dd>{@code 0x} <i>HexDigits<sub>opt</sub> 367 * </i>{@code .}<i> HexDigits</i> 368 * <dd>{@code 0X}<i> HexDigits<sub>opt</sub> 369 * </i>{@code .} <i>HexDigits</i> 370 * </dl> 371 * 372 * <dl> 373 * <dt><i>BinaryExponent:</i> 374 * <dd><i>BinaryExponentIndicator SignedInteger</i> 375 * </dl> 376 * 377 * <dl> 378 * <dt><i>BinaryExponentIndicator:</i> 379 * <dd>{@code p} 380 * <dd>{@code P} 381 * </dl> 382 * 383 * </blockquote> 384 * 385 * where <i>Sign</i>, <i>FloatingPointLiteral</i>, 386 * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and 387 * <i>FloatTypeSuffix</i> are as defined in the lexical structure 388 * sections of 389 * <cite>The Java Language Specification</cite>, 390 * except that underscores are not accepted between digits. 391 * If {@code s} does not have the form of 392 * a <i>FloatValue</i>, then a {@code NumberFormatException} 393 * is thrown. Otherwise, {@code s} is regarded as 394 * representing an exact decimal value in the usual 395 * "computerized scientific notation" or as an exact 396 * hexadecimal value; this exact numerical value is then 397 * conceptually converted to an "infinitely precise" 398 * binary value that is then rounded to type {@code float} 399 * by the usual round-to-nearest rule of IEEE 754 floating-point 400 * arithmetic, which includes preserving the sign of a zero 401 * value. 402 * 403 * Note that the round-to-nearest rule also implies overflow and 404 * underflow behaviour; if the exact value of {@code s} is large 405 * enough in magnitude (greater than or equal to ({@link 406 * #MAX_VALUE} + {@link Math#ulp(float) ulp(MAX_VALUE)}/2), 407 * rounding to {@code float} will result in an infinity and if the 408 * exact value of {@code s} is small enough in magnitude (less 409 * than or equal to {@link #MIN_VALUE}/2), rounding to float will 410 * result in a zero. 411 * 412 * Finally, after rounding a {@code Float} object representing 413 * this {@code float} value is returned. 414 * 415 * <p>To interpret localized string representations of a 416 * floating-point value, use subclasses of {@link 417 * java.text.NumberFormat}. 418 * 419 * <p>Note that trailing format specifiers, specifiers that 420 * determine the type of a floating-point literal 421 * ({@code 1.0f} is a {@code float} value; 422 * {@code 1.0d} is a {@code double} value), do 423 * <em>not</em> influence the results of this method. In other 424 * words, the numerical value of the input string is converted 425 * directly to the target floating-point type. In general, the 426 * two-step sequence of conversions, string to {@code double} 427 * followed by {@code double} to {@code float}, is 428 * <em>not</em> equivalent to converting a string directly to 429 * {@code float}. For example, if first converted to an 430 * intermediate {@code double} and then to 431 * {@code float}, the string<br> 432 * {@code "1.00000017881393421514957253748434595763683319091796875001d"}<br> 433 * results in the {@code float} value 434 * {@code 1.0000002f}; if the string is converted directly to 435 * {@code float}, <code>1.000000<b>1</b>f</code> results. 436 * 437 * <p>To avoid calling this method on an invalid string and having 438 * a {@code NumberFormatException} be thrown, the documentation 439 * for {@link Double#valueOf Double.valueOf} lists a regular 440 * expression which can be used to screen the input. 441 * 442 * @param s the string to be parsed. 443 * @return a {@code Float} object holding the value 444 * represented by the {@code String} argument. 445 * @throws NumberFormatException if the string does not contain a 446 * parsable number. 447 */ valueOf(String s)448 public static Float valueOf(String s) throws NumberFormatException { 449 return new Float(parseFloat(s)); 450 } 451 452 /** 453 * Returns a {@code Float} instance representing the specified 454 * {@code float} value. 455 * If a new {@code Float} instance is not required, this method 456 * should generally be used in preference to the constructor 457 * {@link #Float(float)}, as this method is likely to yield 458 * significantly better space and time performance by caching 459 * frequently requested values. 460 * 461 * @param f a float value. 462 * @return a {@code Float} instance representing {@code f}. 463 * @since 1.5 464 */ 465 @IntrinsicCandidate valueOf(float f)466 public static Float valueOf(float f) { 467 return new Float(f); 468 } 469 470 /** 471 * Returns a new {@code float} initialized to the value 472 * represented by the specified {@code String}, as performed 473 * by the {@code valueOf} method of class {@code Float}. 474 * 475 * @param s the string to be parsed. 476 * @return the {@code float} value represented by the string 477 * argument. 478 * @throws NullPointerException if the string is null 479 * @throws NumberFormatException if the string does not contain a 480 * parsable {@code float}. 481 * @see java.lang.Float#valueOf(String) 482 * @since 1.2 483 */ parseFloat(String s)484 public static float parseFloat(String s) throws NumberFormatException { 485 return FloatingDecimal.parseFloat(s); 486 } 487 488 /** 489 * Returns {@code true} if the specified number is a 490 * Not-a-Number (NaN) value, {@code false} otherwise. 491 * 492 * @param v the value to be tested. 493 * @return {@code true} if the argument is NaN; 494 * {@code false} otherwise. 495 */ isNaN(float v)496 public static boolean isNaN(float v) { 497 return (v != v); 498 } 499 500 /** 501 * Returns {@code true} if the specified number is infinitely 502 * large in magnitude, {@code false} otherwise. 503 * 504 * @param v the value to be tested. 505 * @return {@code true} if the argument is positive infinity or 506 * negative infinity; {@code false} otherwise. 507 */ isInfinite(float v)508 public static boolean isInfinite(float v) { 509 return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY); 510 } 511 512 513 /** 514 * Returns {@code true} if the argument is a finite floating-point 515 * value; returns {@code false} otherwise (for NaN and infinity 516 * arguments). 517 * 518 * @param f the {@code float} value to be tested 519 * @return {@code true} if the argument is a finite 520 * floating-point value, {@code false} otherwise. 521 * @since 1.8 522 */ isFinite(float f)523 public static boolean isFinite(float f) { 524 return Math.abs(f) <= Float.MAX_VALUE; 525 } 526 527 /** 528 * The value of the Float. 529 * 530 * @serial 531 */ 532 private final float value; 533 534 /** 535 * Constructs a newly allocated {@code Float} object that 536 * represents the primitive {@code float} argument. 537 * 538 * @param value the value to be represented by the {@code Float}. 539 * 540 * @deprecated 541 * It is rarely appropriate to use this constructor. The static factory 542 * {@link #valueOf(float)} is generally a better choice, as it is 543 * likely to yield significantly better space and time performance. 544 */ 545 // Android-changed: not yet forRemoval on Android. 546 @Deprecated(since="9"/*, forRemoval = true*/) Float(float value)547 public Float(float value) { 548 this.value = value; 549 } 550 551 /** 552 * Constructs a newly allocated {@code Float} object that 553 * represents the argument converted to type {@code float}. 554 * 555 * @param value the value to be represented by the {@code Float}. 556 * 557 * @deprecated 558 * It is rarely appropriate to use this constructor. Instead, use the 559 * static factory method {@link #valueOf(float)} method as follows: 560 * {@code Float.valueOf((float)value)}. 561 */ 562 // Android-changed: not yet forRemoval on Android. 563 @Deprecated(since="9"/*, forRemoval = true*/) Float(double value)564 public Float(double value) { 565 this.value = (float)value; 566 } 567 568 /** 569 * Constructs a newly allocated {@code Float} object that 570 * represents the floating-point value of type {@code float} 571 * represented by the string. The string is converted to a 572 * {@code float} value as if by the {@code valueOf} method. 573 * 574 * @param s a string to be converted to a {@code Float}. 575 * @throws NumberFormatException if the string does not contain a 576 * parsable number. 577 * 578 * @deprecated 579 * It is rarely appropriate to use this constructor. 580 * Use {@link #parseFloat(String)} to convert a string to a 581 * {@code float} primitive, or use {@link #valueOf(String)} 582 * to convert a string to a {@code Float} object. 583 */ 584 // Android-changed: not yet forRemoval on Android. 585 @Deprecated(since="9"/*, forRemoval = true*/) Float(String s)586 public Float(String s) throws NumberFormatException { 587 value = parseFloat(s); 588 } 589 590 /** 591 * Returns {@code true} if this {@code Float} value is a 592 * Not-a-Number (NaN), {@code false} otherwise. 593 * 594 * @return {@code true} if the value represented by this object is 595 * NaN; {@code false} otherwise. 596 */ isNaN()597 public boolean isNaN() { 598 return isNaN(value); 599 } 600 601 /** 602 * Returns {@code true} if this {@code Float} value is 603 * infinitely large in magnitude, {@code false} otherwise. 604 * 605 * @return {@code true} if the value represented by this object is 606 * positive infinity or negative infinity; 607 * {@code false} otherwise. 608 */ isInfinite()609 public boolean isInfinite() { 610 return isInfinite(value); 611 } 612 613 /** 614 * Returns a string representation of this {@code Float} object. 615 * The primitive {@code float} value represented by this object 616 * is converted to a {@code String} exactly as if by the method 617 * {@code toString} of one argument. 618 * 619 * @return a {@code String} representation of this object. 620 * @see java.lang.Float#toString(float) 621 */ toString()622 public String toString() { 623 return Float.toString(value); 624 } 625 626 /** 627 * Returns the value of this {@code Float} as a {@code byte} after 628 * a narrowing primitive conversion. 629 * 630 * @return the {@code float} value represented by this object 631 * converted to type {@code byte} 632 * @jls 5.1.3 Narrowing Primitive Conversion 633 */ byteValue()634 public byte byteValue() { 635 return (byte)value; 636 } 637 638 /** 639 * Returns the value of this {@code Float} as a {@code short} 640 * after a narrowing primitive conversion. 641 * 642 * @return the {@code float} value represented by this object 643 * converted to type {@code short} 644 * @jls 5.1.3 Narrowing Primitive Conversion 645 * @since 1.1 646 */ shortValue()647 public short shortValue() { 648 return (short)value; 649 } 650 651 /** 652 * Returns the value of this {@code Float} as an {@code int} after 653 * a narrowing primitive conversion. 654 * 655 * @return the {@code float} value represented by this object 656 * converted to type {@code int} 657 * @jls 5.1.3 Narrowing Primitive Conversion 658 */ intValue()659 public int intValue() { 660 return (int)value; 661 } 662 663 /** 664 * Returns value of this {@code Float} as a {@code long} after a 665 * narrowing primitive conversion. 666 * 667 * @return the {@code float} value represented by this object 668 * converted to type {@code long} 669 * @jls 5.1.3 Narrowing Primitive Conversion 670 */ longValue()671 public long longValue() { 672 return (long)value; 673 } 674 675 /** 676 * Returns the {@code float} value of this {@code Float} object. 677 * 678 * @return the {@code float} value represented by this object 679 */ 680 @IntrinsicCandidate floatValue()681 public float floatValue() { 682 return value; 683 } 684 685 /** 686 * Returns the value of this {@code Float} as a {@code double} 687 * after a widening primitive conversion. 688 * 689 * @return the {@code float} value represented by this 690 * object converted to type {@code double} 691 * @jls 5.1.2 Widening Primitive Conversion 692 */ doubleValue()693 public double doubleValue() { 694 return (double)value; 695 } 696 697 /** 698 * Returns a hash code for this {@code Float} object. The 699 * result is the integer bit representation, exactly as produced 700 * by the method {@link #floatToIntBits(float)}, of the primitive 701 * {@code float} value represented by this {@code Float} 702 * object. 703 * 704 * @return a hash code value for this object. 705 */ 706 @Override hashCode()707 public int hashCode() { 708 return Float.hashCode(value); 709 } 710 711 /** 712 * Returns a hash code for a {@code float} value; compatible with 713 * {@code Float.hashCode()}. 714 * 715 * @param value the value to hash 716 * @return a hash code value for a {@code float} value. 717 * @since 1.8 718 */ hashCode(float value)719 public static int hashCode(float value) { 720 return floatToIntBits(value); 721 } 722 723 /** 724 * Compares this object against the specified object. The result 725 * is {@code true} if and only if the argument is not 726 * {@code null} and is a {@code Float} object that 727 * represents a {@code float} with the same value as the 728 * {@code float} represented by this object. For this 729 * purpose, two {@code float} values are considered to be the 730 * same if and only if the method {@link #floatToIntBits(float)} 731 * returns the identical {@code int} value when applied to 732 * each. 733 * 734 * @apiNote 735 * This method is defined in terms of {@link 736 * #floatToIntBits(float)} rather than the {@code ==} operator on 737 * {@code float} values since the {@code ==} operator does 738 * <em>not</em> define an equivalence relation and to satisfy the 739 * {@linkplain Object#equals equals contract} an equivalence 740 * relation must be implemented; see <a 741 * href="Double.html#equivalenceRelation">this discussion</a> for 742 * details of floating-point equality and equivalence. 743 * 744 * @param obj the object to be compared 745 * @return {@code true} if the objects are the same; 746 * {@code false} otherwise. 747 * @see java.lang.Float#floatToIntBits(float) 748 * @jls 15.21.1 Numerical Equality Operators == and != 749 */ equals(Object obj)750 public boolean equals(Object obj) { 751 return (obj instanceof Float) 752 && (floatToIntBits(((Float)obj).value) == floatToIntBits(value)); 753 } 754 755 /** 756 * Returns a representation of the specified floating-point value 757 * according to the IEEE 754 floating-point "single format" bit 758 * layout. 759 * 760 * <p>Bit 31 (the bit that is selected by the mask 761 * {@code 0x80000000}) represents the sign of the floating-point 762 * number. 763 * Bits 30-23 (the bits that are selected by the mask 764 * {@code 0x7f800000}) represent the exponent. 765 * Bits 22-0 (the bits that are selected by the mask 766 * {@code 0x007fffff}) represent the significand (sometimes called 767 * the mantissa) of the floating-point number. 768 * 769 * <p>If the argument is positive infinity, the result is 770 * {@code 0x7f800000}. 771 * 772 * <p>If the argument is negative infinity, the result is 773 * {@code 0xff800000}. 774 * 775 * <p>If the argument is NaN, the result is {@code 0x7fc00000}. 776 * 777 * <p>In all cases, the result is an integer that, when given to the 778 * {@link #intBitsToFloat(int)} method, will produce a floating-point 779 * value the same as the argument to {@code floatToIntBits} 780 * (except all NaN values are collapsed to a single 781 * "canonical" NaN value). 782 * 783 * @param value a floating-point number. 784 * @return the bits that represent the floating-point number. 785 */ 786 @IntrinsicCandidate floatToIntBits(float value)787 public static int floatToIntBits(float value) { 788 if (!isNaN(value)) { 789 return floatToRawIntBits(value); 790 } 791 return 0x7fc00000; 792 } 793 794 /** 795 * Returns a representation of the specified floating-point value 796 * according to the IEEE 754 floating-point "single format" bit 797 * layout, preserving Not-a-Number (NaN) values. 798 * 799 * <p>Bit 31 (the bit that is selected by the mask 800 * {@code 0x80000000}) represents the sign of the floating-point 801 * number. 802 * Bits 30-23 (the bits that are selected by the mask 803 * {@code 0x7f800000}) represent the exponent. 804 * Bits 22-0 (the bits that are selected by the mask 805 * {@code 0x007fffff}) represent the significand (sometimes called 806 * the mantissa) of the floating-point number. 807 * 808 * <p>If the argument is positive infinity, the result is 809 * {@code 0x7f800000}. 810 * 811 * <p>If the argument is negative infinity, the result is 812 * {@code 0xff800000}. 813 * 814 * <p>If the argument is NaN, the result is the integer representing 815 * the actual NaN value. Unlike the {@code floatToIntBits} 816 * method, {@code floatToRawIntBits} does not collapse all the 817 * bit patterns encoding a NaN to a single "canonical" 818 * NaN value. 819 * 820 * <p>In all cases, the result is an integer that, when given to the 821 * {@link #intBitsToFloat(int)} method, will produce a 822 * floating-point value the same as the argument to 823 * {@code floatToRawIntBits}. 824 * 825 * @param value a floating-point number. 826 * @return the bits that represent the floating-point number. 827 * @since 1.3 828 */ 829 @IntrinsicCandidate floatToRawIntBits(float value)830 public static native int floatToRawIntBits(float value); 831 832 /** 833 * Returns the {@code float} value corresponding to a given 834 * bit representation. 835 * The argument is considered to be a representation of a 836 * floating-point value according to the IEEE 754 floating-point 837 * "single format" bit layout. 838 * 839 * <p>If the argument is {@code 0x7f800000}, the result is positive 840 * infinity. 841 * 842 * <p>If the argument is {@code 0xff800000}, the result is negative 843 * infinity. 844 * 845 * <p>If the argument is any value in the range 846 * {@code 0x7f800001} through {@code 0x7fffffff} or in 847 * the range {@code 0xff800001} through 848 * {@code 0xffffffff}, the result is a NaN. No IEEE 754 849 * floating-point operation provided by Java can distinguish 850 * between two NaN values of the same type with different bit 851 * patterns. Distinct values of NaN are only distinguishable by 852 * use of the {@code Float.floatToRawIntBits} method. 853 * 854 * <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three 855 * values that can be computed from the argument: 856 * 857 * <blockquote><pre>{@code 858 * int s = ((bits >> 31) == 0) ? 1 : -1; 859 * int e = ((bits >> 23) & 0xff); 860 * int m = (e == 0) ? 861 * (bits & 0x7fffff) << 1 : 862 * (bits & 0x7fffff) | 0x800000; 863 * }</pre></blockquote> 864 * 865 * Then the floating-point result equals the value of the mathematical 866 * expression <i>s</i>·<i>m</i>·2<sup><i>e</i>-150</sup>. 867 * 868 * <p>Note that this method may not be able to return a 869 * {@code float} NaN with exactly same bit pattern as the 870 * {@code int} argument. IEEE 754 distinguishes between two 871 * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The 872 * differences between the two kinds of NaN are generally not 873 * visible in Java. Arithmetic operations on signaling NaNs turn 874 * them into quiet NaNs with a different, but often similar, bit 875 * pattern. However, on some processors merely copying a 876 * signaling NaN also performs that conversion. In particular, 877 * copying a signaling NaN to return it to the calling method may 878 * perform this conversion. So {@code intBitsToFloat} may 879 * not be able to return a {@code float} with a signaling NaN 880 * bit pattern. Consequently, for some {@code int} values, 881 * {@code floatToRawIntBits(intBitsToFloat(start))} may 882 * <i>not</i> equal {@code start}. Moreover, which 883 * particular bit patterns represent signaling NaNs is platform 884 * dependent; although all NaN bit patterns, quiet or signaling, 885 * must be in the NaN range identified above. 886 * 887 * @param bits an integer. 888 * @return the {@code float} floating-point value with the same bit 889 * pattern. 890 */ 891 @IntrinsicCandidate intBitsToFloat(int bits)892 public static native float intBitsToFloat(int bits); 893 894 /** 895 * Compares two {@code Float} objects numerically. 896 * 897 * This method imposes a total order on {@code Float} objects 898 * with two differences compared to the incomplete order defined by 899 * the Java language numerical comparison operators ({@code <, <=, 900 * ==, >=, >}) on {@code float} values. 901 * 902 * <ul><li> A NaN is <em>unordered</em> with respect to other 903 * values and unequal to itself under the comparison 904 * operators. This method chooses to define {@code 905 * Float.NaN} to be equal to itself and greater than all 906 * other {@code double} values (including {@code 907 * Float.POSITIVE_INFINITY}). 908 * 909 * <li> Positive zero and negative zero compare equal 910 * numerically, but are distinct and distinguishable values. 911 * This method chooses to define positive zero ({@code +0.0f}), 912 * to be greater than negative zero ({@code -0.0f}). 913 * </ul> 914 * 915 * This ensures that the <i>natural ordering</i> of {@code Float} 916 * objects imposed by this method is <i>consistent with 917 * equals</i>; see <a href="Double.html#equivalenceRelation">this 918 * discussion</a> for details of floating-point comparison and 919 * ordering. 920 * 921 * 922 * @param anotherFloat the {@code Float} to be compared. 923 * @return the value {@code 0} if {@code anotherFloat} is 924 * numerically equal to this {@code Float}; a value 925 * less than {@code 0} if this {@code Float} 926 * is numerically less than {@code anotherFloat}; 927 * and a value greater than {@code 0} if this 928 * {@code Float} is numerically greater than 929 * {@code anotherFloat}. 930 * 931 * @jls 15.20.1 Numerical Comparison Operators {@code <}, {@code <=}, {@code >}, and {@code >=} 932 * @since 1.2 933 */ compareTo(Float anotherFloat)934 public int compareTo(Float anotherFloat) { 935 return Float.compare(value, anotherFloat.value); 936 } 937 938 /** 939 * Compares the two specified {@code float} values. The sign 940 * of the integer value returned is the same as that of the 941 * integer that would be returned by the call: 942 * <pre> 943 * new Float(f1).compareTo(new Float(f2)) 944 * </pre> 945 * 946 * @param f1 the first {@code float} to compare. 947 * @param f2 the second {@code float} to compare. 948 * @return the value {@code 0} if {@code f1} is 949 * numerically equal to {@code f2}; a value less than 950 * {@code 0} if {@code f1} is numerically less than 951 * {@code f2}; and a value greater than {@code 0} 952 * if {@code f1} is numerically greater than 953 * {@code f2}. 954 * @since 1.4 955 */ compare(float f1, float f2)956 public static int compare(float f1, float f2) { 957 if (f1 < f2) 958 return -1; // Neither val is NaN, thisVal is smaller 959 if (f1 > f2) 960 return 1; // Neither val is NaN, thisVal is larger 961 962 // Cannot use floatToRawIntBits because of possibility of NaNs. 963 int thisBits = Float.floatToIntBits(f1); 964 int anotherBits = Float.floatToIntBits(f2); 965 966 return (thisBits == anotherBits ? 0 : // Values are equal 967 (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN) 968 1)); // (0.0, -0.0) or (NaN, !NaN) 969 } 970 971 /** 972 * Adds two {@code float} values together as per the + operator. 973 * 974 * @param a the first operand 975 * @param b the second operand 976 * @return the sum of {@code a} and {@code b} 977 * @jls 4.2.4 Floating-Point Operations 978 * @see java.util.function.BinaryOperator 979 * @since 1.8 980 */ sum(float a, float b)981 public static float sum(float a, float b) { 982 return a + b; 983 } 984 985 /** 986 * Returns the greater of two {@code float} values 987 * as if by calling {@link Math#max(float, float) Math.max}. 988 * 989 * @param a the first operand 990 * @param b the second operand 991 * @return the greater of {@code a} and {@code b} 992 * @see java.util.function.BinaryOperator 993 * @since 1.8 994 */ max(float a, float b)995 public static float max(float a, float b) { 996 return Math.max(a, b); 997 } 998 999 /** 1000 * Returns the smaller of two {@code float} values 1001 * as if by calling {@link Math#min(float, float) Math.min}. 1002 * 1003 * @param a the first operand 1004 * @param b the second operand 1005 * @return the smaller of {@code a} and {@code b} 1006 * @see java.util.function.BinaryOperator 1007 * @since 1.8 1008 */ min(float a, float b)1009 public static float min(float a, float b) { 1010 return Math.min(a, b); 1011 } 1012 1013 // BEGIN Android-removed: dynamic constants not supported on Android. 1014 /** 1015 * Returns an {@link Optional} containing the nominal descriptor for this 1016 * instance, which is the instance itself. 1017 * 1018 * @return an {@link Optional} describing the {@linkplain Float} instance 1019 * @since 12 1020 * 1021 @Override 1022 public Optional<Float> describeConstable() { 1023 return Optional.of(this); 1024 } 1025 1026 /** 1027 * Resolves this instance as a {@link ConstantDesc}, the result of which is 1028 * the instance itself. 1029 * 1030 * @param lookup ignored 1031 * @return the {@linkplain Float} instance 1032 * @since 12 1033 * 1034 @Override 1035 public Float resolveConstantDesc(MethodHandles.Lookup lookup) { 1036 return this; 1037 } 1038 // END Android-removed: dynamic constants not supported on Android. 1039 1040 /** use serialVersionUID from JDK 1.0.2 for interoperability */ 1041 @java.io.Serial 1042 private static final long serialVersionUID = -2671257302660747028L; 1043 } 1044