1 /* Float.java -- object wrapper for float
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
42 * Instances of class <code>Float</code> represent primitive
43 * <code>float</code> values.
45 * Additionally, this class provides various helper functions and variables
49 * @author Andrew Haley <aph@cygnus.com>
50 * @author Eric Blake <ebb9@email.byu.edu>
52 * @status updated to 1.4
54 public final class Float
extends Number
implements Comparable
57 * Compatible with JDK 1.0+.
59 private static final long serialVersionUID
= -2671257302660747028L;
62 * The maximum positive value a <code>double</code> may represent
65 public static final float MAX_VALUE
= 3.4028235e+38f
;
68 * The minimum positive value a <code>float</code> may represent
71 public static final float MIN_VALUE
= 1.4e-45f
;
74 * The value of a float representation -1.0/0.0, negative infinity.
76 public static final float NEGATIVE_INFINITY
= -1.0f
/ 0.0f
;
79 * The value of a float representation 1.0/0.0, positive infinity.
81 public static final float POSITIVE_INFINITY
= 1.0f
/ 0.0f
;
84 * All IEEE 754 values of NaN have the same value in Java.
86 public static final float NaN
= 0.0f
/ 0.0f
;
89 * The primitive type <code>float</code> is represented by this
90 * <code>Class</code> object.
93 public static final Class TYPE
= VMClassLoader
.getPrimitiveClass('F');
96 * The immutable value of this Float.
98 * @serial the wrapped float
100 private final float value
;
103 * Create a <code>Float</code> from the primitive <code>float</code>
106 * @param value the <code>float</code> argument
108 public Float(float value
)
114 * Create a <code>Float</code> from the primitive <code>double</code>
117 * @param value the <code>double</code> argument
119 public Float(double value
)
121 this.value
= (float) value
;
125 * Create a <code>Float</code> from the specified <code>String</code>.
126 * This method calls <code>Float.parseFloat()</code>.
128 * @param s the <code>String</code> to convert
129 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
131 * @throws NullPointerException if <code>s</code> is null
132 * @see #parseFloat(String)
134 public Float(String s
)
136 value
= parseFloat(s
);
140 * Convert the <code>float</code> to a <code>String</code>.
141 * Floating-point string representation is fairly complex: here is a
142 * rundown of the possible values. "<code>[-]</code>" indicates that a
143 * negative sign will be printed if the value (or exponent) is negative.
144 * "<code><number></code>" means a string of digits ('0' to '9').
145 * "<code><digit></code>" means a single digit ('0' to '9').<br>
148 * <tr><th>Value of Float</th><th>String Representation</th></tr>
149 * <tr><td>[+-] 0</td> <td><code>[-]0.0</code></td></tr>
150 * <tr><td>Between [+-] 10<sup>-3</sup> and 10<sup>7</sup>, exclusive</td>
151 * <td><code>[-]number.number</code></td></tr>
152 * <tr><td>Other numeric value</td>
153 * <td><code>[-]<digit>.<number>
154 * E[-]<number></code></td></tr>
155 * <tr><td>[+-] infinity</td> <td><code>[-]Infinity</code></td></tr>
156 * <tr><td>NaN</td> <td><code>NaN</code></td></tr>
159 * Yes, negative zero <em>is</em> a possible value. Note that there is
160 * <em>always</em> a <code>.</code> and at least one digit printed after
161 * it: even if the number is 3, it will be printed as <code>3.0</code>.
162 * After the ".", all digits will be printed except trailing zeros. The
163 * result is rounded to the shortest decimal number which will parse back
166 * <p>To create other output formats, use {@link java.text.NumberFormat}.
168 * @XXX specify where we are not in accord with the spec.
170 * @param f the <code>float</code> to convert
171 * @return the <code>String</code> representing the <code>float</code>
173 public static String
toString(float f
)
175 return Double
.toString(f
, true);
179 * Creates a new <code>Float</code> object using the <code>String</code>.
181 * @param s the <code>String</code> to convert
182 * @return the new <code>Float</code>
183 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
185 * @throws NullPointerException if <code>s</code> is null
186 * @see #parseFloat(String)
188 public static Float
valueOf(String s
)
190 return new Float(parseFloat(s
));
194 * Parse the specified <code>String</code> as a <code>float</code>. The
195 * extended BNF grammar is as follows:<br>
197 * <em>DecodableString</em>:
198 * ( [ <code>-</code> | <code>+</code> ] <code>NaN</code> )
199 * | ( [ <code>-</code> | <code>+</code> ] <code>Infinity</code> )
200 * | ( [ <code>-</code> | <code>+</code> ] <em>FloatingPoint</em>
201 * [ <code>f</code> | <code>F</code> | <code>d</code>
202 * | <code>D</code>] )
203 * <em>FloatingPoint</em>:
204 * ( { <em>Digit</em> }+ [ <code>.</code> { <em>Digit</em> } ]
205 * [ <em>Exponent</em> ] )
206 * | ( <code>.</code> { <em>Digit</em> }+ [ <em>Exponent</em> ] )
208 * ( ( <code>e</code> | <code>E</code> )
209 * [ <code>-</code> | <code>+</code> ] { <em>Digit</em> }+ )
210 * <em>Digit</em>: <em><code>'0'</code> through <code>'9'</code></em>
213 * <p>NaN and infinity are special cases, to allow parsing of the output
214 * of toString. Otherwise, the result is determined by calculating
215 * <em>n * 10<sup>exponent</sup></em> to infinite precision, then rounding
216 * to the nearest float. Remember that many numbers cannot be precisely
217 * represented in floating point. In case of overflow, infinity is used,
218 * and in case of underflow, signed zero is used. Unlike Integer.parseInt,
219 * this does not accept Unicode digits outside the ASCII range.
221 * <p>If an unexpected character is found in the <code>String</code>, a
222 * <code>NumberFormatException</code> will be thrown. Leading and trailing
223 * 'whitespace' is ignored via <code>String.trim()</code>, but spaces
224 * internal to the actual number are not allowed.
226 * <p>To parse numbers according to another format, consider using
227 * {@link java.text.NumberFormat}.
229 * @XXX specify where/how we are not in accord with the spec.
231 * @param str the <code>String</code> to convert
232 * @return the <code>float</code> value of <code>s</code>
233 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
235 * @throws NullPointerException if <code>s</code> is null
238 * @see #POSITIVE_INFINITY
239 * @see #NEGATIVE_INFINITY
242 public static float parseFloat(String s
)
244 // XXX Rounding parseDouble() causes some errors greater than 1 ulp from
245 // the infinitely precise decimal.
246 return (float) Double
.parseDouble(s
);
250 * Return <code>true</code> if the <code>float</code> has the same
251 * value as <code>NaN</code>, otherwise return <code>false</code>.
253 * @param v the <code>float</code> to compare
254 * @return whether the argument is <code>NaN</code>
256 public static boolean isNaN(float v
)
258 // This works since NaN != NaN is the only reflexive inequality
259 // comparison which returns true.
264 * Return <code>true</code> if the <code>float</code> has a value
265 * equal to either <code>NEGATIVE_INFINITY</code> or
266 * <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
268 * @param v the <code>float</code> to compare
269 * @return whether the argument is (-/+) infinity
271 public static boolean isInfinite(float v
)
273 return v
== POSITIVE_INFINITY
|| v
== NEGATIVE_INFINITY
;
277 * Return <code>true</code> if the value of this <code>Float</code>
278 * is the same as <code>NaN</code>, otherwise return <code>false</code>.
280 * @return whether this <code>Float</code> is <code>NaN</code>
282 public boolean isNaN()
288 * Return <code>true</code> if the value of this <code>Float</code>
289 * is the same as <code>NEGATIVE_INFINITY</code> or
290 * <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
292 * @return whether this <code>Float</code> is (-/+) infinity
294 public boolean isInfinite()
296 return isInfinite(value
);
300 * Convert the <code>float</code> value of this <code>Float</code>
301 * to a <code>String</code>. This method calls
302 * <code>Float.toString(float)</code> to do its dirty work.
304 * @return the <code>String</code> representation
305 * @see #toString(float)
307 public String
toString()
309 return toString(value
);
313 * Return the value of this <code>Float</code> as a <code>byte</code>.
315 * @return the byte value
318 public byte byteValue()
324 * Return the value of this <code>Float</code> as a <code>short</code>.
326 * @return the short value
329 public short shortValue()
331 return (short) value
;
335 * Return the value of this <code>Integer</code> as an <code>int</code>.
337 * @return the int value
339 public int intValue()
345 * Return the value of this <code>Integer</code> as a <code>long</code>.
347 * @return the long value
349 public long longValue()
355 * Return the value of this <code>Float</code>.
357 * @return the float value
359 public float floatValue()
365 * Return the value of this <code>Float</code> as a <code>double</code>
367 * @return the double value
369 public double doubleValue()
375 * Return a hashcode representing this Object. <code>Float</code>'s hash
376 * code is calculated by calling <code>floatToIntBits(floatValue())</code>.
378 * @return this Object's hash code
379 * @see #floatToIntBits(float)
381 public int hashCode()
383 return floatToIntBits(value
);
387 * Returns <code>true</code> if <code>obj</code> is an instance of
388 * <code>Float</code> and represents the same float value. Unlike comparing
389 * two floats with <code>==</code>, this treats two instances of
390 * <code>Float.NaN</code> as equal, but treats <code>0.0</code> and
391 * <code>-0.0</code> as unequal.
393 * <p>Note that <code>f1.equals(f2)<code> is identical to
394 * <code>floatToIntBits(f1.floatValue()) ==
395 * floatToIntBits(f2.floatValue())<code>.
397 * @param obj the object to compare
398 * @return whether the objects are semantically equal
400 public boolean equals(Object obj
)
402 if (! (obj
instanceof Float
))
405 float f
= ((Float
) obj
).value
;
407 // Avoid call to native method. However, some implementations, like gcj,
408 // are better off using floatToIntBits(value) == floatToIntBits(f).
409 // Check common case first, then check NaN and 0.
411 return (value
!= 0) || (1 / value
== 1 / f
);
412 return isNaN(value
) && isNaN(f
);
416 * Convert the float to the IEEE 754 floating-point "single format" bit
417 * layout. Bit 31 (the most significant) is the sign bit, bits 30-23
418 * (masked by 0x7f800000) represent the exponent, and bits 22-0
419 * (masked by 0x007fffff) are the mantissa. This function collapses all
420 * versions of NaN to 0x7fc00000. The result of this function can be used
421 * as the argument to <code>Float.intBitsToFloat(int)</code> to obtain the
422 * original <code>float</code> value.
424 * @param value the <code>float</code> to convert
425 * @return the bits of the <code>float</code>
426 * @see #intBitsToFloat(int)
428 // GCJ LOCAL: We diverge from Classpath for efficiency.
429 public static native int floatToIntBits(float value
);
433 * Convert the float to the IEEE 754 floating-point "single format" bit
434 * layout. Bit 31 (the most significant) is the sign bit, bits 30-23
435 * (masked by 0x7f800000) represent the exponent, and bits 22-0
436 * (masked by 0x007fffff) are the mantissa. This function leaves NaN alone,
437 * rather than collapsing to a canonical value. The result of this function
438 * can be used as the argument to <code>Float.intBitsToFloat(int)</code> to
439 * obtain the original <code>float</code> value.
441 * @param value the <code>float</code> to convert
442 * @return the bits of the <code>float</code>
443 * @see #intBitsToFloat(int)
445 // GCJ LOCAL: We diverge from Classpath for efficiency.
446 public static native int floatToRawIntBits(float value
);
450 * Convert the argument in IEEE 754 floating-point "single format" bit
451 * layout to the corresponding float. Bit 31 (the most significant) is the
452 * sign bit, bits 30-23 (masked by 0x7f800000) represent the exponent, and
453 * bits 22-0 (masked by 0x007fffff) are the mantissa. This function leaves
454 * NaN alone, so that you can recover the bit pattern with
455 * <code>Float.floatToRawIntBits(float)</code>.
457 * @param bits the bits to convert
458 * @return the <code>float</code> represented by the bits
459 * @see #floatToIntBits(float)
460 * @see #floatToRawIntBits(float)
462 // GCJ LOCAL: We diverge from Classpath for efficiency.
463 public static native float intBitsToFloat(int bits
);
467 * Compare two Floats numerically by comparing their <code>float</code>
468 * values. The result is positive if the first is greater, negative if the
469 * second is greater, and 0 if the two are equal. However, this special
470 * cases NaN and signed zero as follows: NaN is considered greater than
471 * all other floats, including <code>POSITIVE_INFINITY</code>, and positive
472 * zero is considered greater than negative zero.
474 * @param f the Float to compare
475 * @return the comparison
478 public int compareTo(Float f
)
480 return compare(value
, f
.value
);
484 * Behaves like <code>compareTo(Float)</code> unless the Object
485 * is not an <code>Float</code>.
487 * @param o the object to compare
488 * @return the comparison
489 * @throws ClassCastException if the argument is not a <code>Float</code>
490 * @see #compareTo(Float)
494 public int compareTo(Object o
)
496 return compare(value
, ((Float
) o
).value
);
500 * Behaves like <code>new Float(x).compareTo(new Float(y))</code>; in
501 * other words this compares two floats, special casing NaN and zero,
502 * without the overhead of objects.
504 * @param x the first float to compare
505 * @param y the second float to compare
506 * @return the comparison
509 public static int compare(float x
, float y
)
512 return isNaN(y
) ?
0 : 1;
515 // recall that 0.0 == -0.0, so we convert to infinities and try again
516 if (x
== 0 && y
== 0)
517 return (int) (1 / x
- 1 / y
);
521 return x
> y ?
1 : -1;