1 | /* Float.java -- object wrapper for float
|
---|
2 | Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
6 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
21 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 |
|
---|
39 | package java.lang;
|
---|
40 |
|
---|
41 | import gnu.classpath.Configuration;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Instances of class <code>Float</code> represent primitive
|
---|
45 | * <code>float</code> values.
|
---|
46 | *
|
---|
47 | * Additionally, this class provides various helper functions and variables
|
---|
48 | * related to floats.
|
---|
49 | *
|
---|
50 | * @author Paul Fisher
|
---|
51 | * @author Andrew Haley <aph@cygnus.com>
|
---|
52 | * @author Eric Blake <ebb9@email.byu.edu>
|
---|
53 | * @since 1.0
|
---|
54 | * @status updated to 1.4
|
---|
55 | */
|
---|
56 | public final class Float extends Number implements Comparable
|
---|
57 | {
|
---|
58 | /**
|
---|
59 | * Compatible with JDK 1.0+.
|
---|
60 | */
|
---|
61 | private static final long serialVersionUID = -2671257302660747028L;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * The maximum positive value a <code>double</code> may represent
|
---|
65 | * is 3.4028235e+38f.
|
---|
66 | */
|
---|
67 | public static final float MAX_VALUE = 3.4028235e+38f;
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * The minimum positive value a <code>float</code> may represent
|
---|
71 | * is 1.4e-45.
|
---|
72 | */
|
---|
73 | public static final float MIN_VALUE = 1.4e-45f;
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * The value of a float representation -1.0/0.0, negative infinity.
|
---|
77 | */
|
---|
78 | public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * The value of a float representation 1.0/0.0, positive infinity.
|
---|
82 | */
|
---|
83 | public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * All IEEE 754 values of NaN have the same value in Java.
|
---|
87 | */
|
---|
88 | public static final float NaN = 0.0f / 0.0f;
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * The primitive type <code>float</code> is represented by this
|
---|
92 | * <code>Class</code> object.
|
---|
93 | * @since 1.1
|
---|
94 | */
|
---|
95 | public static final Class TYPE = VMClassLoader.getPrimitiveClass('F');
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * The immutable value of this Float.
|
---|
99 | *
|
---|
100 | * @serial the wrapped float
|
---|
101 | */
|
---|
102 | private final float value;
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Load native routines necessary for this class.
|
---|
106 | */
|
---|
107 | static
|
---|
108 | {
|
---|
109 | if (Configuration.INIT_LOAD_LIBRARY)
|
---|
110 | {
|
---|
111 | System.loadLibrary("javalang");
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Create a <code>Float</code> from the primitive <code>float</code>
|
---|
117 | * specified.
|
---|
118 | *
|
---|
119 | * @param value the <code>float</code> argument
|
---|
120 | */
|
---|
121 | public Float(float value)
|
---|
122 | {
|
---|
123 | this.value = value;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Create a <code>Float</code> from the primitive <code>double</code>
|
---|
128 | * specified.
|
---|
129 | *
|
---|
130 | * @param value the <code>double</code> argument
|
---|
131 | */
|
---|
132 | public Float(double value)
|
---|
133 | {
|
---|
134 | this.value = (float) value;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Create a <code>Float</code> from the specified <code>String</code>.
|
---|
139 | * This method calls <code>Float.parseFloat()</code>.
|
---|
140 | *
|
---|
141 | * @param s the <code>String</code> to convert
|
---|
142 | * @throws NumberFormatException if <code>s</code> cannot be parsed as a
|
---|
143 | * <code>float</code>
|
---|
144 | * @throws NullPointerException if <code>s</code> is null
|
---|
145 | * @see #parseFloat(String)
|
---|
146 | */
|
---|
147 | public Float(String s)
|
---|
148 | {
|
---|
149 | value = parseFloat(s);
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Convert the <code>float</code> to a <code>String</code>.
|
---|
154 | * Floating-point string representation is fairly complex: here is a
|
---|
155 | * rundown of the possible values. "<code>[-]</code>" indicates that a
|
---|
156 | * negative sign will be printed if the value (or exponent) is negative.
|
---|
157 | * "<code><number></code>" means a string of digits ('0' to '9').
|
---|
158 | * "<code><digit></code>" means a single digit ('0' to '9').<br>
|
---|
159 | *
|
---|
160 | * <table border=1>
|
---|
161 | * <tr><th>Value of Float</th><th>String Representation</th></tr>
|
---|
162 | * <tr><td>[+-] 0</td> <td><code>[-]0.0</code></td></tr>
|
---|
163 | * <tr><td>Between [+-] 10<sup>-3</sup> and 10<sup>7</sup>, exclusive</td>
|
---|
164 | * <td><code>[-]number.number</code></td></tr>
|
---|
165 | * <tr><td>Other numeric value</td>
|
---|
166 | * <td><code>[-]<digit>.<number>
|
---|
167 | * E[-]<number></code></td></tr>
|
---|
168 | * <tr><td>[+-] infinity</td> <td><code>[-]Infinity</code></td></tr>
|
---|
169 | * <tr><td>NaN</td> <td><code>NaN</code></td></tr>
|
---|
170 | * </table>
|
---|
171 | *
|
---|
172 | * Yes, negative zero <em>is</em> a possible value. Note that there is
|
---|
173 | * <em>always</em> a <code>.</code> and at least one digit printed after
|
---|
174 | * it: even if the number is 3, it will be printed as <code>3.0</code>.
|
---|
175 | * After the ".", all digits will be printed except trailing zeros. The
|
---|
176 | * result is rounded to the shortest decimal number which will parse back
|
---|
177 | * to the same float.
|
---|
178 | *
|
---|
179 | * <p>To create other output formats, use {@link java.text.NumberFormat}.
|
---|
180 | *
|
---|
181 | * @XXX specify where we are not in accord with the spec.
|
---|
182 | *
|
---|
183 | * @param f the <code>float</code> to convert
|
---|
184 | * @return the <code>String</code> representing the <code>float</code>
|
---|
185 | */
|
---|
186 | public static String toString(float f)
|
---|
187 | {
|
---|
188 | return Double.toString(f, true);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Creates a new <code>Float</code> object using the <code>String</code>.
|
---|
193 | *
|
---|
194 | * @param s the <code>String</code> to convert
|
---|
195 | * @return the new <code>Float</code>
|
---|
196 | * @throws NumberFormatException if <code>s</code> cannot be parsed as a
|
---|
197 | * <code>float</code>
|
---|
198 | * @throws NullPointerException if <code>s</code> is null
|
---|
199 | * @see #parseFloat(String)
|
---|
200 | */
|
---|
201 | public static Float valueOf(String s)
|
---|
202 | {
|
---|
203 | return new Float(parseFloat(s));
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Parse the specified <code>String</code> as a <code>float</code>. The
|
---|
208 | * extended BNF grammar is as follows:<br>
|
---|
209 | * <pre>
|
---|
210 | * <em>DecodableString</em>:
|
---|
211 | * ( [ <code>-</code> | <code>+</code> ] <code>NaN</code> )
|
---|
212 | * | ( [ <code>-</code> | <code>+</code> ] <code>Infinity</code> )
|
---|
213 | * | ( [ <code>-</code> | <code>+</code> ] <em>FloatingPoint</em>
|
---|
214 | * [ <code>f</code> | <code>F</code> | <code>d</code>
|
---|
215 | * | <code>D</code>] )
|
---|
216 | * <em>FloatingPoint</em>:
|
---|
217 | * ( { <em>Digit</em> }+ [ <code>.</code> { <em>Digit</em> } ]
|
---|
218 | * [ <em>Exponent</em> ] )
|
---|
219 | * | ( <code>.</code> { <em>Digit</em> }+ [ <em>Exponent</em> ] )
|
---|
220 | * <em>Exponent</em>:
|
---|
221 | * ( ( <code>e</code> | <code>E</code> )
|
---|
222 | * [ <code>-</code> | <code>+</code> ] { <em>Digit</em> }+ )
|
---|
223 | * <em>Digit</em>: <em><code>'0'</code> through <code>'9'</code></em>
|
---|
224 | * </pre>
|
---|
225 | *
|
---|
226 | * <p>NaN and infinity are special cases, to allow parsing of the output
|
---|
227 | * of toString. Otherwise, the result is determined by calculating
|
---|
228 | * <em>n * 10<sup>exponent</sup></em> to infinite precision, then rounding
|
---|
229 | * to the nearest float. Remember that many numbers cannot be precisely
|
---|
230 | * represented in floating point. In case of overflow, infinity is used,
|
---|
231 | * and in case of underflow, signed zero is used. Unlike Integer.parseInt,
|
---|
232 | * this does not accept Unicode digits outside the ASCII range.
|
---|
233 | *
|
---|
234 | * <p>If an unexpected character is found in the <code>String</code>, a
|
---|
235 | * <code>NumberFormatException</code> will be thrown. Leading and trailing
|
---|
236 | * 'whitespace' is ignored via <code>String.trim()</code>, but spaces
|
---|
237 | * internal to the actual number are not allowed.
|
---|
238 | *
|
---|
239 | * <p>To parse numbers according to another format, consider using
|
---|
240 | * {@link java.text.NumberFormat}.
|
---|
241 | *
|
---|
242 | * @XXX specify where/how we are not in accord with the spec.
|
---|
243 | *
|
---|
244 | * @param str the <code>String</code> to convert
|
---|
245 | * @return the <code>float</code> value of <code>s</code>
|
---|
246 | * @throws NumberFormatException if <code>s</code> cannot be parsed as a
|
---|
247 | * <code>float</code>
|
---|
248 | * @throws NullPointerException if <code>s</code> is null
|
---|
249 | * @see #MIN_VALUE
|
---|
250 | * @see #MAX_VALUE
|
---|
251 | * @see #POSITIVE_INFINITY
|
---|
252 | * @see #NEGATIVE_INFINITY
|
---|
253 | * @since 1.2
|
---|
254 | */
|
---|
255 | public static float parseFloat(String s)
|
---|
256 | {
|
---|
257 | // XXX Rounding parseDouble() causes some errors greater than 1 ulp from
|
---|
258 | // the infinitely precise decimal.
|
---|
259 | return (float) Double.parseDouble(s);
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Return <code>true</code> if the <code>float</code> has the same
|
---|
264 | * value as <code>NaN</code>, otherwise return <code>false</code>.
|
---|
265 | *
|
---|
266 | * @param v the <code>float</code> to compare
|
---|
267 | * @return whether the argument is <code>NaN</code>
|
---|
268 | */
|
---|
269 | public static boolean isNaN(float v)
|
---|
270 | {
|
---|
271 | // This works since NaN != NaN is the only reflexive inequality
|
---|
272 | // comparison which returns true.
|
---|
273 | return v != v;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Return <code>true</code> if the <code>float</code> has a value
|
---|
278 | * equal to either <code>NEGATIVE_INFINITY</code> or
|
---|
279 | * <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
|
---|
280 | *
|
---|
281 | * @param v the <code>float</code> to compare
|
---|
282 | * @return whether the argument is (-/+) infinity
|
---|
283 | */
|
---|
284 | public static boolean isInfinite(float v)
|
---|
285 | {
|
---|
286 | return v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY;
|
---|
287 | }
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Return <code>true</code> if the value of this <code>Float</code>
|
---|
291 | * is the same as <code>NaN</code>, otherwise return <code>false</code>.
|
---|
292 | *
|
---|
293 | * @return whether this <code>Float</code> is <code>NaN</code>
|
---|
294 | */
|
---|
295 | public boolean isNaN()
|
---|
296 | {
|
---|
297 | return isNaN(value);
|
---|
298 | }
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Return <code>true</code> if the value of this <code>Float</code>
|
---|
302 | * is the same as <code>NEGATIVE_INFINITY</code> or
|
---|
303 | * <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
|
---|
304 | *
|
---|
305 | * @return whether this <code>Float</code> is (-/+) infinity
|
---|
306 | */
|
---|
307 | public boolean isInfinite()
|
---|
308 | {
|
---|
309 | return isInfinite(value);
|
---|
310 | }
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * Convert the <code>float</code> value of this <code>Float</code>
|
---|
314 | * to a <code>String</code>. This method calls
|
---|
315 | * <code>Float.toString(float)</code> to do its dirty work.
|
---|
316 | *
|
---|
317 | * @return the <code>String</code> representation
|
---|
318 | * @see #toString(float)
|
---|
319 | */
|
---|
320 | public String toString()
|
---|
321 | {
|
---|
322 | return toString(value);
|
---|
323 | }
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Return the value of this <code>Float</code> as a <code>byte</code>.
|
---|
327 | *
|
---|
328 | * @return the byte value
|
---|
329 | * @since 1.1
|
---|
330 | */
|
---|
331 | public byte byteValue()
|
---|
332 | {
|
---|
333 | return (byte) value;
|
---|
334 | }
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * Return the value of this <code>Float</code> as a <code>short</code>.
|
---|
338 | *
|
---|
339 | * @return the short value
|
---|
340 | * @since 1.1
|
---|
341 | */
|
---|
342 | public short shortValue()
|
---|
343 | {
|
---|
344 | return (short) value;
|
---|
345 | }
|
---|
346 |
|
---|
347 | /**
|
---|
348 | * Return the value of this <code>Integer</code> as an <code>int</code>.
|
---|
349 | *
|
---|
350 | * @return the int value
|
---|
351 | */
|
---|
352 | public int intValue()
|
---|
353 | {
|
---|
354 | return (int) value;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * Return the value of this <code>Integer</code> as a <code>long</code>.
|
---|
359 | *
|
---|
360 | * @return the long value
|
---|
361 | */
|
---|
362 | public long longValue()
|
---|
363 | {
|
---|
364 | return (long) value;
|
---|
365 | }
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Return the value of this <code>Float</code>.
|
---|
369 | *
|
---|
370 | * @return the float value
|
---|
371 | */
|
---|
372 | public float floatValue()
|
---|
373 | {
|
---|
374 | return value;
|
---|
375 | }
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * Return the value of this <code>Float</code> as a <code>double</code>
|
---|
379 | *
|
---|
380 | * @return the double value
|
---|
381 | */
|
---|
382 | public double doubleValue()
|
---|
383 | {
|
---|
384 | return value;
|
---|
385 | }
|
---|
386 |
|
---|
387 | /**
|
---|
388 | * Return a hashcode representing this Object. <code>Float</code>'s hash
|
---|
389 | * code is calculated by calling <code>floatToIntBits(floatValue())</code>.
|
---|
390 | *
|
---|
391 | * @return this Object's hash code
|
---|
392 | * @see #floatToIntBits(float)
|
---|
393 | */
|
---|
394 | public int hashCode()
|
---|
395 | {
|
---|
396 | return floatToIntBits(value);
|
---|
397 | }
|
---|
398 |
|
---|
399 | /**
|
---|
400 | * Returns <code>true</code> if <code>obj</code> is an instance of
|
---|
401 | * <code>Float</code> and represents the same float value. Unlike comparing
|
---|
402 | * two floats with <code>==</code>, this treats two instances of
|
---|
403 | * <code>Float.NaN</code> as equal, but treats <code>0.0</code> and
|
---|
404 | * <code>-0.0</code> as unequal.
|
---|
405 | *
|
---|
406 | * <p>Note that <code>f1.equals(f2)<code> is identical to
|
---|
407 | * <code>floatToIntBits(f1.floatValue()) ==
|
---|
408 | * floatToIntBits(f2.floatValue())<code>.
|
---|
409 | *
|
---|
410 | * @param obj the object to compare
|
---|
411 | * @return whether the objects are semantically equal
|
---|
412 | */
|
---|
413 | public boolean equals(Object obj)
|
---|
414 | {
|
---|
415 | if (! (obj instanceof Float))
|
---|
416 | return false;
|
---|
417 |
|
---|
418 | float f = ((Float) obj).value;
|
---|
419 |
|
---|
420 | // Avoid call to native method. However, some implementations, like gcj,
|
---|
421 | // are better off using floatToIntBits(value) == floatToIntBits(f).
|
---|
422 | // Check common case first, then check NaN and 0.
|
---|
423 | if (value == f)
|
---|
424 | return (value != 0) || (1 / value == 1 / f);
|
---|
425 | return isNaN(value) && isNaN(f);
|
---|
426 | }
|
---|
427 |
|
---|
428 | /**
|
---|
429 | * Convert the float to the IEEE 754 floating-point "single format" bit
|
---|
430 | * layout. Bit 31 (the most significant) is the sign bit, bits 30-23
|
---|
431 | * (masked by 0x7f800000) represent the exponent, and bits 22-0
|
---|
432 | * (masked by 0x007fffff) are the mantissa. This function collapses all
|
---|
433 | * versions of NaN to 0x7fc00000. The result of this function can be used
|
---|
434 | * as the argument to <code>Float.intBitsToFloat(int)</code> to obtain the
|
---|
435 | * original <code>float</code> value.
|
---|
436 | *
|
---|
437 | * @param value the <code>float</code> to convert
|
---|
438 | * @return the bits of the <code>float</code>
|
---|
439 | * @see #intBitsToFloat(int)
|
---|
440 | */
|
---|
441 | public static native int floatToIntBits(float value);
|
---|
442 |
|
---|
443 | /**
|
---|
444 | * Convert the float to the IEEE 754 floating-point "single format" bit
|
---|
445 | * layout. Bit 31 (the most significant) is the sign bit, bits 30-23
|
---|
446 | * (masked by 0x7f800000) represent the exponent, and bits 22-0
|
---|
447 | * (masked by 0x007fffff) are the mantissa. This function leaves NaN alone,
|
---|
448 | * rather than collapsing to a canonical value. The result of this function
|
---|
449 | * can be used as the argument to <code>Float.intBitsToFloat(int)</code> to
|
---|
450 | * obtain the original <code>float</code> value.
|
---|
451 | *
|
---|
452 | * @param value the <code>float</code> to convert
|
---|
453 | * @return the bits of the <code>float</code>
|
---|
454 | * @see #intBitsToFloat(int)
|
---|
455 | */
|
---|
456 | public static native int floatToRawIntBits(float value);
|
---|
457 |
|
---|
458 | /**
|
---|
459 | * Convert the argument in IEEE 754 floating-point "single format" bit
|
---|
460 | * layout to the corresponding float. Bit 31 (the most significant) is the
|
---|
461 | * sign bit, bits 30-23 (masked by 0x7f800000) represent the exponent, and
|
---|
462 | * bits 22-0 (masked by 0x007fffff) are the mantissa. This function leaves
|
---|
463 | * NaN alone, so that you can recover the bit pattern with
|
---|
464 | * <code>Float.floatToRawIntBits(float)</code>.
|
---|
465 | *
|
---|
466 | * @param bits the bits to convert
|
---|
467 | * @return the <code>float</code> represented by the bits
|
---|
468 | * @see #floatToIntBits(float)
|
---|
469 | * @see #floatToRawIntBits(float)
|
---|
470 | */
|
---|
471 | public static native float intBitsToFloat(int bits);
|
---|
472 |
|
---|
473 | /**
|
---|
474 | * Compare two Floats numerically by comparing their <code>float</code>
|
---|
475 | * values. The result is positive if the first is greater, negative if the
|
---|
476 | * second is greater, and 0 if the two are equal. However, this special
|
---|
477 | * cases NaN and signed zero as follows: NaN is considered greater than
|
---|
478 | * all other floats, including <code>POSITIVE_INFINITY</code>, and positive
|
---|
479 | * zero is considered greater than negative zero.
|
---|
480 | *
|
---|
481 | * @param f the Float to compare
|
---|
482 | * @return the comparison
|
---|
483 | * @since 1.2
|
---|
484 | */
|
---|
485 | public int compareTo(Float f)
|
---|
486 | {
|
---|
487 | return compare(value, f.value);
|
---|
488 | }
|
---|
489 |
|
---|
490 | /**
|
---|
491 | * Behaves like <code>compareTo(Float)</code> unless the Object
|
---|
492 | * is not an <code>Float</code>.
|
---|
493 | *
|
---|
494 | * @param o the object to compare
|
---|
495 | * @return the comparison
|
---|
496 | * @throws ClassCastException if the argument is not a <code>Float</code>
|
---|
497 | * @see #compareTo(Float)
|
---|
498 | * @see Comparable
|
---|
499 | * @since 1.2
|
---|
500 | */
|
---|
501 | public int compareTo(Object o)
|
---|
502 | {
|
---|
503 | return compare(value, ((Float) o).value);
|
---|
504 | }
|
---|
505 |
|
---|
506 | /**
|
---|
507 | * Behaves like <code>new Float(x).compareTo(new Float(y))</code>; in
|
---|
508 | * other words this compares two floats, special casing NaN and zero,
|
---|
509 | * without the overhead of objects.
|
---|
510 | *
|
---|
511 | * @param x the first float to compare
|
---|
512 | * @param y the second float to compare
|
---|
513 | * @return the comparison
|
---|
514 | * @since 1.4
|
---|
515 | */
|
---|
516 | public static int compare(float x, float y)
|
---|
517 | {
|
---|
518 | if (isNaN(x))
|
---|
519 | return isNaN(y) ? 0 : 1;
|
---|
520 | if (isNaN(y))
|
---|
521 | return -1;
|
---|
522 | // recall that 0.0 == -0.0, so we convert to infinities and try again
|
---|
523 | if (x == 0 && y == 0)
|
---|
524 | return (int) (1 / x - 1 / y);
|
---|
525 | if (x == y)
|
---|
526 | return 0;
|
---|
527 |
|
---|
528 | return x > y ? 1 : -1;
|
---|
529 | }
|
---|
530 | }
|
---|