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