reformat benchmark source codes to meet the requirements of the annotation generation.
[IRC.git] / Robust / src / ClassLibrary / SSJavaInfer / Double.java
1 /* Double.java -- object wrapper for double
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 //package java.lang;
40
41 /**
42  * Instances of class <code>Double</code> represent primitive
43  * <code>double</code> values.
44  * 
45  * Additionally, this class provides various helper functions and variables
46  * related to doubles.
47  * 
48  * @author Paul Fisher
49  * @author Andrew Haley (aph@cygnus.com)
50  * @author Eric Blake (ebb9@email.byu.edu)
51  * @author Tom Tromey (tromey@redhat.com)
52  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
53  * @since 1.0
54  * @status partly updated to 1.5
55  */
56
57 public final class Double extends Number // implements Comparable<Double>
58 {
59   /**
60    * Compatible with JDK 1.0+.
61    */
62   /**
63    * The immutable value of this Double.
64    * 
65    * @serial the wrapped double
66    */
67
68   private final double value;
69
70   /**
71    * Create a <code>Double</code> from the primitive <code>double</code>
72    * specified.
73    * 
74    * @param value
75    *          the <code>double</code> argument
76    */
77   public Double(double value) {
78     this.value = value;
79   }
80
81   /**
82    * Create a <code>Double</code> from the specified <code>String</code>. This
83    * method calls <code>Double.parseDouble()</code>.
84    * 
85    * @param s
86    *          the <code>String</code> to convert
87    * @throws NumberFormatException
88    *           if <code>s</code> cannot be parsed as a <code>double</code>
89    * @throws NullPointerException
90    *           if <code>s</code> is null
91    * @see #parseDouble(String)
92    */
93   public Double(String s) {
94     value = parseDouble(s);
95   }
96
97   /**
98    * Convert the <code>double</code> to a <code>String</code>. Floating-point
99    * string representation is fairly complex: here is a rundown of the possible
100    * values. "<code>[-]</code>" indicates that a negative sign will be printed
101    * if the value (or exponent) is negative. "<code>&lt;number&gt;</code>" means
102    * a string of digits ('0' to '9'). "<code>&lt;digit&gt;</code>" means a
103    * single digit ('0' to '9').<br>
104    * 
105    * <table border=1>
106    * <tr>
107    * <th>Value of Double</th>
108    * <th>String Representation</th>
109    * </tr>
110    * <tr>
111    * <td>[+-] 0</td>
112    * <td><code>[-]0.0</code></td>
113    * </tr>
114    * <tr>
115    * <td>Between [+-] 10<sup>-3</sup> and 10<sup>7</sup>, exclusive</td>
116    * <td><code>[-]number.number</code></td>
117    * </tr>
118    * <tr>
119    * <td>Other numeric value</td>
120    * <td><code>[-]&lt;digit&gt;.&lt;number&gt;
121    *          E[-]&lt;number&gt;</code></td>
122    * </tr>
123    * <tr>
124    * <td>[+-] infinity</td>
125    * <td><code>[-]Infinity</code></td>
126    * </tr>
127    * <tr>
128    * <td>NaN</td>
129    * <td><code>NaN</code></td>
130    * </tr>
131    * </table>
132    * 
133    * Yes, negative zero <em>is</em> a possible value. Note that there is
134    * <em>always</em> a <code>.</code> and at least one digit printed after it:
135    * even if the number is 3, it will be printed as <code>3.0</code>. After the
136    * ".", all digits will be printed except trailing zeros. The result is
137    * rounded to the shortest decimal number which will parse back to the same
138    * double.
139    * 
140    * <p>
141    * To create other output formats, use {@link java.text.NumberFormat}.
142    * 
143    * @XXX specify where we are not in accord with the spec.
144    * 
145    * @param d
146    *          the <code>double</code> to convert
147    * @return the <code>String</code> representing the <code>double</code>
148    */
149   public static String toString(double d) {
150     return String.valueOf(d);
151   }
152
153   /**
154    * Convert a double value to a hexadecimal string. This converts as follows:
155    * <ul>
156    * <li>A NaN value is converted to the string "NaN".
157    * <li>Positive infinity is converted to the string "Infinity".
158    * <li>Negative infinity is converted to the string "-Infinity".
159    * <li>For all other values, the first character of the result is '-' if the
160    * value is negative. This is followed by '0x1.' if the value is normal, and
161    * '0x0.' if the value is denormal. This is then followed by a (lower-case)
162    * hexadecimal representation of the mantissa, with leading zeros as required
163    * for denormal values. The next character is a 'p', and this is followed by a
164    * decimal representation of the unbiased exponent.
165    * </ul>
166    * 
167    * @param d
168    *          the double value
169    * @return the hexadecimal string representation
170    * @since 1.5
171    */
172   public static String toHexString(double d) {
173     /*
174      * if (isNaN(d)) return "NaN"; if (isInfinite(d)) return d < 0 ? "-Infinity"
175      * : "Infinity";
176      * 
177      * long bits = doubleToLongBits(d); StringBuilder result = new
178      * StringBuilder();
179      * 
180      * if (bits < 0) result.append('-'); result.append("0x");
181      * 
182      * final int mantissaBits = 52; final int exponentBits = 11; long mantMask =
183      * (1L << mantissaBits) - 1; long mantissa = bits & mantMask; long expMask =
184      * (1L << exponentBits) - 1; long exponent = (bits >>> mantissaBits) &
185      * expMask;
186      * 
187      * result.append(exponent == 0 ? '0' : '1'); result.append('.');
188      * result.append(Long.toHexString(mantissa)); if (exponent == 0 && mantissa
189      * != 0) { // Treat denormal specially by inserting '0's to make // the
190      * length come out right. The constants here are // to account for things
191      * like the '0x'. int offset = 4 + ((bits < 0) ? 1 : 0); // The silly +3 is
192      * here to keep the code the same between // the Float and Double cases. In
193      * Float the value is // not a multiple of 4. int desiredLength = offset +
194      * (mantissaBits + 3) / 4; while (result.length() < desiredLength)
195      * result.insert(offset, '0'); } result.append('p'); if (exponent == 0 &&
196      * mantissa == 0) { // Zero, so do nothing special. } else { // Apply bias.
197      * boolean denormal = exponent == 0; exponent -= (1 << (exponentBits - 1)) -
198      * 1; // Handle denormal. if (denormal) ++exponent; }
199      * 
200      * result.append(Long.toString(exponent)); return result.toString();
201      */
202     return "0x0";
203   }
204
205   /**
206    * Returns a <code>Double</code> object wrapping the value. In contrast to the
207    * <code>Double</code> constructor, this method may cache some values. It is
208    * used by boxing conversion.
209    * 
210    * @param val
211    *          the value to wrap
212    * @return the <code>Double</code>
213    * @since 1.5
214    */
215   public static Double valueOf(double val) {
216     // We don't actually cache, but we could.
217     return new Double(val);
218   }
219
220   /**
221    * Create a new <code>Double</code> object using the <code>String</code>.
222    * 
223    * @param s
224    *          the <code>String</code> to convert
225    * @return the new <code>Double</code>
226    * @throws NumberFormatException
227    *           if <code>s</code> cannot be parsed as a <code>double</code>
228    * @throws NullPointerException
229    *           if <code>s</code> is null.
230    * @see #parseDouble(String)
231    */
232   public static Double valueOf(String s) {
233     return new Double(parseDouble(s));
234   }
235
236   /**
237    * Parse the specified <code>String</code> as a <code>double</code>. The
238    * extended BNF grammar is as follows:<br>
239    * 
240    * <pre>
241    * <em>DecodableString</em>:
242    *      ( [ <code>-</code> | <code>+</code> ] <code>NaN</code> )
243    *    | ( [ <code>-</code> | <code>+</code> ] <code>Infinity</code> )
244    *    | ( [ <code>-</code> | <code>+</code> ] <em>FloatingPoint</em>
245    *              [ <code>f</code> | <code>F</code> | <code>d</code>
246    *                | <code>D</code>] )
247    * <em>FloatingPoint</em>:
248    *      ( { <em>Digit</em> }+ [ <code>.</code> { <em>Digit</em> } ]
249    *              [ <em>Exponent</em> ] )
250    *    | ( <code>.</code> { <em>Digit</em> }+ [ <em>Exponent</em> ] )
251    * <em>Exponent</em>:
252    *      ( ( <code>e</code> | <code>E</code> )
253    *              [ <code>-</code> | <code>+</code> ] { <em>Digit</em> }+ )
254    * <em>Digit</em>: <em><code>'0'</code> through <code>'9'</code></em>
255    * </pre>
256    * 
257    * <p>
258    * NaN and infinity are special cases, to allow parsing of the output of
259    * toString. Otherwise, the result is determined by calculating
260    * <em>n * 10<sup>exponent</sup></em> to infinite precision, then rounding to
261    * the nearest double. Remember that many numbers cannot be precisely
262    * represented in floating point. In case of overflow, infinity is used, and
263    * in case of underflow, signed zero is used. Unlike Integer.parseInt, this
264    * does not accept Unicode digits outside the ASCII range.
265    * 
266    * <p>
267    * If an unexpected character is found in the <code>String</code>, a
268    * <code>NumberFormatException</code> will be thrown. Leading and trailing
269    * 'whitespace' is ignored via <code>String.trim()</code>, but spaces internal
270    * to the actual number are not allowed.
271    * 
272    * <p>
273    * To parse numbers according to another format, consider using
274    * {@link java.text.NumberFormat}.
275    * 
276    * @XXX specify where/how we are not in accord with the spec.
277    * 
278    * @param str
279    *          the <code>String</code> to convert
280    * @return the <code>double</code> value of <code>s</code>
281    * @throws NumberFormatException
282    *           if <code>s</code> cannot be parsed as a <code>double</code>
283    * @throws NullPointerException
284    *           if <code>s</code> is null
285    * @see #MIN_VALUE
286    * @see #MAX_VALUE
287    * @see #POSITIVE_INFINITY
288    * @see #NEGATIVE_INFINITY
289    * @since 1.2
290    */
291   public static double parseDouble(String str) {
292     return nativeparsedouble(str);
293   }
294
295   public static native double nativeparsedouble(String str);
296
297   public static native double nativeparsedouble(int start, int length, byte[] str);
298
299   /**
300    * Return <code>true</code> if the <code>double</code> has the same value as
301    * <code>NaN</code>, otherwise return <code>false</code>.
302    * 
303    * @param v
304    *          the <code>double</code> to compare
305    * @return whether the argument is <code>NaN</code>.
306    */
307   public static boolean isNaN(double v) {
308     // This works since NaN != NaN is the only reflexive inequality
309     // comparison which returns true.
310     return v != v;
311   }
312
313   /**
314    * Return <code>true</code> if the <code>double</code> has a value equal to
315    * either <code>NEGATIVE_INFINITY</code> or <code>POSITIVE_INFINITY</code>,
316    * otherwise return <code>false</code>.
317    * 
318    * @param v
319    *          the <code>double</code> to compare
320    * @return whether the argument is (-/+) infinity.
321    */
322   public static boolean isInfinite(double v) {
323     return false;
324   }
325
326   /**
327    * Return <code>true</code> if the value of this <code>Double</code> is the
328    * same as <code>NaN</code>, otherwise return <code>false</code>.
329    * 
330    * @return whether this <code>Double</code> is <code>NaN</code>
331    */
332   public boolean isNaN() {
333     return isNaN(value);
334   }
335
336   /**
337    * Return <code>true</code> if the value of this <code>Double</code> is the
338    * same as <code>NEGATIVE_INFINITY</code> or <code>POSITIVE_INFINITY</code>,
339    * otherwise return <code>false</code>.
340    * 
341    * @return whether this <code>Double</code> is (-/+) infinity
342    */
343   public boolean isInfinite() {
344     return isInfinite(value);
345   }
346
347   /**
348    * Convert the <code>double</code> value of this <code>Double</code> to a
349    * <code>String</code>. This method calls <code>Double.toString(double)</code>
350    * to do its dirty work.
351    * 
352    * @return the <code>String</code> representation
353    * @see #toString(double)
354    */
355   public String toString() {
356     return toString(value);
357   }
358
359   /**
360    * Return the value of this <code>Double</code> as a <code>byte</code>.
361    * 
362    * @return the byte value
363    * @since 1.1
364    */
365   public byte byteValue() {
366     return (byte) value;
367   }
368
369   /**
370    * Return the value of this <code>Double</code> as a <code>short</code>.
371    * 
372    * @return the short value
373    * @since 1.1
374    */
375   public short shortValue() {
376     return (short) value;
377   }
378
379   /**
380    * Return the value of this <code>Double</code> as an <code>int</code>.
381    * 
382    * @return the int value
383    */
384   public int intValue() {
385     return (int) value;
386   }
387
388   /**
389    * Return the value of this <code>Double</code> as a <code>long</code>.
390    * 
391    * @return the long value
392    */
393   public long longValue() {
394     return (long) value;
395   }
396
397   /**
398    * Return the value of this <code>Double</code> as a <code>float</code>.
399    * 
400    * @return the float value
401    */
402   public float floatValue() {
403     return (float) value;
404   }
405
406   /**
407    * Return the value of this <code>Double</code>.
408    * 
409    * @return the double value
410    */
411   public double doubleValue() {
412     return value;
413   }
414
415   /**
416    * Return a hashcode representing this Object. <code>Double</code>'s hash code
417    * is calculated by:<br>
418    * <code>long v = Double.doubleToLongBits(doubleValue());<br>
419    *    int hash = (int)(v^(v&gt;&gt;32))</code>.
420    * 
421    * @return this Object's hash code
422    * @see #doubleToLongBits(double)
423    */
424   public int hashCode() {
425     long v = doubleToLongBits(value);
426     return (int) (v ^ (v >>> 32));
427   }
428
429   /**
430    * Returns <code>true</code> if <code>obj</code> is an instance of
431    * <code>Double</code> and represents the same double value. Unlike comparing
432    * two doubles with <code>==</code>, this treats two instances of
433    * <code>Double.NaN</code> as equal, but treats <code>0.0</code> and
434    * <code>-0.0</code> as unequal.
435    * 
436    * <p>
437    * Note that <code>d1.equals(d2)</code> is identical to
438    * <code>doubleToLongBits(d1.doubleValue()) ==
439    *    doubleToLongBits(d2.doubleValue())</code>.
440    * 
441    * @param obj
442    *          the object to compare
443    * @return whether the objects are semantically equal
444    */
445   public boolean equals(Object obj) {
446     if (!(obj instanceof Double))
447       return false;
448
449     double d = ((Double) obj).value;
450
451     // Avoid call to native method. However, some implementations, like gcj,
452     // are better off using floatToIntBits(value) == floatToIntBits(f).
453     // Check common case first, then check NaN and 0.
454     if (value == d)
455       return (value != 0) || (1 / value == 1 / d);
456     return isNaN(value) && isNaN(d);
457   }
458
459   /**
460    * Convert the double to the IEEE 754 floating-point "double format" bit
461    * layout. Bit 63 (the most significant) is the sign bit, bits 62-52 (masked
462    * by 0x7ff0000000000000L) represent the exponent, and bits 51-0 (masked by
463    * 0x000fffffffffffffL) are the mantissa. This function collapses all versions
464    * of NaN to 0x7ff8000000000000L. The result of this function can be used as
465    * the argument to <code>Double.longBitsToDouble(long)</code> to obtain the
466    * original <code>double</code> value.
467    * 
468    * @param value
469    *          the <code>double</code> to convert
470    * @return the bits of the <code>double</code>
471    * @see #longBitsToDouble(long)
472    */
473   public static long doubleToLongBits(double value) {
474     if (isNaN(value))
475       return 0x7ff8000000000000L;
476     else
477       return /* VMDouble. */doubleToRawLongBits(value);
478   }
479
480   /**
481    * Convert the double to the IEEE 754 floating-point "double format" bit
482    * layout. Bit 63 (the most significant) is the sign bit, bits 62-52 (masked
483    * by 0x7ff0000000000000L) represent the exponent, and bits 51-0 (masked by
484    * 0x000fffffffffffffL) are the mantissa. This function leaves NaN alone,
485    * rather than collapsing to a canonical value. The result of this function
486    * can be used as the argument to <code>Double.longBitsToDouble(long)</code>
487    * to obtain the original <code>double</code> value.
488    * 
489    * @param value
490    *          the <code>double</code> to convert
491    * @return the bits of the <code>double</code>
492    * @see #longBitsToDouble(long)
493    */
494   /*
495    * public static long doubleToRawLongBits(double value) { return
496    * VMDouble.doubleToRawLongBits(value); }
497    */
498   public static native long doubleToRawLongBits(double value);
499
500   /**
501    * Convert the argument in IEEE 754 floating-point "double format" bit layout
502    * to the corresponding float. Bit 63 (the most significant) is the sign bit,
503    * bits 62-52 (masked by 0x7ff0000000000000L) represent the exponent, and bits
504    * 51-0 (masked by 0x000fffffffffffffL) are the mantissa. This function leaves
505    * NaN alone, so that you can recover the bit pattern with
506    * <code>Double.doubleToRawLongBits(double)</code>.
507    * 
508    * @param bits
509    *          the bits to convert
510    * @return the <code>double</code> represented by the bits
511    * @see #doubleToLongBits(double)
512    * @see #doubleToRawLongBits(double)
513    */
514   /*
515    * public static double longBitsToDouble(long bits) { return
516    * VMDouble.longBitsToDouble(bits); }
517    */
518   public static native double longBitsToDouble(long bits);
519
520   /**
521    * Compare two Doubles numerically by comparing their <code>double</code>
522    * values. The result is positive if the first is greater, negative if the
523    * second is greater, and 0 if the two are equal. However, this special cases
524    * NaN and signed zero as follows: NaN is considered greater than all other
525    * doubles, including <code>POSITIVE_INFINITY</code>, and positive zero is
526    * considered greater than negative zero.
527    * 
528    * @param d
529    *          the Double to compare
530    * @return the comparison
531    * @since 1.2
532    */
533   public int compareTo(Double d) {
534     return compare(value, d.value);
535   }
536
537   /**
538    * Behaves like <code>new Double(x).compareTo(new Double(y))</code>; in other
539    * words this compares two doubles, special casing NaN and zero, without the
540    * overhead of objects.
541    * 
542    * @param x
543    *          the first double to compare
544    * @param y
545    *          the second double to compare
546    * @return the comparison
547    * @since 1.4
548    */
549   public static int compare(double x, double y) {
550     // handle the easy cases:
551     if (x < y)
552       return -1;
553     if (x > y)
554       return 1;
555
556     // handle equality respecting that 0.0 != -0.0 (hence not using x == y):
557     long lx = doubleToRawLongBits(x);
558     long ly = doubleToRawLongBits(y);
559     if (lx == ly)
560       return 0;
561
562     // handle NaNs:
563     if (x != x)
564       return (y != y) ? 0 : 1;
565     else if (y != y)
566       return -1;
567
568     // handle +/- 0.0
569     return (lx < ly) ? -1 : 1;
570   }
571 }