< prev index next >

src/java.base/share/classes/java/lang/Float.java

In addition, this class provides several methods for converting a 40 * {@code float} to a {@code String} and a 41 * {@code String} to a {@code float}, as well as other 42 * constants and methods useful when dealing with a 43 * {@code float}. 44 * 45 * @author Lee Boynton 46 * @author Arthur van Hoff 47 * @author Joseph D. Darcy 48 * @since 1.0 49 */ 50 public final class Float extends Number implements Comparable { 265 *

{@code -1.0} {@code -0x1.0p0}
{@code 2.0} {@code 0x1.0p1}
{@code 3.0} {@code 0x1.8p1}
{@code 0.5} {@code 0x1.0p-1}
{@code 0.25} {@code 0x1.0p-2}
{@code Float.MAX_VALUE}{@code 0x1.fffffep127}
{@code Minimum Normal Value}{@code 0x1.0p-126}
{@code Maximum Subnormal Value}{@code 0x0.fffffep-126}
{@code Float.MIN_VALUE}{@code 0x0.000002p-126}
266 * 267 * 268 * 269 * 270 * 271 * 272 * 273 * 274 * 275 * 276 * 277 * 278 * 279 * @param f the {@code float} to be converted. 280 * @return a hex string representation of the argument. 281 * @since 1.5 282 * @author Joseph D. Darcy 283 */ 284 public static String toHexString(float f) { 285 if (Math.abs(f) < FloatConsts.MIN_NORMAL 286 && f != 0.0f ) {// float subnormal 287 // Adjust exponent to create subnormal double, then 288 // replace subnormal double exponent with subnormal float 289 // exponent 290 String s = Double.toHexString(Math.scalb((double)f, 291 /* -1022+126 */ 292 DoubleConsts.MIN_EXPONENT- 293 FloatConsts.MIN_EXPONENT)); 294 return s.replaceFirst("p-1022\$", "p-126"); 295 } 296 else // double string will be the same as float string 297 return Double.toHexString(f); 298 } 299 300 /** 301 * Returns a {@code Float} object holding the 302 * {@code float} value represented by the argument string 303 * {@code s}. 304 * 305 *

If {@code s} is {@code null}, then a 306 * {@code NullPointerException} is thrown. 307 * 308 *

In addition, this class provides several methods for converting a 38 * {@code float} to a {@code String} and a 39 * {@code String} to a {@code float}, as well as other 40 * constants and methods useful when dealing with a 41 * {@code float}. 42 * 43 * @author Lee Boynton 44 * @author Arthur van Hoff 45 * @author Joseph D. Darcy 46 * @since 1.0 47 */ 48 public final class Float extends Number implements Comparable { 263 *

{@code -1.0} {@code -0x1.0p0}
{@code 2.0} {@code 0x1.0p1}
{@code 3.0} {@code 0x1.8p1}
{@code 0.5} {@code 0x1.0p-1}
{@code 0.25} {@code 0x1.0p-2}
{@code Float.MAX_VALUE}{@code 0x1.fffffep127}
{@code Minimum Normal Value}{@code 0x1.0p-126}
{@code Maximum Subnormal Value}{@code 0x0.fffffep-126}
{@code Float.MIN_VALUE}{@code 0x0.000002p-126}
264 * 265 * 266 * 267 * 268 * 269 * 270 * 271 * 272 * 273 * 274 * 275 * 276 * 277 * @param f the {@code float} to be converted. 278 * @return a hex string representation of the argument. 279 * @since 1.5 280 * @author Joseph D. Darcy 281 */ 282 public static String toHexString(float f) { 283 if (Math.abs(f) < Float.MIN_NORMAL 284 && f != 0.0f ) {// float subnormal 285 // Adjust exponent to create subnormal double, then 286 // replace subnormal double exponent with subnormal float 287 // exponent 288 String s = Double.toHexString(Math.scalb((double)f, 289 /* -1022+126 */ 290 Double.MIN_EXPONENT- 291 Float.MIN_EXPONENT)); 292 return s.replaceFirst("p-1022\$", "p-126"); 293 } 294 else // double string will be the same as float string 295 return Double.toHexString(f); 296 } 297 298 /** 299 * Returns a {@code Float} object holding the 300 * {@code float} value represented by the argument string 301 * {@code s}. 302 * 303 *

If {@code s} is {@code null}, then a 304 * {@code NullPointerException} is thrown. 305 * 306 *

Leading and trailing whitespace characters in {@code s} 307 * are ignored. Whitespace is removed as if by the {@link 308 * String#trim} method; that is, both ASCII space and control 309 * characters are removed. The rest of {@code s} should 310 * constitute a FloatValue as described by the lexical 311 * syntax rules: 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 */ 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 */ 489 public static boolean isFinite(float f) { 490 return Math.abs(f) <= Float.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 */ 506 public Float(float value) { 507 this.value = value; 508 } 509 510 /**

< prev index next >