try to make mp3decoder compile
[IRC.git] / Robust / src / ClassLibrary / SSJava / Float.java
1 /* Float.java -- object wrapper for float
2    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3    Free Software Foundation, Inc.
4
5 This file is part of GNU Classpath.
6
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.
11
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.
16
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
20 02110-1301 USA.
21
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.
26
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. */
38
39
40 /**
41  * Instances of class <code>Float</code> represent primitive
42  * <code>float</code> values.
43  *
44  * Additionally, this class provides various helper functions and variables
45  * related to floats.
46  *
47  * @author Paul Fisher
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)
52  * @since 1.0
53  * @status partly updated to 1.5
54  */
55 public final class Float
56 {
57   /**
58    * Compatible with JDK 1.0+.
59    */
60   private static final long serialVersionUID = -2671257302660747028L;
61
62   /**
63    * The maximum positive value a <code>double</code> may represent
64    * is 3.4028235e+38f.
65    */
66   public static final float MAX_VALUE = 3.4028235e+38f;
67
68   /**
69    * The minimum positive value a <code>float</code> may represent
70    * is 1.4e-45.
71    */
72   public static final float MIN_VALUE = 1.4e-45f;
73
74   /**
75    * The value of a float representation -1.0/0.0, negative infinity.
76    */
77   public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
78
79   /**
80    * The value of a float representation 1.0/0.0, positive infinity.
81    */
82   public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
83
84   /**
85    * All IEEE 754 values of NaN have the same value in Java.
86    */
87   public static final float NaN = 0.0f / 0.0f;
88
89   /**
90    * The primitive type <code>float</code> is represented by this
91    * <code>Class</code> object.
92    * @since 1.1
93    */
94   //public static final Class<Float> TYPE = (Class<Float>) VMClassLoader.getPrimitiveClass('F');
95
96   /**
97    * The number of bits needed to represent a <code>float</code>.
98    * @since 1.5
99    */
100   public static final int SIZE = 32;
101
102   /**
103    * Cache representation of 0
104    */
105   private static final Float ZERO = new Float(0.0f);
106
107   /**
108    * Cache representation of 1
109    */
110   private static final Float ONE = new Float(1.0f);
111
112   /**
113    * The immutable value of this Float.
114    *
115    * @serial the wrapped float
116    */
117   private final float value;
118
119   /**
120    * Create a <code>Float</code> from the primitive <code>float</code>
121    * specified.
122    *
123    * @param value the <code>float</code> argument
124    */
125   public Float(float value)
126   {
127     this.value = value;
128   }
129
130   /**
131    * Create a <code>Float</code> from the primitive <code>double</code>
132    * specified.
133    *
134    * @param value the <code>double</code> argument
135    */
136   public Float(double value)
137   {
138     this.value = (float) value;
139   }
140
141   /**
142    * Create a <code>Float</code> from the specified <code>String</code>.
143    * This method calls <code>Float.parseFloat()</code>.
144    *
145    * @param s the <code>String</code> to convert
146    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
147    *         <code>float</code>
148    * @throws NullPointerException if <code>s</code> is null
149    * @see #parseFloat(String)
150    */
151   public Float(String s)
152   {
153     value = parseFloat(s);
154   }
155
156   /**
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>&lt;number&gt;</code>" means a string of digits ('0' to '9').
162    * "<code>&lt;digit&gt;</code>" means a single digit ('0' to '9').<br>
163    *
164    * <table border=1>
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>[-]&lt;digit&gt;.&lt;number&gt;
171    *          E[-]&lt;number&gt;</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>
174    * </table>
175    *
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
181    * to the same float.
182    *
183    * <p>To create other output formats, use {@link java.text.NumberFormat}.
184    *
185    * @XXX specify where we are not in accord with the spec.
186    *
187    * @param f the <code>float</code> to convert
188    * @return the <code>String</code> representing the <code>float</code>
189    */
190   /*public static String toString(float f)
191   {
192     return VMFloat.toString(f);
193   }*/
194
195   /**
196    * Convert a float value to a hexadecimal string.  This converts as
197    * follows:
198    * <ul>
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.
209    * </ul>
210    * @param f the float value
211    * @return the hexadecimal string representation
212    * @since 1.5
213    */
214   /*public static String toHexString(float f)
215   {
216     if (isNaN(f))
217       return "NaN";
218     if (isInfinite(f))
219       return f < 0 ? "-Infinity" : "Infinity";
220
221     int bits = floatToIntBits(f);
222     CPStringBuilder result = new CPStringBuilder();
223     
224     if (bits < 0)
225       result.append('-');
226     result.append("0x");
227
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;
234
235     result.append(exponent == 0 ? '0' : '1');
236     result.append('.');
237     // For Float only, we have to adjust the mantissa.
238     mantissa <<= 1;
239     result.append(Integer.toHexString(mantissa));
240     if (exponent == 0 && mantissa != 0)
241       {
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');
252       }
253     result.append('p');
254     if (exponent == 0 && mantissa == 0)
255       {
256         // Zero, so do nothing special.
257       }
258     else
259       {
260         // Apply bias.
261         boolean denormal = exponent == 0;
262         exponent -= (1 << (exponentBits - 1)) - 1;
263         // Handle denormal.
264         if (denormal)
265           ++exponent;
266       }
267
268     result.append(Integer.toString(exponent));
269     return result.toString();
270   }*/
271
272   /**
273    * Creates a new <code>Float</code> object using the <code>String</code>.
274    *
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
278    *         <code>float</code>
279    * @throws NullPointerException if <code>s</code> is null
280    * @see #parseFloat(String)
281    */
282   public static Float valueOf(String s)
283   {
284     return valueOf(parseFloat(s));
285   }
286
287   /**
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.
291    *
292    * @param val the value to wrap
293    * @return the <code>Float</code>
294    * @since 1.5
295    */
296   public static Float valueOf(float val)
297   {
298     if ((val == 0.0)/* && (floatToRawIntBits(val) == 0)*/)
299       return ZERO;
300     else if (val == 1.0)
301       return ONE;
302     else
303       return new Float(val);
304   }
305
306   /**
307    * Parse the specified <code>String</code> as a <code>float</code>. The
308    * extended BNF grammar is as follows:<br>
309    * <pre>
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> ] )
320    * <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>
324    * </pre>
325    *
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.
333    *
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.
338    *
339    * <p>To parse numbers according to another format, consider using
340    * {@link java.text.NumberFormat}.
341    *
342    * @XXX specify where/how we are not in accord with the spec.
343    *
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
347    *         <code>float</code>
348    * @throws NullPointerException if <code>str</code> is null
349    * @see #MIN_VALUE
350    * @see #MAX_VALUE
351    * @see #POSITIVE_INFINITY
352    * @see #NEGATIVE_INFINITY
353    * @since 1.2
354    */
355   public static float parseFloat(String str)
356   {
357     //return VMFloat.parseFloat(str);
358     return (float)(Long.parseLong(str));
359   }
360
361   /**
362    * Return <code>true</code> if the <code>float</code> has the same
363    * value as <code>NaN</code>, otherwise return <code>false</code>.
364    *
365    * @param v the <code>float</code> to compare
366    * @return whether the argument is <code>NaN</code>
367    */
368   public static boolean isNaN(float v)
369   {
370     // This works since NaN != NaN is the only reflexive inequality
371     // comparison which returns true.
372     return v != v;
373   }
374
375   /**
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>.
379    *
380    * @param v the <code>float</code> to compare
381    * @return whether the argument is (-/+) infinity
382    */
383   public static boolean isInfinite(float v)
384   {
385     return v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY;
386   }
387
388   /**
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>.
391    *
392    * @return whether this <code>Float</code> is <code>NaN</code>
393    */
394   public boolean isNaN()
395   {
396     return isNaN(value);
397   }
398
399   /**
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>.
403    *
404    * @return whether this <code>Float</code> is (-/+) infinity
405    */
406   public boolean isInfinite()
407   {
408     return isInfinite(value);
409   }
410
411   /**
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.
415    *
416    * @return the <code>String</code> representation
417    * @see #toString(float)
418    */
419   /*public String toString()
420   {
421     return toString(value);
422   }*/
423
424   /**
425    * Return the value of this <code>Float</code> as a <code>byte</code>.
426    *
427    * @return the byte value
428    * @since 1.1
429    */
430   public byte byteValue()
431   {
432     return (byte) value;
433   }
434
435   /**
436    * Return the value of this <code>Float</code> as a <code>short</code>.
437    *
438    * @return the short value
439    * @since 1.1
440    */
441   public short shortValue()
442   {
443     return (short) value;
444   }
445
446   /**
447    * Return the value of this <code>Integer</code> as an <code>int</code>.
448    *
449    * @return the int value
450    */
451   public int intValue()
452   {
453     return (int) value;
454   }
455
456   /**
457    * Return the value of this <code>Integer</code> as a <code>long</code>.
458    *
459    * @return the long value
460    */
461   public long longValue()
462   {
463     return (long) value;
464   }
465
466   /**
467    * Return the value of this <code>Float</code>.
468    *
469    * @return the float value
470    */
471   public float floatValue()
472   {
473     return value;
474   }
475
476   /**
477    * Return the value of this <code>Float</code> as a <code>double</code>
478    *
479    * @return the double value
480    */
481   public double doubleValue()
482   {
483     return value;
484   }
485
486   /**
487    * Return a hashcode representing this Object. <code>Float</code>'s hash
488    * code is calculated by calling <code>floatToIntBits(floatValue())</code>.
489    *
490    * @return this Object's hash code
491    * @see #floatToIntBits(float)
492    */
493   /*public int hashCode()
494   {
495     return floatToIntBits(value);
496   }*/
497
498   /**
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.
504    *
505    * <p>Note that <code>f1.equals(f2)</code> is identical to
506    * <code>floatToIntBits(f1.floatValue()) ==
507    *    floatToIntBits(f2.floatValue())</code>.
508    *
509    * @param obj the object to compare
510    * @return whether the objects are semantically equal
511    */
512   /*public boolean equals(Object obj)
513   {
514     if (obj instanceof Float)
515       {
516         float f = ((Float) obj).value;
517         return (floatToRawIntBits(value) == floatToRawIntBits(f)) ||
518           (isNaN(value) && isNaN(f));
519       }
520     return false;
521   }*/
522
523   /**
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.
531    *
532    * @param value the <code>float</code> to convert
533    * @return the bits of the <code>float</code>
534    * @see #intBitsToFloat(int)
535    */
536   /*public static int floatToIntBits(float value)
537   {
538     if (isNaN(value))
539       return 0x7fc00000;
540     else
541       return VMFloat.floatToRawIntBits(value);
542   }*/
543
544   /**
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.
552    *
553    * @param value the <code>float</code> to convert
554    * @return the bits of the <code>float</code>
555    * @see #intBitsToFloat(int)
556    */
557   /*public static int floatToRawIntBits(float value)
558   {
559     return VMFloat.floatToRawIntBits(value);
560   }*/
561
562   /**
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>.
569    *
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)
574    */
575   /*public static float intBitsToFloat(int bits)
576   {
577     return VMFloat.intBitsToFloat(bits);
578   }*/
579
580   /**
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.
587    *
588    * @param f the Float to compare
589    * @return the comparison
590    * @since 1.2
591    */
592   /*public int compareTo(Float f)
593   {
594     return compare(value, f.value);
595   }*/
596
597   /**
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.
601    *
602    * @param x the first float to compare
603    * @param y the second float to compare
604    * @return the comparison
605    * @since 1.4
606    */
607   /*public static int compare(float x, float y)
608   {
609       // handle the easy cases:
610       if (x < y)
611           return -1;
612       if (x > y)
613           return 1;
614
615       // handle equality respecting that 0.0 != -0.0 (hence not using x == y):
616       int ix = floatToRawIntBits(x);
617       int iy = floatToRawIntBits(y);
618       if (ix == iy)
619           return 0;
620
621       // handle NaNs:
622       if (x != x)
623           return (y != y) ? 0 : 1;
624       else if (y != y)
625           return -1;
626
627       // handle +/- 0.0
628       return (ix < iy) ? -1 : 1;
629   }*/
630 }