1 /* Float.java -- object wrapper for float
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 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)
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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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. */
41 * Instances of class <code>Float</code> represent primitive
42 * <code>float</code> values.
44 * Additionally, this class provides various helper functions and variables
48 * @author Andrew Haley (aph@cygnus.com)
49 * @author Eric Blake (ebb9@email.byu.edu)
50 * @author Tom Tromey (tromey@redhat.com)
51 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
53 * @status partly updated to 1.5
55 public final class Float
58 * Compatible with JDK 1.0+.
60 private static final long serialVersionUID = -2671257302660747028L;
63 * The maximum positive value a <code>double</code> may represent
66 public static final float MAX_VALUE = 3.4028235e+38f;
69 * The minimum positive value a <code>float</code> may represent
72 public static final float MIN_VALUE = 1.4e-45f;
75 * The value of a float representation -1.0/0.0, negative infinity.
77 public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
80 * The value of a float representation 1.0/0.0, positive infinity.
82 public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
85 * All IEEE 754 values of NaN have the same value in Java.
87 public static final float NaN = 0.0f / 0.0f;
90 * The primitive type <code>float</code> is represented by this
91 * <code>Class</code> object.
94 //public static final Class<Float> TYPE = (Class<Float>) VMClassLoader.getPrimitiveClass('F');
97 * The number of bits needed to represent a <code>float</code>.
100 public static final int SIZE = 32;
103 * Cache representation of 0
105 private static final Float ZERO = new Float(0.0f);
108 * Cache representation of 1
110 private static final Float ONE = new Float(1.0f);
113 * The immutable value of this Float.
115 * @serial the wrapped float
117 private final float value;
120 * Create a <code>Float</code> from the primitive <code>float</code>
123 * @param value the <code>float</code> argument
125 public Float(float value)
131 * Create a <code>Float</code> from the primitive <code>double</code>
134 * @param value the <code>double</code> argument
136 public Float(double value)
138 this.value = (float) value;
142 * Create a <code>Float</code> from the specified <code>String</code>.
143 * This method calls <code>Float.parseFloat()</code>.
145 * @param s the <code>String</code> to convert
146 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
148 * @throws NullPointerException if <code>s</code> is null
149 * @see #parseFloat(String)
151 public Float(String s)
153 value = parseFloat(s);
157 * Convert the <code>float</code> to a <code>String</code>.
158 * Floating-point string representation is fairly complex: here is a
159 * rundown of the possible values. "<code>[-]</code>" indicates that a
160 * negative sign will be printed if the value (or exponent) is negative.
161 * "<code><number></code>" means a string of digits ('0' to '9').
162 * "<code><digit></code>" means a single digit ('0' to '9').<br>
165 * <tr><th>Value of Float</th><th>String Representation</th></tr>
166 * <tr><td>[+-] 0</td> <td><code>[-]0.0</code></td></tr>
167 * <tr><td>Between [+-] 10<sup>-3</sup> and 10<sup>7</sup>, exclusive</td>
168 * <td><code>[-]number.number</code></td></tr>
169 * <tr><td>Other numeric value</td>
170 * <td><code>[-]<digit>.<number>
171 * E[-]<number></code></td></tr>
172 * <tr><td>[+-] infinity</td> <td><code>[-]Infinity</code></td></tr>
173 * <tr><td>NaN</td> <td><code>NaN</code></td></tr>
176 * Yes, negative zero <em>is</em> a possible value. Note that there is
177 * <em>always</em> a <code>.</code> and at least one digit printed after
178 * it: even if the number is 3, it will be printed as <code>3.0</code>.
179 * After the ".", all digits will be printed except trailing zeros. The
180 * result is rounded to the shortest decimal number which will parse back
183 * <p>To create other output formats, use {@link java.text.NumberFormat}.
185 * @XXX specify where we are not in accord with the spec.
187 * @param f the <code>float</code> to convert
188 * @return the <code>String</code> representing the <code>float</code>
190 /*public static String toString(float f)
192 return VMFloat.toString(f);
196 * Convert a float value to a hexadecimal string. This converts as
199 * <li> A NaN value is converted to the string "NaN".
200 * <li> Positive infinity is converted to the string "Infinity".
201 * <li> Negative infinity is converted to the string "-Infinity".
202 * <li> For all other values, the first character of the result is '-'
203 * if the value is negative. This is followed by '0x1.' if the
204 * value is normal, and '0x0.' if the value is denormal. This is
205 * then followed by a (lower-case) hexadecimal representation of the
206 * mantissa, with leading zeros as required for denormal values.
207 * The next character is a 'p', and this is followed by a decimal
208 * representation of the unbiased exponent.
210 * @param f the float value
211 * @return the hexadecimal string representation
214 /*public static String toHexString(float f)
219 return f < 0 ? "-Infinity" : "Infinity";
221 int bits = floatToIntBits(f);
222 CPStringBuilder result = new CPStringBuilder();
228 final int mantissaBits = 23;
229 final int exponentBits = 8;
230 int mantMask = (1 << mantissaBits) - 1;
231 int mantissa = bits & mantMask;
232 int expMask = (1 << exponentBits) - 1;
233 int exponent = (bits >>> mantissaBits) & expMask;
235 result.append(exponent == 0 ? '0' : '1');
237 // For Float only, we have to adjust the mantissa.
239 result.append(Integer.toHexString(mantissa));
240 if (exponent == 0 && mantissa != 0)
242 // Treat denormal specially by inserting '0's to make
243 // the length come out right. The constants here are
244 // to account for things like the '0x'.
245 int offset = 4 + ((bits < 0) ? 1 : 0);
246 // The silly +3 is here to keep the code the same between
247 // the Float and Double cases. In Float the value is
248 // not a multiple of 4.
249 int desiredLength = offset + (mantissaBits + 3) / 4;
250 while (result.length() < desiredLength)
251 result.insert(offset, '0');
254 if (exponent == 0 && mantissa == 0)
256 // Zero, so do nothing special.
261 boolean denormal = exponent == 0;
262 exponent -= (1 << (exponentBits - 1)) - 1;
268 result.append(Integer.toString(exponent));
269 return result.toString();
273 * Creates a new <code>Float</code> object using the <code>String</code>.
275 * @param s the <code>String</code> to convert
276 * @return the new <code>Float</code>
277 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
279 * @throws NullPointerException if <code>s</code> is null
280 * @see #parseFloat(String)
282 public static Float valueOf(String s)
284 return valueOf(parseFloat(s));
288 * Returns a <code>Float</code> object wrapping the value.
289 * In contrast to the <code>Float</code> constructor, this method
290 * may cache some values. It is used by boxing conversion.
292 * @param val the value to wrap
293 * @return the <code>Float</code>
296 public static Float valueOf(float val)
298 if ((val == 0.0)/* && (floatToRawIntBits(val) == 0)*/)
303 return new Float(val);
307 * Parse the specified <code>String</code> as a <code>float</code>. The
308 * extended BNF grammar is as follows:<br>
310 * <em>DecodableString</em>:
311 * ( [ <code>-</code> | <code>+</code> ] <code>NaN</code> )
312 * | ( [ <code>-</code> | <code>+</code> ] <code>Infinity</code> )
313 * | ( [ <code>-</code> | <code>+</code> ] <em>FloatingPoint</em>
314 * [ <code>f</code> | <code>F</code> | <code>d</code>
315 * | <code>D</code>] )
316 * <em>FloatingPoint</em>:
317 * ( { <em>Digit</em> }+ [ <code>.</code> { <em>Digit</em> } ]
318 * [ <em>Exponent</em> ] )
319 * | ( <code>.</code> { <em>Digit</em> }+ [ <em>Exponent</em> ] )
321 * ( ( <code>e</code> | <code>E</code> )
322 * [ <code>-</code> | <code>+</code> ] { <em>Digit</em> }+ )
323 * <em>Digit</em>: <em><code>'0'</code> through <code>'9'</code></em>
326 * <p>NaN and infinity are special cases, to allow parsing of the output
327 * of toString. Otherwise, the result is determined by calculating
328 * <em>n * 10<sup>exponent</sup></em> to infinite precision, then rounding
329 * to the nearest float. Remember that many numbers cannot be precisely
330 * represented in floating point. In case of overflow, infinity is used,
331 * and in case of underflow, signed zero is used. Unlike Integer.parseInt,
332 * this does not accept Unicode digits outside the ASCII range.
334 * <p>If an unexpected character is found in the <code>String</code>, a
335 * <code>NumberFormatException</code> will be thrown. Leading and trailing
336 * 'whitespace' is ignored via <code>String.trim()</code>, but spaces
337 * internal to the actual number are not allowed.
339 * <p>To parse numbers according to another format, consider using
340 * {@link java.text.NumberFormat}.
342 * @XXX specify where/how we are not in accord with the spec.
344 * @param str the <code>String</code> to convert
345 * @return the <code>float</code> value of <code>s</code>
346 * @throws NumberFormatException if <code>str</code> cannot be parsed as a
348 * @throws NullPointerException if <code>str</code> is null
351 * @see #POSITIVE_INFINITY
352 * @see #NEGATIVE_INFINITY
355 public static float parseFloat(String str)
357 //return VMFloat.parseFloat(str);
358 return (float)(Long.parseLong(str));
362 * Return <code>true</code> if the <code>float</code> has the same
363 * value as <code>NaN</code>, otherwise return <code>false</code>.
365 * @param v the <code>float</code> to compare
366 * @return whether the argument is <code>NaN</code>
368 public static boolean isNaN(float v)
370 // This works since NaN != NaN is the only reflexive inequality
371 // comparison which returns true.
376 * Return <code>true</code> if the <code>float</code> has a value
377 * equal to either <code>NEGATIVE_INFINITY</code> or
378 * <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
380 * @param v the <code>float</code> to compare
381 * @return whether the argument is (-/+) infinity
383 public static boolean isInfinite(float v)
385 return v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY;
389 * Return <code>true</code> if the value of this <code>Float</code>
390 * is the same as <code>NaN</code>, otherwise return <code>false</code>.
392 * @return whether this <code>Float</code> is <code>NaN</code>
394 public boolean isNaN()
400 * Return <code>true</code> if the value of this <code>Float</code>
401 * is the same as <code>NEGATIVE_INFINITY</code> or
402 * <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
404 * @return whether this <code>Float</code> is (-/+) infinity
406 public boolean isInfinite()
408 return isInfinite(value);
412 * Convert the <code>float</code> value of this <code>Float</code>
413 * to a <code>String</code>. This method calls
414 * <code>Float.toString(float)</code> to do its dirty work.
416 * @return the <code>String</code> representation
417 * @see #toString(float)
419 /*public String toString()
421 return toString(value);
425 * Return the value of this <code>Float</code> as a <code>byte</code>.
427 * @return the byte value
430 public byte byteValue()
436 * Return the value of this <code>Float</code> as a <code>short</code>.
438 * @return the short value
441 public short shortValue()
443 return (short) value;
447 * Return the value of this <code>Integer</code> as an <code>int</code>.
449 * @return the int value
451 public int intValue()
457 * Return the value of this <code>Integer</code> as a <code>long</code>.
459 * @return the long value
461 public long longValue()
467 * Return the value of this <code>Float</code>.
469 * @return the float value
471 public float floatValue()
477 * Return the value of this <code>Float</code> as a <code>double</code>
479 * @return the double value
481 public double doubleValue()
487 * Return a hashcode representing this Object. <code>Float</code>'s hash
488 * code is calculated by calling <code>floatToIntBits(floatValue())</code>.
490 * @return this Object's hash code
491 * @see #floatToIntBits(float)
493 /*public int hashCode()
495 return floatToIntBits(value);
499 * Returns <code>true</code> if <code>obj</code> is an instance of
500 * <code>Float</code> and represents the same float value. Unlike comparing
501 * two floats with <code>==</code>, this treats two instances of
502 * <code>Float.NaN</code> as equal, but treats <code>0.0</code> and
503 * <code>-0.0</code> as unequal.
505 * <p>Note that <code>f1.equals(f2)</code> is identical to
506 * <code>floatToIntBits(f1.floatValue()) ==
507 * floatToIntBits(f2.floatValue())</code>.
509 * @param obj the object to compare
510 * @return whether the objects are semantically equal
512 /*public boolean equals(Object obj)
514 if (obj instanceof Float)
516 float f = ((Float) obj).value;
517 return (floatToRawIntBits(value) == floatToRawIntBits(f)) ||
518 (isNaN(value) && isNaN(f));
524 * Convert the float to the IEEE 754 floating-point "single format" bit
525 * layout. Bit 31 (the most significant) is the sign bit, bits 30-23
526 * (masked by 0x7f800000) represent the exponent, and bits 22-0
527 * (masked by 0x007fffff) are the mantissa. This function collapses all
528 * versions of NaN to 0x7fc00000. The result of this function can be used
529 * as the argument to <code>Float.intBitsToFloat(int)</code> to obtain the
530 * original <code>float</code> value.
532 * @param value the <code>float</code> to convert
533 * @return the bits of the <code>float</code>
534 * @see #intBitsToFloat(int)
536 /*public static int floatToIntBits(float value)
541 return VMFloat.floatToRawIntBits(value);
545 * Convert the float to the IEEE 754 floating-point "single format" bit
546 * layout. Bit 31 (the most significant) is the sign bit, bits 30-23
547 * (masked by 0x7f800000) represent the exponent, and bits 22-0
548 * (masked by 0x007fffff) are the mantissa. This function leaves NaN alone,
549 * rather than collapsing to a canonical value. The result of this function
550 * can be used as the argument to <code>Float.intBitsToFloat(int)</code> to
551 * obtain the original <code>float</code> value.
553 * @param value the <code>float</code> to convert
554 * @return the bits of the <code>float</code>
555 * @see #intBitsToFloat(int)
557 /*public static int floatToRawIntBits(float value)
559 return VMFloat.floatToRawIntBits(value);
563 * Convert the argument in IEEE 754 floating-point "single format" bit
564 * layout to the corresponding float. Bit 31 (the most significant) is the
565 * sign bit, bits 30-23 (masked by 0x7f800000) represent the exponent, and
566 * bits 22-0 (masked by 0x007fffff) are the mantissa. This function leaves
567 * NaN alone, so that you can recover the bit pattern with
568 * <code>Float.floatToRawIntBits(float)</code>.
570 * @param bits the bits to convert
571 * @return the <code>float</code> represented by the bits
572 * @see #floatToIntBits(float)
573 * @see #floatToRawIntBits(float)
575 /*public static float intBitsToFloat(int bits)
577 return VMFloat.intBitsToFloat(bits);
581 * Compare two Floats numerically by comparing their <code>float</code>
582 * values. The result is positive if the first is greater, negative if the
583 * second is greater, and 0 if the two are equal. However, this special
584 * cases NaN and signed zero as follows: NaN is considered greater than
585 * all other floats, including <code>POSITIVE_INFINITY</code>, and positive
586 * zero is considered greater than negative zero.
588 * @param f the Float to compare
589 * @return the comparison
592 /*public int compareTo(Float f)
594 return compare(value, f.value);
598 * Behaves like <code>new Float(x).compareTo(new Float(y))</code>; in
599 * other words this compares two floats, special casing NaN and zero,
600 * without the overhead of objects.
602 * @param x the first float to compare
603 * @param y the second float to compare
604 * @return the comparison
607 /*public static int compare(float x, float y)
609 // handle the easy cases:
615 // handle equality respecting that 0.0 != -0.0 (hence not using x == y):
616 int ix = floatToRawIntBits(x);
617 int iy = floatToRawIntBits(y);
623 return (y != y) ? 0 : 1;
628 return (ix < iy) ? -1 : 1;