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 jdk.internal.math.FloatingDecimal;
29 import jdk.internal.math.FloatConsts;
30 import jdk.internal.math.DoubleConsts;
31 import jdk.internal.HotSpotIntrinsicCandidate;
32
33 /**
34 * The {@code Float} class wraps a value of primitive type
35 * {@code float} in an object. An object of type
36 * {@code Float} contains a single field whose type is
37 * {@code float}.
38 *
39 * <p>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<Float> {
265 * <tr><td>{@code -1.0}</td> <td>{@code -0x1.0p0}</td>
266 * <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>
267 * <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>
268 * <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>
269 * <tr><td>{@code 0.25}</td> <td>{@code 0x1.0p-2}</td>
270 * <tr><td>{@code Float.MAX_VALUE}</td>
271 * <td>{@code 0x1.fffffep127}</td>
272 * <tr><td>{@code Minimum Normal Value}</td>
273 * <td>{@code 0x1.0p-126}</td>
274 * <tr><td>{@code Maximum Subnormal Value}</td>
275 * <td>{@code 0x0.fffffep-126}</td>
276 * <tr><td>{@code Float.MIN_VALUE}</td>
277 * <td>{@code 0x0.000002p-126}</td>
278 * </table>
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 * <p>If {@code s} is {@code null}, then a
306 * {@code NullPointerException} is thrown.
307 *
308 * <p>Leading and trailing whitespace characters in {@code s}
309 * are ignored. Whitespace is removed as if by the {@link
310 * String#trim} method; that is, both ASCII space and control
311 * characters are removed. The rest of {@code s} should
312 * constitute a <i>FloatValue</i> as described by the lexical
313 * syntax rules:
472 * @param v the value to be tested.
473 * @return {@code true} if the argument is positive infinity or
474 * negative infinity; {@code false} otherwise.
475 */
476 public static boolean isInfinite(float v) {
477 return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
478 }
479
480
481 /**
482 * Returns {@code true} if the argument is a finite floating-point
483 * value; returns {@code false} otherwise (for NaN and infinity
484 * arguments).
485 *
486 * @param f the {@code float} value to be tested
487 * @return {@code true} if the argument is a finite
488 * floating-point value, {@code false} otherwise.
489 * @since 1.8
490 */
491 public static boolean isFinite(float f) {
492 return Math.abs(f) <= FloatConsts.MAX_VALUE;
493 }
494
495 /**
496 * The value of the Float.
497 *
498 * @serial
499 */
500 private final float value;
501
502 /**
503 * Constructs a newly allocated {@code Float} object that
504 * represents the primitive {@code float} argument.
505 *
506 * @param value the value to be represented by the {@code Float}.
507 */
508 public Float(float value) {
509 this.value = value;
510 }
511
512 /**
|
1 /*
2 * Copyright (c) 1994, 2016, 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 jdk.internal.math.FloatingDecimal;
29 import jdk.internal.HotSpotIntrinsicCandidate;
30
31 /**
32 * The {@code Float} class wraps a value of primitive type
33 * {@code float} in an object. An object of type
34 * {@code Float} contains a single field whose type is
35 * {@code float}.
36 *
37 * <p>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<Float> {
263 * <tr><td>{@code -1.0}</td> <td>{@code -0x1.0p0}</td>
264 * <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>
265 * <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>
266 * <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>
267 * <tr><td>{@code 0.25}</td> <td>{@code 0x1.0p-2}</td>
268 * <tr><td>{@code Float.MAX_VALUE}</td>
269 * <td>{@code 0x1.fffffep127}</td>
270 * <tr><td>{@code Minimum Normal Value}</td>
271 * <td>{@code 0x1.0p-126}</td>
272 * <tr><td>{@code Maximum Subnormal Value}</td>
273 * <td>{@code 0x0.fffffep-126}</td>
274 * <tr><td>{@code Float.MIN_VALUE}</td>
275 * <td>{@code 0x0.000002p-126}</td>
276 * </table>
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 * <p>If {@code s} is {@code null}, then a
304 * {@code NullPointerException} is thrown.
305 *
306 * <p>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 <i>FloatValue</i> 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 /**
|