author | simonis |
Fri, 14 Aug 2015 10:35:45 +0200 | |
changeset 32209 | 24bb680a1609 |
parent 31671 | 362e0c0acece |
child 34781 | 479b1724ab80 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
18143
b6ef7bd945ce
7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
15311
diff
changeset
|
2 |
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.lang; |
|
27 |
||
28 |
import sun.misc.FloatingDecimal; |
|
29 |
import sun.misc.FloatConsts; |
|
30 |
import sun.misc.DoubleConsts; |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
31 |
import jdk.internal.HotSpotIntrinsicCandidate; |
2 | 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 |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
22290
diff
changeset
|
48 |
* @since 1.0 |
2 | 49 |
*/ |
50 |
public final class Float extends Number implements Comparable<Float> { |
|
51 |
/** |
|
52 |
* A constant holding the positive infinity of type |
|
53 |
* {@code float}. It is equal to the value returned by |
|
54 |
* {@code Float.intBitsToFloat(0x7f800000)}. |
|
55 |
*/ |
|
56 |
public static final float POSITIVE_INFINITY = 1.0f / 0.0f; |
|
57 |
||
58 |
/** |
|
59 |
* A constant holding the negative infinity of type |
|
60 |
* {@code float}. It is equal to the value returned by |
|
61 |
* {@code Float.intBitsToFloat(0xff800000)}. |
|
62 |
*/ |
|
63 |
public static final float NEGATIVE_INFINITY = -1.0f / 0.0f; |
|
64 |
||
65 |
/** |
|
66 |
* A constant holding a Not-a-Number (NaN) value of type |
|
67 |
* {@code float}. It is equivalent to the value returned by |
|
68 |
* {@code Float.intBitsToFloat(0x7fc00000)}. |
|
69 |
*/ |
|
70 |
public static final float NaN = 0.0f / 0.0f; |
|
71 |
||
72 |
/** |
|
73 |
* A constant holding the largest positive finite value of type |
|
74 |
* {@code float}, (2-2<sup>-23</sup>)·2<sup>127</sup>. |
|
75 |
* It is equal to the hexadecimal floating-point literal |
|
76 |
* {@code 0x1.fffffeP+127f} and also equal to |
|
77 |
* {@code Float.intBitsToFloat(0x7f7fffff)}. |
|
78 |
*/ |
|
79 |
public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f |
|
80 |
||
81 |
/** |
|
82 |
* A constant holding the smallest positive normal value of type |
|
83 |
* {@code float}, 2<sup>-126</sup>. It is equal to the |
|
84 |
* hexadecimal floating-point literal {@code 0x1.0p-126f} and also |
|
85 |
* equal to {@code Float.intBitsToFloat(0x00800000)}. |
|
86 |
* |
|
87 |
* @since 1.6 |
|
88 |
*/ |
|
89 |
public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f |
|
90 |
||
91 |
/** |
|
92 |
* A constant holding the smallest positive nonzero value of type |
|
93 |
* {@code float}, 2<sup>-149</sup>. It is equal to the |
|
94 |
* hexadecimal floating-point literal {@code 0x0.000002P-126f} |
|
95 |
* and also equal to {@code Float.intBitsToFloat(0x1)}. |
|
96 |
*/ |
|
97 |
public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f |
|
98 |
||
99 |
/** |
|
100 |
* Maximum exponent a finite {@code float} variable may have. It |
|
101 |
* is equal to the value returned by {@code |
|
102 |
* Math.getExponent(Float.MAX_VALUE)}. |
|
103 |
* |
|
104 |
* @since 1.6 |
|
105 |
*/ |
|
106 |
public static final int MAX_EXPONENT = 127; |
|
107 |
||
108 |
/** |
|
109 |
* Minimum exponent a normalized {@code float} variable may have. |
|
110 |
* It is equal to the value returned by {@code |
|
111 |
* Math.getExponent(Float.MIN_NORMAL)}. |
|
112 |
* |
|
113 |
* @since 1.6 |
|
114 |
*/ |
|
115 |
public static final int MIN_EXPONENT = -126; |
|
116 |
||
117 |
/** |
|
118 |
* The number of bits used to represent a {@code float} value. |
|
119 |
* |
|
120 |
* @since 1.5 |
|
121 |
*/ |
|
122 |
public static final int SIZE = 32; |
|
123 |
||
124 |
/** |
|
14507
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
125 |
* The number of bytes used to represent a {@code float} value. |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
126 |
* |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
127 |
* @since 1.8 |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
128 |
*/ |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
129 |
public static final int BYTES = SIZE / Byte.SIZE; |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
130 |
|
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
131 |
/** |
2 | 132 |
* The {@code Class} instance representing the primitive type |
133 |
* {@code float}. |
|
134 |
* |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
22290
diff
changeset
|
135 |
* @since 1.1 |
2 | 136 |
*/ |
11275 | 137 |
@SuppressWarnings("unchecked") |
138 |
public static final Class<Float> TYPE = (Class<Float>) Class.getPrimitiveClass("float"); |
|
2 | 139 |
|
140 |
/** |
|
141 |
* Returns a string representation of the {@code float} |
|
142 |
* argument. All characters mentioned below are ASCII characters. |
|
143 |
* <ul> |
|
144 |
* <li>If the argument is NaN, the result is the string |
|
145 |
* "{@code NaN}". |
|
146 |
* <li>Otherwise, the result is a string that represents the sign and |
|
147 |
* magnitude (absolute value) of the argument. If the sign is |
|
148 |
* negative, the first character of the result is |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11275
diff
changeset
|
149 |
* '{@code -}' ({@code '\u005Cu002D'}); if the sign is |
2 | 150 |
* positive, no sign character appears in the result. As for |
151 |
* the magnitude <i>m</i>: |
|
152 |
* <ul> |
|
153 |
* <li>If <i>m</i> is infinity, it is represented by the characters |
|
154 |
* {@code "Infinity"}; thus, positive infinity produces |
|
155 |
* the result {@code "Infinity"} and negative infinity |
|
156 |
* produces the result {@code "-Infinity"}. |
|
157 |
* <li>If <i>m</i> is zero, it is represented by the characters |
|
158 |
* {@code "0.0"}; thus, negative zero produces the result |
|
159 |
* {@code "-0.0"} and positive zero produces the result |
|
160 |
* {@code "0.0"}. |
|
161 |
* <li> If <i>m</i> is greater than or equal to 10<sup>-3</sup> but |
|
162 |
* less than 10<sup>7</sup>, then it is represented as the |
|
163 |
* integer part of <i>m</i>, in decimal form with no leading |
|
164 |
* zeroes, followed by '{@code .}' |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11275
diff
changeset
|
165 |
* ({@code '\u005Cu002E'}), followed by one or more |
2 | 166 |
* decimal digits representing the fractional part of |
167 |
* <i>m</i>. |
|
168 |
* <li> If <i>m</i> is less than 10<sup>-3</sup> or greater than or |
|
169 |
* equal to 10<sup>7</sup>, then it is represented in |
|
170 |
* so-called "computerized scientific notation." Let <i>n</i> |
|
171 |
* be the unique integer such that 10<sup><i>n</i> </sup>≤ |
|
172 |
* <i>m</i> {@literal <} 10<sup><i>n</i>+1</sup>; then let <i>a</i> |
|
173 |
* be the mathematically exact quotient of <i>m</i> and |
|
174 |
* 10<sup><i>n</i></sup> so that 1 ≤ <i>a</i> {@literal <} 10. |
|
175 |
* The magnitude is then represented as the integer part of |
|
176 |
* <i>a</i>, as a single decimal digit, followed by |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11275
diff
changeset
|
177 |
* '{@code .}' ({@code '\u005Cu002E'}), followed by |
2 | 178 |
* decimal digits representing the fractional part of |
179 |
* <i>a</i>, followed by the letter '{@code E}' |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11275
diff
changeset
|
180 |
* ({@code '\u005Cu0045'}), followed by a representation |
2 | 181 |
* of <i>n</i> as a decimal integer, as produced by the |
182 |
* method {@link java.lang.Integer#toString(int)}. |
|
183 |
* |
|
184 |
* </ul> |
|
185 |
* </ul> |
|
186 |
* How many digits must be printed for the fractional part of |
|
187 |
* <i>m</i> or <i>a</i>? There must be at least one digit |
|
188 |
* to represent the fractional part, and beyond that as many, but |
|
189 |
* only as many, more digits as are needed to uniquely distinguish |
|
190 |
* the argument value from adjacent values of type |
|
191 |
* {@code float}. That is, suppose that <i>x</i> is the |
|
192 |
* exact mathematical value represented by the decimal |
|
193 |
* representation produced by this method for a finite nonzero |
|
194 |
* argument <i>f</i>. Then <i>f</i> must be the {@code float} |
|
195 |
* value nearest to <i>x</i>; or, if two {@code float} values are |
|
196 |
* equally close to <i>x</i>, then <i>f</i> must be one of |
|
197 |
* them and the least significant bit of the significand of |
|
198 |
* <i>f</i> must be {@code 0}. |
|
199 |
* |
|
200 |
* <p>To create localized string representations of a floating-point |
|
201 |
* value, use subclasses of {@link java.text.NumberFormat}. |
|
202 |
* |
|
203 |
* @param f the float to be converted. |
|
204 |
* @return a string representation of the argument. |
|
205 |
*/ |
|
206 |
public static String toString(float f) { |
|
18143
b6ef7bd945ce
7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
15311
diff
changeset
|
207 |
return FloatingDecimal.toJavaFormatString(f); |
2 | 208 |
} |
209 |
||
210 |
/** |
|
211 |
* Returns a hexadecimal string representation of the |
|
212 |
* {@code float} argument. All characters mentioned below are |
|
213 |
* ASCII characters. |
|
214 |
* |
|
215 |
* <ul> |
|
216 |
* <li>If the argument is NaN, the result is the string |
|
217 |
* "{@code NaN}". |
|
218 |
* <li>Otherwise, the result is a string that represents the sign and |
|
219 |
* magnitude (absolute value) of the argument. If the sign is negative, |
|
220 |
* the first character of the result is '{@code -}' |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11275
diff
changeset
|
221 |
* ({@code '\u005Cu002D'}); if the sign is positive, no sign character |
2 | 222 |
* appears in the result. As for the magnitude <i>m</i>: |
223 |
* |
|
224 |
* <ul> |
|
225 |
* <li>If <i>m</i> is infinity, it is represented by the string |
|
226 |
* {@code "Infinity"}; thus, positive infinity produces the |
|
227 |
* result {@code "Infinity"} and negative infinity produces |
|
228 |
* the result {@code "-Infinity"}. |
|
229 |
* |
|
230 |
* <li>If <i>m</i> is zero, it is represented by the string |
|
231 |
* {@code "0x0.0p0"}; thus, negative zero produces the result |
|
232 |
* {@code "-0x0.0p0"} and positive zero produces the result |
|
233 |
* {@code "0x0.0p0"}. |
|
234 |
* |
|
235 |
* <li>If <i>m</i> is a {@code float} value with a |
|
236 |
* normalized representation, substrings are used to represent the |
|
237 |
* significand and exponent fields. The significand is |
|
238 |
* represented by the characters {@code "0x1."} |
|
239 |
* followed by a lowercase hexadecimal representation of the rest |
|
240 |
* of the significand as a fraction. Trailing zeros in the |
|
241 |
* hexadecimal representation are removed unless all the digits |
|
242 |
* are zero, in which case a single zero is used. Next, the |
|
243 |
* exponent is represented by {@code "p"} followed |
|
244 |
* by a decimal string of the unbiased exponent as if produced by |
|
245 |
* a call to {@link Integer#toString(int) Integer.toString} on the |
|
246 |
* exponent value. |
|
247 |
* |
|
248 |
* <li>If <i>m</i> is a {@code float} value with a subnormal |
|
249 |
* representation, the significand is represented by the |
|
250 |
* characters {@code "0x0."} followed by a |
|
251 |
* hexadecimal representation of the rest of the significand as a |
|
252 |
* fraction. Trailing zeros in the hexadecimal representation are |
|
253 |
* removed. Next, the exponent is represented by |
|
254 |
* {@code "p-126"}. Note that there must be at |
|
255 |
* least one nonzero digit in a subnormal significand. |
|
256 |
* |
|
257 |
* </ul> |
|
258 |
* |
|
259 |
* </ul> |
|
260 |
* |
|
261 |
* <table border> |
|
18776 | 262 |
* <caption>Examples</caption> |
2 | 263 |
* <tr><th>Floating-point Value</th><th>Hexadecimal String</th> |
264 |
* <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td> |
|
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 |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
10067
diff
changeset
|
290 |
String s = Double.toHexString(Math.scalb((double)f, |
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
10067
diff
changeset
|
291 |
/* -1022+126 */ |
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
10067
diff
changeset
|
292 |
DoubleConsts.MIN_EXPONENT- |
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
10067
diff
changeset
|
293 |
FloatConsts.MIN_EXPONENT)); |
2 | 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: |
|
314 |
* |
|
315 |
* <blockquote> |
|
316 |
* <dl> |
|
317 |
* <dt><i>FloatValue:</i> |
|
318 |
* <dd><i>Sign<sub>opt</sub></i> {@code NaN} |
|
319 |
* <dd><i>Sign<sub>opt</sub></i> {@code Infinity} |
|
320 |
* <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i> |
|
321 |
* <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i> |
|
322 |
* <dd><i>SignedInteger</i> |
|
323 |
* </dl> |
|
324 |
* |
|
325 |
* <dl> |
|
326 |
* <dt><i>HexFloatingPointLiteral</i>: |
|
327 |
* <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i> |
|
328 |
* </dl> |
|
329 |
* |
|
330 |
* <dl> |
|
331 |
* <dt><i>HexSignificand:</i> |
|
332 |
* <dd><i>HexNumeral</i> |
|
333 |
* <dd><i>HexNumeral</i> {@code .} |
|
334 |
* <dd>{@code 0x} <i>HexDigits<sub>opt</sub> |
|
335 |
* </i>{@code .}<i> HexDigits</i> |
|
336 |
* <dd>{@code 0X}<i> HexDigits<sub>opt</sub> |
|
337 |
* </i>{@code .} <i>HexDigits</i> |
|
338 |
* </dl> |
|
339 |
* |
|
340 |
* <dl> |
|
341 |
* <dt><i>BinaryExponent:</i> |
|
342 |
* <dd><i>BinaryExponentIndicator SignedInteger</i> |
|
343 |
* </dl> |
|
344 |
* |
|
345 |
* <dl> |
|
346 |
* <dt><i>BinaryExponentIndicator:</i> |
|
347 |
* <dd>{@code p} |
|
348 |
* <dd>{@code P} |
|
349 |
* </dl> |
|
350 |
* |
|
351 |
* </blockquote> |
|
352 |
* |
|
353 |
* where <i>Sign</i>, <i>FloatingPointLiteral</i>, |
|
354 |
* <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and |
|
355 |
* <i>FloatTypeSuffix</i> are as defined in the lexical structure |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
7517
diff
changeset
|
356 |
* sections of |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
7517
diff
changeset
|
357 |
* <cite>The Java™ Language Specification</cite>, |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
7517
diff
changeset
|
358 |
* except that underscores are not accepted between digits. |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
7517
diff
changeset
|
359 |
* If {@code s} does not have the form of |
2 | 360 |
* a <i>FloatValue</i>, then a {@code NumberFormatException} |
361 |
* is thrown. Otherwise, {@code s} is regarded as |
|
362 |
* representing an exact decimal value in the usual |
|
363 |
* "computerized scientific notation" or as an exact |
|
364 |
* hexadecimal value; this exact numerical value is then |
|
365 |
* conceptually converted to an "infinitely precise" |
|
366 |
* binary value that is then rounded to type {@code float} |
|
367 |
* by the usual round-to-nearest rule of IEEE 754 floating-point |
|
368 |
* arithmetic, which includes preserving the sign of a zero |
|
1824
7a685390c6ab
6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents:
2
diff
changeset
|
369 |
* value. |
7a685390c6ab
6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents:
2
diff
changeset
|
370 |
* |
7a685390c6ab
6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents:
2
diff
changeset
|
371 |
* Note that the round-to-nearest rule also implies overflow and |
7a685390c6ab
6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents:
2
diff
changeset
|
372 |
* underflow behaviour; if the exact value of {@code s} is large |
7a685390c6ab
6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents:
2
diff
changeset
|
373 |
* enough in magnitude (greater than or equal to ({@link |
7a685390c6ab
6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents:
2
diff
changeset
|
374 |
* #MAX_VALUE} + {@link Math#ulp(float) ulp(MAX_VALUE)}/2), |
7a685390c6ab
6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents:
2
diff
changeset
|
375 |
* rounding to {@code float} will result in an infinity and if the |
7a685390c6ab
6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents:
2
diff
changeset
|
376 |
* exact value of {@code s} is small enough in magnitude (less |
7a685390c6ab
6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents:
2
diff
changeset
|
377 |
* than or equal to {@link #MIN_VALUE}/2), rounding to float will |
7a685390c6ab
6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents:
2
diff
changeset
|
378 |
* result in a zero. |
7a685390c6ab
6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents:
2
diff
changeset
|
379 |
* |
7a685390c6ab
6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents:
2
diff
changeset
|
380 |
* Finally, after rounding a {@code Float} object representing |
7a685390c6ab
6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents:
2
diff
changeset
|
381 |
* this {@code float} value is returned. |
2 | 382 |
* |
383 |
* <p>To interpret localized string representations of a |
|
384 |
* floating-point value, use subclasses of {@link |
|
385 |
* java.text.NumberFormat}. |
|
386 |
* |
|
387 |
* <p>Note that trailing format specifiers, specifiers that |
|
388 |
* determine the type of a floating-point literal |
|
389 |
* ({@code 1.0f} is a {@code float} value; |
|
390 |
* {@code 1.0d} is a {@code double} value), do |
|
391 |
* <em>not</em> influence the results of this method. In other |
|
392 |
* words, the numerical value of the input string is converted |
|
393 |
* directly to the target floating-point type. In general, the |
|
394 |
* two-step sequence of conversions, string to {@code double} |
|
395 |
* followed by {@code double} to {@code float}, is |
|
396 |
* <em>not</em> equivalent to converting a string directly to |
|
397 |
* {@code float}. For example, if first converted to an |
|
398 |
* intermediate {@code double} and then to |
|
399 |
* {@code float}, the string<br> |
|
400 |
* {@code "1.00000017881393421514957253748434595763683319091796875001d"}<br> |
|
401 |
* results in the {@code float} value |
|
402 |
* {@code 1.0000002f}; if the string is converted directly to |
|
403 |
* {@code float}, <code>1.000000<b>1</b>f</code> results. |
|
404 |
* |
|
405 |
* <p>To avoid calling this method on an invalid string and having |
|
406 |
* a {@code NumberFormatException} be thrown, the documentation |
|
407 |
* for {@link Double#valueOf Double.valueOf} lists a regular |
|
408 |
* expression which can be used to screen the input. |
|
409 |
* |
|
410 |
* @param s the string to be parsed. |
|
411 |
* @return a {@code Float} object holding the value |
|
412 |
* represented by the {@code String} argument. |
|
413 |
* @throws NumberFormatException if the string does not contain a |
|
414 |
* parsable number. |
|
415 |
*/ |
|
416 |
public static Float valueOf(String s) throws NumberFormatException { |
|
18143
b6ef7bd945ce
7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
15311
diff
changeset
|
417 |
return new Float(parseFloat(s)); |
2 | 418 |
} |
419 |
||
420 |
/** |
|
421 |
* Returns a {@code Float} instance representing the specified |
|
422 |
* {@code float} value. |
|
423 |
* If a new {@code Float} instance is not required, this method |
|
424 |
* should generally be used in preference to the constructor |
|
425 |
* {@link #Float(float)}, as this method is likely to yield |
|
426 |
* significantly better space and time performance by caching |
|
427 |
* frequently requested values. |
|
428 |
* |
|
429 |
* @param f a float value. |
|
430 |
* @return a {@code Float} instance representing {@code f}. |
|
431 |
* @since 1.5 |
|
432 |
*/ |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
433 |
@HotSpotIntrinsicCandidate |
2 | 434 |
public static Float valueOf(float f) { |
435 |
return new Float(f); |
|
436 |
} |
|
437 |
||
438 |
/** |
|
439 |
* Returns a new {@code float} initialized to the value |
|
440 |
* represented by the specified {@code String}, as performed |
|
441 |
* by the {@code valueOf} method of class {@code Float}. |
|
442 |
* |
|
3312
d8cd9665ece8
6463998: Undocumented NullPointerExeption from Float.parseFloat and Double.parseDouble
darcy
parents:
1824
diff
changeset
|
443 |
* @param s the string to be parsed. |
2 | 444 |
* @return the {@code float} value represented by the string |
445 |
* argument. |
|
3312
d8cd9665ece8
6463998: Undocumented NullPointerExeption from Float.parseFloat and Double.parseDouble
darcy
parents:
1824
diff
changeset
|
446 |
* @throws NullPointerException if the string is null |
d8cd9665ece8
6463998: Undocumented NullPointerExeption from Float.parseFloat and Double.parseDouble
darcy
parents:
1824
diff
changeset
|
447 |
* @throws NumberFormatException if the string does not contain a |
2 | 448 |
* parsable {@code float}. |
3312
d8cd9665ece8
6463998: Undocumented NullPointerExeption from Float.parseFloat and Double.parseDouble
darcy
parents:
1824
diff
changeset
|
449 |
* @see java.lang.Float#valueOf(String) |
2 | 450 |
* @since 1.2 |
451 |
*/ |
|
452 |
public static float parseFloat(String s) throws NumberFormatException { |
|
18143
b6ef7bd945ce
7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
15311
diff
changeset
|
453 |
return FloatingDecimal.parseFloat(s); |
2 | 454 |
} |
455 |
||
456 |
/** |
|
457 |
* Returns {@code true} if the specified number is a |
|
458 |
* Not-a-Number (NaN) value, {@code false} otherwise. |
|
459 |
* |
|
460 |
* @param v the value to be tested. |
|
461 |
* @return {@code true} if the argument is NaN; |
|
462 |
* {@code false} otherwise. |
|
463 |
*/ |
|
10608 | 464 |
public static boolean isNaN(float v) { |
2 | 465 |
return (v != v); |
466 |
} |
|
467 |
||
468 |
/** |
|
469 |
* Returns {@code true} if the specified number is infinitely |
|
470 |
* large in magnitude, {@code false} otherwise. |
|
471 |
* |
|
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 |
*/ |
|
10608 | 476 |
public static boolean isInfinite(float v) { |
2 | 477 |
return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY); |
478 |
} |
|
479 |
||
10608 | 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 |
||
2 | 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 |
/** |
|
513 |
* Constructs a newly allocated {@code Float} object that |
|
514 |
* represents the argument converted to type {@code float}. |
|
515 |
* |
|
516 |
* @param value the value to be represented by the {@code Float}. |
|
517 |
*/ |
|
518 |
public Float(double value) { |
|
519 |
this.value = (float)value; |
|
520 |
} |
|
521 |
||
522 |
/** |
|
523 |
* Constructs a newly allocated {@code Float} object that |
|
524 |
* represents the floating-point value of type {@code float} |
|
525 |
* represented by the string. The string is converted to a |
|
526 |
* {@code float} value as if by the {@code valueOf} method. |
|
527 |
* |
|
528 |
* @param s a string to be converted to a {@code Float}. |
|
529 |
* @throws NumberFormatException if the string does not contain a |
|
530 |
* parsable number. |
|
531 |
* @see java.lang.Float#valueOf(java.lang.String) |
|
532 |
*/ |
|
533 |
public Float(String s) throws NumberFormatException { |
|
11016
e2665f4ac6d2
7110111: Minor Java SE javadoc & Constructor clean up
lancea
parents:
10608
diff
changeset
|
534 |
value = parseFloat(s); |
2 | 535 |
} |
536 |
||
537 |
/** |
|
538 |
* Returns {@code true} if this {@code Float} value is a |
|
539 |
* Not-a-Number (NaN), {@code false} otherwise. |
|
540 |
* |
|
541 |
* @return {@code true} if the value represented by this object is |
|
542 |
* NaN; {@code false} otherwise. |
|
543 |
*/ |
|
544 |
public boolean isNaN() { |
|
545 |
return isNaN(value); |
|
546 |
} |
|
547 |
||
548 |
/** |
|
549 |
* Returns {@code true} if this {@code Float} value is |
|
550 |
* infinitely large in magnitude, {@code false} otherwise. |
|
551 |
* |
|
552 |
* @return {@code true} if the value represented by this object is |
|
553 |
* positive infinity or negative infinity; |
|
554 |
* {@code false} otherwise. |
|
555 |
*/ |
|
556 |
public boolean isInfinite() { |
|
557 |
return isInfinite(value); |
|
558 |
} |
|
559 |
||
560 |
/** |
|
561 |
* Returns a string representation of this {@code Float} object. |
|
562 |
* The primitive {@code float} value represented by this object |
|
563 |
* is converted to a {@code String} exactly as if by the method |
|
564 |
* {@code toString} of one argument. |
|
565 |
* |
|
566 |
* @return a {@code String} representation of this object. |
|
567 |
* @see java.lang.Float#toString(float) |
|
568 |
*/ |
|
569 |
public String toString() { |
|
3964
cf913644be58
6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents:
3312
diff
changeset
|
570 |
return Float.toString(value); |
2 | 571 |
} |
572 |
||
573 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
574 |
* Returns the value of this {@code Float} as a {@code byte} after |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
575 |
* a narrowing primitive conversion. |
2 | 576 |
* |
577 |
* @return the {@code float} value represented by this object |
|
578 |
* converted to type {@code byte} |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
579 |
* @jls 5.1.3 Narrowing Primitive Conversions |
2 | 580 |
*/ |
581 |
public byte byteValue() { |
|
582 |
return (byte)value; |
|
583 |
} |
|
584 |
||
585 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
586 |
* Returns the value of this {@code Float} as a {@code short} |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
587 |
* after a narrowing primitive conversion. |
2 | 588 |
* |
589 |
* @return the {@code float} value represented by this object |
|
590 |
* converted to type {@code short} |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
591 |
* @jls 5.1.3 Narrowing Primitive Conversions |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
22290
diff
changeset
|
592 |
* @since 1.1 |
2 | 593 |
*/ |
594 |
public short shortValue() { |
|
595 |
return (short)value; |
|
596 |
} |
|
597 |
||
598 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
599 |
* Returns the value of this {@code Float} as an {@code int} after |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
600 |
* a narrowing primitive conversion. |
2 | 601 |
* |
602 |
* @return the {@code float} value represented by this object |
|
603 |
* converted to type {@code int} |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
604 |
* @jls 5.1.3 Narrowing Primitive Conversions |
2 | 605 |
*/ |
606 |
public int intValue() { |
|
607 |
return (int)value; |
|
608 |
} |
|
609 |
||
610 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
611 |
* Returns value of this {@code Float} as a {@code long} after a |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
612 |
* narrowing primitive conversion. |
2 | 613 |
* |
614 |
* @return the {@code float} value represented by this object |
|
615 |
* converted to type {@code long} |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
616 |
* @jls 5.1.3 Narrowing Primitive Conversions |
2 | 617 |
*/ |
618 |
public long longValue() { |
|
619 |
return (long)value; |
|
620 |
} |
|
621 |
||
622 |
/** |
|
623 |
* Returns the {@code float} value of this {@code Float} object. |
|
624 |
* |
|
625 |
* @return the {@code float} value represented by this object |
|
626 |
*/ |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
627 |
@HotSpotIntrinsicCandidate |
2 | 628 |
public float floatValue() { |
629 |
return value; |
|
630 |
} |
|
631 |
||
632 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
633 |
* Returns the value of this {@code Float} as a {@code double} |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
634 |
* after a widening primitive conversion. |
2 | 635 |
* |
636 |
* @return the {@code float} value represented by this |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
637 |
* object converted to type {@code double} |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
638 |
* @jls 5.1.2 Widening Primitive Conversions |
2 | 639 |
*/ |
640 |
public double doubleValue() { |
|
641 |
return (double)value; |
|
642 |
} |
|
643 |
||
644 |
/** |
|
645 |
* Returns a hash code for this {@code Float} object. The |
|
646 |
* result is the integer bit representation, exactly as produced |
|
647 |
* by the method {@link #floatToIntBits(float)}, of the primitive |
|
648 |
* {@code float} value represented by this {@code Float} |
|
649 |
* object. |
|
650 |
* |
|
651 |
* @return a hash code value for this object. |
|
652 |
*/ |
|
14503
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
653 |
@Override |
2 | 654 |
public int hashCode() { |
14503
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
655 |
return Float.hashCode(value); |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
656 |
} |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
657 |
|
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
658 |
/** |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
659 |
* Returns a hash code for a {@code float} value; compatible with |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
660 |
* {@code Float.hashCode()}. |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
661 |
* |
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
18156
diff
changeset
|
662 |
* @param value the value to hash |
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
18156
diff
changeset
|
663 |
* @return a hash code value for a {@code float} value. |
14503
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
664 |
* @since 1.8 |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
665 |
*/ |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
666 |
public static int hashCode(float value) { |
2 | 667 |
return floatToIntBits(value); |
668 |
} |
|
669 |
||
670 |
/** |
|
671 |
||
672 |
* Compares this object against the specified object. The result |
|
673 |
* is {@code true} if and only if the argument is not |
|
674 |
* {@code null} and is a {@code Float} object that |
|
675 |
* represents a {@code float} with the same value as the |
|
676 |
* {@code float} represented by this object. For this |
|
677 |
* purpose, two {@code float} values are considered to be the |
|
678 |
* same if and only if the method {@link #floatToIntBits(float)} |
|
679 |
* returns the identical {@code int} value when applied to |
|
680 |
* each. |
|
681 |
* |
|
682 |
* <p>Note that in most cases, for two instances of class |
|
683 |
* {@code Float}, {@code f1} and {@code f2}, the value |
|
684 |
* of {@code f1.equals(f2)} is {@code true} if and only if |
|
685 |
* |
|
686 |
* <blockquote><pre> |
|
687 |
* f1.floatValue() == f2.floatValue() |
|
688 |
* </pre></blockquote> |
|
689 |
* |
|
690 |
* <p>also has the value {@code true}. However, there are two exceptions: |
|
691 |
* <ul> |
|
692 |
* <li>If {@code f1} and {@code f2} both represent |
|
693 |
* {@code Float.NaN}, then the {@code equals} method returns |
|
694 |
* {@code true}, even though {@code Float.NaN==Float.NaN} |
|
695 |
* has the value {@code false}. |
|
696 |
* <li>If {@code f1} represents {@code +0.0f} while |
|
697 |
* {@code f2} represents {@code -0.0f}, or vice |
|
698 |
* versa, the {@code equal} test has the value |
|
699 |
* {@code false}, even though {@code 0.0f==-0.0f} |
|
700 |
* has the value {@code true}. |
|
701 |
* </ul> |
|
702 |
* |
|
703 |
* This definition allows hash tables to operate properly. |
|
704 |
* |
|
705 |
* @param obj the object to be compared |
|
706 |
* @return {@code true} if the objects are the same; |
|
707 |
* {@code false} otherwise. |
|
708 |
* @see java.lang.Float#floatToIntBits(float) |
|
709 |
*/ |
|
710 |
public boolean equals(Object obj) { |
|
711 |
return (obj instanceof Float) |
|
712 |
&& (floatToIntBits(((Float)obj).value) == floatToIntBits(value)); |
|
713 |
} |
|
714 |
||
715 |
/** |
|
716 |
* Returns a representation of the specified floating-point value |
|
717 |
* according to the IEEE 754 floating-point "single format" bit |
|
718 |
* layout. |
|
719 |
* |
|
720 |
* <p>Bit 31 (the bit that is selected by the mask |
|
721 |
* {@code 0x80000000}) represents the sign of the floating-point |
|
722 |
* number. |
|
723 |
* Bits 30-23 (the bits that are selected by the mask |
|
724 |
* {@code 0x7f800000}) represent the exponent. |
|
725 |
* Bits 22-0 (the bits that are selected by the mask |
|
726 |
* {@code 0x007fffff}) represent the significand (sometimes called |
|
727 |
* the mantissa) of the floating-point number. |
|
728 |
* |
|
729 |
* <p>If the argument is positive infinity, the result is |
|
730 |
* {@code 0x7f800000}. |
|
731 |
* |
|
732 |
* <p>If the argument is negative infinity, the result is |
|
733 |
* {@code 0xff800000}. |
|
734 |
* |
|
735 |
* <p>If the argument is NaN, the result is {@code 0x7fc00000}. |
|
736 |
* |
|
737 |
* <p>In all cases, the result is an integer that, when given to the |
|
738 |
* {@link #intBitsToFloat(int)} method, will produce a floating-point |
|
739 |
* value the same as the argument to {@code floatToIntBits} |
|
740 |
* (except all NaN values are collapsed to a single |
|
741 |
* "canonical" NaN value). |
|
742 |
* |
|
743 |
* @param value a floating-point number. |
|
744 |
* @return the bits that represent the floating-point number. |
|
745 |
*/ |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
746 |
@HotSpotIntrinsicCandidate |
2 | 747 |
public static int floatToIntBits(float value) { |
22290
6d4551cf4b3e
6667086: Double.doubleToLongBits(final double value) contains inefficient test for NaN
bpb
parents:
21334
diff
changeset
|
748 |
if (!isNaN(value)) { |
6d4551cf4b3e
6667086: Double.doubleToLongBits(final double value) contains inefficient test for NaN
bpb
parents:
21334
diff
changeset
|
749 |
return floatToRawIntBits(value); |
6d4551cf4b3e
6667086: Double.doubleToLongBits(final double value) contains inefficient test for NaN
bpb
parents:
21334
diff
changeset
|
750 |
} |
6d4551cf4b3e
6667086: Double.doubleToLongBits(final double value) contains inefficient test for NaN
bpb
parents:
21334
diff
changeset
|
751 |
return 0x7fc00000; |
2 | 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 |
*/ |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
789 |
@HotSpotIntrinsicCandidate |
2 | 790 |
public static native int floatToRawIntBits(float value); |
791 |
||
792 |
/** |
|
793 |
* Returns the {@code float} value corresponding to a given |
|
794 |
* bit representation. |
|
795 |
* The argument is considered to be a representation of a |
|
796 |
* floating-point value according to the IEEE 754 floating-point |
|
797 |
* "single format" bit layout. |
|
798 |
* |
|
799 |
* <p>If the argument is {@code 0x7f800000}, the result is positive |
|
800 |
* infinity. |
|
801 |
* |
|
802 |
* <p>If the argument is {@code 0xff800000}, the result is negative |
|
803 |
* infinity. |
|
804 |
* |
|
805 |
* <p>If the argument is any value in the range |
|
806 |
* {@code 0x7f800001} through {@code 0x7fffffff} or in |
|
807 |
* the range {@code 0xff800001} through |
|
808 |
* {@code 0xffffffff}, the result is a NaN. No IEEE 754 |
|
809 |
* floating-point operation provided by Java can distinguish |
|
810 |
* between two NaN values of the same type with different bit |
|
811 |
* patterns. Distinct values of NaN are only distinguishable by |
|
812 |
* use of the {@code Float.floatToRawIntBits} method. |
|
813 |
* |
|
814 |
* <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three |
|
815 |
* values that can be computed from the argument: |
|
816 |
* |
|
18156 | 817 |
* <blockquote><pre>{@code |
818 |
* int s = ((bits >> 31) == 0) ? 1 : -1; |
|
819 |
* int e = ((bits >> 23) & 0xff); |
|
2 | 820 |
* int m = (e == 0) ? |
18156 | 821 |
* (bits & 0x7fffff) << 1 : |
2 | 822 |
* (bits & 0x7fffff) | 0x800000; |
18156 | 823 |
* }</pre></blockquote> |
2 | 824 |
* |
825 |
* Then the floating-point result equals the value of the mathematical |
|
826 |
* expression <i>s</i>·<i>m</i>·2<sup><i>e</i>-150</sup>. |
|
827 |
* |
|
828 |
* <p>Note that this method may not be able to return a |
|
829 |
* {@code float} NaN with exactly same bit pattern as the |
|
830 |
* {@code int} argument. IEEE 754 distinguishes between two |
|
831 |
* kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The |
|
832 |
* differences between the two kinds of NaN are generally not |
|
833 |
* visible in Java. Arithmetic operations on signaling NaNs turn |
|
834 |
* them into quiet NaNs with a different, but often similar, bit |
|
835 |
* pattern. However, on some processors merely copying a |
|
836 |
* signaling NaN also performs that conversion. In particular, |
|
837 |
* copying a signaling NaN to return it to the calling method may |
|
838 |
* perform this conversion. So {@code intBitsToFloat} may |
|
839 |
* not be able to return a {@code float} with a signaling NaN |
|
840 |
* bit pattern. Consequently, for some {@code int} values, |
|
841 |
* {@code floatToRawIntBits(intBitsToFloat(start))} may |
|
842 |
* <i>not</i> equal {@code start}. Moreover, which |
|
843 |
* particular bit patterns represent signaling NaNs is platform |
|
844 |
* dependent; although all NaN bit patterns, quiet or signaling, |
|
845 |
* must be in the NaN range identified above. |
|
846 |
* |
|
847 |
* @param bits an integer. |
|
848 |
* @return the {@code float} floating-point value with the same bit |
|
849 |
* pattern. |
|
850 |
*/ |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
851 |
@HotSpotIntrinsicCandidate |
2 | 852 |
public static native float intBitsToFloat(int bits); |
853 |
||
854 |
/** |
|
855 |
* Compares two {@code Float} objects numerically. There are |
|
856 |
* two ways in which comparisons performed by this method differ |
|
857 |
* from those performed by the Java language numerical comparison |
|
858 |
* operators ({@code <, <=, ==, >=, >}) when |
|
859 |
* applied to primitive {@code float} values: |
|
860 |
* |
|
861 |
* <ul><li> |
|
862 |
* {@code Float.NaN} is considered by this method to |
|
863 |
* be equal to itself and greater than all other |
|
864 |
* {@code float} values |
|
865 |
* (including {@code Float.POSITIVE_INFINITY}). |
|
866 |
* <li> |
|
867 |
* {@code 0.0f} is considered by this method to be greater |
|
868 |
* than {@code -0.0f}. |
|
869 |
* </ul> |
|
870 |
* |
|
871 |
* This ensures that the <i>natural ordering</i> of {@code Float} |
|
872 |
* objects imposed by this method is <i>consistent with equals</i>. |
|
873 |
* |
|
874 |
* @param anotherFloat the {@code Float} to be compared. |
|
875 |
* @return the value {@code 0} if {@code anotherFloat} is |
|
876 |
* numerically equal to this {@code Float}; a value |
|
877 |
* less than {@code 0} if this {@code Float} |
|
878 |
* is numerically less than {@code anotherFloat}; |
|
879 |
* and a value greater than {@code 0} if this |
|
880 |
* {@code Float} is numerically greater than |
|
881 |
* {@code anotherFloat}. |
|
882 |
* |
|
883 |
* @since 1.2 |
|
884 |
* @see Comparable#compareTo(Object) |
|
885 |
*/ |
|
886 |
public int compareTo(Float anotherFloat) { |
|
887 |
return Float.compare(value, anotherFloat.value); |
|
888 |
} |
|
889 |
||
890 |
/** |
|
891 |
* Compares the two specified {@code float} values. The sign |
|
892 |
* of the integer value returned is the same as that of the |
|
893 |
* integer that would be returned by the call: |
|
894 |
* <pre> |
|
895 |
* new Float(f1).compareTo(new Float(f2)) |
|
896 |
* </pre> |
|
897 |
* |
|
898 |
* @param f1 the first {@code float} to compare. |
|
899 |
* @param f2 the second {@code float} to compare. |
|
900 |
* @return the value {@code 0} if {@code f1} is |
|
901 |
* numerically equal to {@code f2}; a value less than |
|
902 |
* {@code 0} if {@code f1} is numerically less than |
|
903 |
* {@code f2}; and a value greater than {@code 0} |
|
904 |
* if {@code f1} is numerically greater than |
|
905 |
* {@code f2}. |
|
906 |
* @since 1.4 |
|
907 |
*/ |
|
908 |
public static int compare(float f1, float f2) { |
|
7517
7303bc0e78d6
7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents:
5506
diff
changeset
|
909 |
if (f1 < f2) |
2 | 910 |
return -1; // Neither val is NaN, thisVal is smaller |
911 |
if (f1 > f2) |
|
912 |
return 1; // Neither val is NaN, thisVal is larger |
|
913 |
||
7517
7303bc0e78d6
7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents:
5506
diff
changeset
|
914 |
// Cannot use floatToRawIntBits because of possibility of NaNs. |
7303bc0e78d6
7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents:
5506
diff
changeset
|
915 |
int thisBits = Float.floatToIntBits(f1); |
2 | 916 |
int anotherBits = Float.floatToIntBits(f2); |
917 |
||
918 |
return (thisBits == anotherBits ? 0 : // Values are equal |
|
919 |
(thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN) |
|
920 |
1)); // (0.0, -0.0) or (NaN, !NaN) |
|
921 |
} |
|
922 |
||
15311
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
923 |
/** |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
924 |
* Adds two {@code float} values together as per the + operator. |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
925 |
* |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
926 |
* @param a the first operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
927 |
* @param b the second operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
928 |
* @return the sum of {@code a} and {@code b} |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
929 |
* @jls 4.2.4 Floating-Point Operations |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
930 |
* @see java.util.function.BinaryOperator |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
931 |
* @since 1.8 |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
932 |
*/ |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
933 |
public static float sum(float a, float b) { |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
934 |
return a + b; |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
935 |
} |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
936 |
|
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
937 |
/** |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
938 |
* Returns the greater of two {@code float} values |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
939 |
* as if by calling {@link Math#max(float, float) Math.max}. |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
940 |
* |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
941 |
* @param a the first operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
942 |
* @param b the second operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
943 |
* @return the greater of {@code a} and {@code b} |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
944 |
* @see java.util.function.BinaryOperator |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
945 |
* @since 1.8 |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
946 |
*/ |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
947 |
public static float max(float a, float b) { |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
948 |
return Math.max(a, b); |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
949 |
} |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
950 |
|
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
951 |
/** |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
952 |
* Returns the smaller of two {@code float} values |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
953 |
* as if by calling {@link Math#min(float, float) Math.min}. |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
954 |
* |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
955 |
* @param a the first operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
956 |
* @param b the second operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
957 |
* @return the smaller of {@code a} and {@code b} |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
958 |
* @see java.util.function.BinaryOperator |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
959 |
* @since 1.8 |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
960 |
*/ |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
961 |
public static float min(float a, float b) { |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
962 |
return Math.min(a, b); |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
963 |
} |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
14507
diff
changeset
|
964 |
|
2 | 965 |
/** use serialVersionUID from JDK 1.0.2 for interoperability */ |
966 |
private static final long serialVersionUID = -2671257302660747028L; |
|
967 |
} |