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