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