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