Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / lang / Double.java
blob81985f04e05b250148154970f451d13b190fd116
1 /* Double.java -- object wrapper for double
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. */
39 package java.lang;
41 import gnu.classpath.Configuration;
43 /**
44 * Instances of class <code>Double</code> represent primitive
45 * <code>double</code> values.
47 * Additionally, this class provides various helper functions and variables
48 * related to doubles.
50 * @author Paul Fisher
51 * @author Andrew Haley (aph@cygnus.com)
52 * @author Eric Blake (ebb9@email.byu.edu)
53 * @since 1.0
54 * @status updated to 1.4
56 public final class Double extends Number implements Comparable
58 /**
59 * Compatible with JDK 1.0+.
61 private static final long serialVersionUID = -9172774392245257468L;
63 /**
64 * The maximum positive value a <code>double</code> may represent
65 * is 1.7976931348623157e+308.
67 public static final double MAX_VALUE = 1.7976931348623157e+308;
69 /**
70 * The minimum positive value a <code>double</code> may represent
71 * is 5e-324.
73 public static final double MIN_VALUE = 5e-324;
75 /**
76 * The value of a double representation -1.0/0.0, negative
77 * infinity.
79 public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
81 /**
82 * The value of a double representing 1.0/0.0, positive infinity.
84 public static final double POSITIVE_INFINITY = 1.0 / 0.0;
86 /**
87 * All IEEE 754 values of NaN have the same value in Java.
89 public static final double NaN = 0.0 / 0.0;
91 /**
92 * The primitive type <code>double</code> is represented by this
93 * <code>Class</code> object.
94 * @since 1.1
96 public static final Class TYPE = VMClassLoader.getPrimitiveClass('D');
98 /**
99 * The immutable value of this Double.
101 * @serial the wrapped double
103 private final double value;
106 * Load native routines necessary for this class.
108 static
110 if (Configuration.INIT_LOAD_LIBRARY)
112 System.loadLibrary("javalang");
113 initIDs();
118 * Create a <code>Double</code> from the primitive <code>double</code>
119 * specified.
121 * @param value the <code>double</code> argument
123 public Double(double value)
125 this.value = value;
129 * Create a <code>Double</code> from the specified <code>String</code>.
130 * This method calls <code>Double.parseDouble()</code>.
132 * @param s the <code>String</code> to convert
133 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
134 * <code>double</code>
135 * @throws NullPointerException if <code>s</code> is null
136 * @see #parseDouble(String)
138 public Double(String s)
140 value = parseDouble(s);
144 * Convert the <code>double</code> to a <code>String</code>.
145 * Floating-point string representation is fairly complex: here is a
146 * rundown of the possible values. "<code>[-]</code>" indicates that a
147 * negative sign will be printed if the value (or exponent) is negative.
148 * "<code>&lt;number&gt;</code>" means a string of digits ('0' to '9').
149 * "<code>&lt;digit&gt;</code>" means a single digit ('0' to '9').<br>
151 * <table border=1>
152 * <tr><th>Value of Double</th><th>String Representation</th></tr>
153 * <tr><td>[+-] 0</td> <td><code>[-]0.0</code></td></tr>
154 * <tr><td>Between [+-] 10<sup>-3</sup> and 10<sup>7</sup>, exclusive</td>
155 * <td><code>[-]number.number</code></td></tr>
156 * <tr><td>Other numeric value</td>
157 * <td><code>[-]&lt;digit&gt;.&lt;number&gt;
158 * E[-]&lt;number&gt;</code></td></tr>
159 * <tr><td>[+-] infinity</td> <td><code>[-]Infinity</code></td></tr>
160 * <tr><td>NaN</td> <td><code>NaN</code></td></tr>
161 * </table>
163 * Yes, negative zero <em>is</em> a possible value. Note that there is
164 * <em>always</em> a <code>.</code> and at least one digit printed after
165 * it: even if the number is 3, it will be printed as <code>3.0</code>.
166 * After the ".", all digits will be printed except trailing zeros. The
167 * result is rounded to the shortest decimal number which will parse back
168 * to the same double.
170 * <p>To create other output formats, use {@link java.text.NumberFormat}.
172 * @XXX specify where we are not in accord with the spec.
174 * @param d the <code>double</code> to convert
175 * @return the <code>String</code> representing the <code>double</code>
177 public static String toString(double d)
179 return toString(d, false);
183 * Create a new <code>Double</code> object using the <code>String</code>.
185 * @param s the <code>String</code> to convert
186 * @return the new <code>Double</code>
187 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
188 * <code>double</code>
189 * @throws NullPointerException if <code>s</code> is null.
190 * @see #parseDouble(String)
192 public static Double valueOf(String s)
194 return new Double(parseDouble(s));
198 * Parse the specified <code>String</code> as a <code>double</code>. The
199 * extended BNF grammar is as follows:<br>
200 * <pre>
201 * <em>DecodableString</em>:
202 * ( [ <code>-</code> | <code>+</code> ] <code>NaN</code> )
203 * | ( [ <code>-</code> | <code>+</code> ] <code>Infinity</code> )
204 * | ( [ <code>-</code> | <code>+</code> ] <em>FloatingPoint</em>
205 * [ <code>f</code> | <code>F</code> | <code>d</code>
206 * | <code>D</code>] )
207 * <em>FloatingPoint</em>:
208 * ( { <em>Digit</em> }+ [ <code>.</code> { <em>Digit</em> } ]
209 * [ <em>Exponent</em> ] )
210 * | ( <code>.</code> { <em>Digit</em> }+ [ <em>Exponent</em> ] )
211 * <em>Exponent</em>:
212 * ( ( <code>e</code> | <code>E</code> )
213 * [ <code>-</code> | <code>+</code> ] { <em>Digit</em> }+ )
214 * <em>Digit</em>: <em><code>'0'</code> through <code>'9'</code></em>
215 * </pre>
217 * <p>NaN and infinity are special cases, to allow parsing of the output
218 * of toString. Otherwise, the result is determined by calculating
219 * <em>n * 10<sup>exponent</sup></em> to infinite precision, then rounding
220 * to the nearest double. Remember that many numbers cannot be precisely
221 * represented in floating point. In case of overflow, infinity is used,
222 * and in case of underflow, signed zero is used. Unlike Integer.parseInt,
223 * this does not accept Unicode digits outside the ASCII range.
225 * <p>If an unexpected character is found in the <code>String</code>, a
226 * <code>NumberFormatException</code> will be thrown. Leading and trailing
227 * 'whitespace' is ignored via <code>String.trim()</code>, but spaces
228 * internal to the actual number are not allowed.
230 * <p>To parse numbers according to another format, consider using
231 * {@link java.text.NumberFormat}.
233 * @XXX specify where/how we are not in accord with the spec.
235 * @param str the <code>String</code> to convert
236 * @return the <code>double</code> value of <code>s</code>
237 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
238 * <code>double</code>
239 * @throws NullPointerException if <code>s</code> is null
240 * @see #MIN_VALUE
241 * @see #MAX_VALUE
242 * @see #POSITIVE_INFINITY
243 * @see #NEGATIVE_INFINITY
244 * @since 1.2
246 public static native double parseDouble(String str);
249 * Return <code>true</code> if the <code>double</code> has the same
250 * value as <code>NaN</code>, otherwise return <code>false</code>.
252 * @param v the <code>double</code> to compare
253 * @return whether the argument is <code>NaN</code>.
255 public static boolean isNaN(double v)
257 // This works since NaN != NaN is the only reflexive inequality
258 // comparison which returns true.
259 return v != v;
263 * Return <code>true</code> if the <code>double</code> has a value
264 * equal to either <code>NEGATIVE_INFINITY</code> or
265 * <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
267 * @param v the <code>double</code> to compare
268 * @return whether the argument is (-/+) infinity.
270 public static boolean isInfinite(double v)
272 return v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY;
276 * Return <code>true</code> if the value of this <code>Double</code>
277 * is the same as <code>NaN</code>, otherwise return <code>false</code>.
279 * @return whether this <code>Double</code> is <code>NaN</code>
281 public boolean isNaN()
283 return isNaN(value);
287 * Return <code>true</code> if the value of this <code>Double</code>
288 * is the same as <code>NEGATIVE_INFINITY</code> or
289 * <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
291 * @return whether this <code>Double</code> is (-/+) infinity
293 public boolean isInfinite()
295 return isInfinite(value);
299 * Convert the <code>double</code> value of this <code>Double</code>
300 * to a <code>String</code>. This method calls
301 * <code>Double.toString(double)</code> to do its dirty work.
303 * @return the <code>String</code> representation
304 * @see #toString(double)
306 public String toString()
308 return toString(value);
312 * Return the value of this <code>Double</code> as a <code>byte</code>.
314 * @return the byte value
315 * @since 1.1
317 public byte byteValue()
319 return (byte) value;
323 * Return the value of this <code>Double</code> as a <code>short</code>.
325 * @return the short value
326 * @since 1.1
328 public short shortValue()
330 return (short) value;
334 * Return the value of this <code>Double</code> as an <code>int</code>.
336 * @return the int value
338 public int intValue()
340 return (int) value;
344 * Return the value of this <code>Double</code> as a <code>long</code>.
346 * @return the long value
348 public long longValue()
350 return (long) value;
354 * Return the value of this <code>Double</code> as a <code>float</code>.
356 * @return the float value
358 public float floatValue()
360 return (float) value;
364 * Return the value of this <code>Double</code>.
366 * @return the double value
368 public double doubleValue()
370 return value;
374 * Return a hashcode representing this Object. <code>Double</code>'s hash
375 * code is calculated by:<br>
376 * <code>long v = Double.doubleToLongBits(doubleValue());<br>
377 * int hash = (int)(v^(v&gt;&gt;32))</code>.
379 * @return this Object's hash code
380 * @see #doubleToLongBits(double)
382 public int hashCode()
384 long v = doubleToLongBits(value);
385 return (int) (v ^ (v >>> 32));
389 * Returns <code>true</code> if <code>obj</code> is an instance of
390 * <code>Double</code> and represents the same double value. Unlike comparing
391 * two doubles with <code>==</code>, this treats two instances of
392 * <code>Double.NaN</code> as equal, but treats <code>0.0</code> and
393 * <code>-0.0</code> as unequal.
395 * <p>Note that <code>d1.equals(d2)</code> is identical to
396 * <code>doubleToLongBits(d1.doubleValue()) ==
397 * doubleToLongBits(d2.doubleValue())</code>.
399 * @param obj the object to compare
400 * @return whether the objects are semantically equal
402 public boolean equals(Object obj)
404 if (! (obj instanceof Double))
405 return false;
407 double d = ((Double) obj).value;
409 // Avoid call to native method. However, some implementations, like gcj,
410 // are better off using floatToIntBits(value) == floatToIntBits(f).
411 // Check common case first, then check NaN and 0.
412 if (value == d)
413 return (value != 0) || (1 / value == 1 / d);
414 return isNaN(value) && isNaN(d);
418 * Convert the double to the IEEE 754 floating-point "double format" bit
419 * layout. Bit 63 (the most significant) is the sign bit, bits 62-52
420 * (masked by 0x7ff0000000000000L) represent the exponent, and bits 51-0
421 * (masked by 0x000fffffffffffffL) are the mantissa. This function
422 * collapses all versions of NaN to 0x7ff8000000000000L. The result of this
423 * function can be used as the argument to
424 * <code>Double.longBitsToDouble(long)</code> to obtain the original
425 * <code>double</code> value.
427 * @param value the <code>double</code> to convert
428 * @return the bits of the <code>double</code>
429 * @see #longBitsToDouble(long)
431 // GCJ LOCAL: We diverge from Classpath for efficiency.
432 public static native long doubleToLongBits(double value);
433 // END GCJ LOCAL
436 * Convert the double to the IEEE 754 floating-point "double format" bit
437 * layout. Bit 63 (the most significant) is the sign bit, bits 62-52
438 * (masked by 0x7ff0000000000000L) represent the exponent, and bits 51-0
439 * (masked by 0x000fffffffffffffL) are the mantissa. This function
440 * leaves NaN alone, rather than collapsing to a canonical value. The
441 * result of this function can be used as the argument to
442 * <code>Double.longBitsToDouble(long)</code> to obtain the original
443 * <code>double</code> value.
445 * @param value the <code>double</code> to convert
446 * @return the bits of the <code>double</code>
447 * @see #longBitsToDouble(long)
449 // GCJ LOCAL: We diverge from Classpath for efficiency.
450 public static native long doubleToRawLongBits(double value);
451 // END GCJ LOCAL
454 * Convert the argument in IEEE 754 floating-point "double format" bit
455 * layout to the corresponding float. Bit 63 (the most significant) is the
456 * sign bit, bits 62-52 (masked by 0x7ff0000000000000L) represent the
457 * exponent, and bits 51-0 (masked by 0x000fffffffffffffL) are the mantissa.
458 * This function leaves NaN alone, so that you can recover the bit pattern
459 * with <code>Double.doubleToRawLongBits(double)</code>.
461 * @param bits the bits to convert
462 * @return the <code>double</code> represented by the bits
463 * @see #doubleToLongBits(double)
464 * @see #doubleToRawLongBits(double)
466 // GCJ LOCAL: We diverge from Classpath for efficiency.
467 public static native double longBitsToDouble(long bits);
468 // END GCJ LOCAL
471 * Compare two Doubles numerically by comparing their <code>double</code>
472 * values. The result is positive if the first is greater, negative if the
473 * second is greater, and 0 if the two are equal. However, this special
474 * cases NaN and signed zero as follows: NaN is considered greater than
475 * all other doubles, including <code>POSITIVE_INFINITY</code>, and positive
476 * zero is considered greater than negative zero.
478 * @param d the Double to compare
479 * @return the comparison
480 * @since 1.2
482 public int compareTo(Double d)
484 return compare(value, d.value);
488 * Behaves like <code>compareTo(Double)</code> unless the Object
489 * is not an <code>Double</code>.
491 * @param o the object to compare
492 * @return the comparison
493 * @throws ClassCastException if the argument is not a <code>Double</code>
494 * @see #compareTo(Double)
495 * @see Comparable
496 * @since 1.2
498 public int compareTo(Object o)
500 return compare(value, ((Double) o).value);
504 * Behaves like <code>new Double(x).compareTo(new Double(y))</code>; in
505 * other words this compares two doubles, special casing NaN and zero,
506 * without the overhead of objects.
508 * @param x the first double to compare
509 * @param y the second double to compare
510 * @return the comparison
511 * @since 1.4
513 public static int compare(double x, double y)
515 if (isNaN(x))
516 return isNaN(y) ? 0 : 1;
517 if (isNaN(y))
518 return -1;
519 // recall that 0.0 == -0.0, so we convert to infinites and try again
520 if (x == 0 && y == 0)
521 return (int) (1 / x - 1 / y);
522 if (x == y)
523 return 0;
525 return x > y ? 1 : -1;
529 * Helper method to convert to string.
531 * @param d the double to convert
532 * @param isFloat true if the conversion is requested by Float (results in
533 * fewer digits)
535 // Package visible for use by Float.
536 static native String toString(double d, boolean isFloat);
539 * Initialize JNI cache. This method is called only by the
540 * static initializer when using JNI.
542 private static native void initIDs();