Add C++11 header <cuchar>.
[official-gcc.git] / libjava / classpath / java / lang / Double.java
blob3ae1b0111affc8dcd4241505edbe206cc9f81007
1 /* Double.java -- object wrapper for double
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)
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., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 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.java.lang.CPStringBuilder;
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 * @author Tom Tromey (tromey@redhat.com)
54 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
55 * @since 1.0
56 * @status partly updated to 1.5
58 public final class Double extends Number implements Comparable<Double>
60 /**
61 * Compatible with JDK 1.0+.
63 private static final long serialVersionUID = -9172774392245257468L;
65 /**
66 * The maximum positive value a <code>double</code> may represent
67 * is 1.7976931348623157e+308.
69 public static final double MAX_VALUE = 1.7976931348623157e+308;
71 /**
72 * The minimum positive value a <code>double</code> may represent
73 * is 5e-324.
75 public static final double MIN_VALUE = 5e-324;
77 /**
78 * The value of a double representation -1.0/0.0, negative
79 * infinity.
81 public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
83 /**
84 * The value of a double representing 1.0/0.0, positive infinity.
86 public static final double POSITIVE_INFINITY = 1.0 / 0.0;
88 /**
89 * All IEEE 754 values of NaN have the same value in Java.
91 public static final double NaN = 0.0 / 0.0;
93 /**
94 * The number of bits needed to represent a <code>double</code>.
95 * @since 1.5
97 public static final int SIZE = 64;
99 /**
100 * The primitive type <code>double</code> is represented by this
101 * <code>Class</code> object.
102 * @since 1.1
104 public static final Class<Double> TYPE = (Class<Double>) VMClassLoader.getPrimitiveClass('D');
107 * Cache representation of 0
109 private static final Double ZERO = new Double(0.0d);
112 * Cache representation of 1
114 private static final Double ONE = new Double(1.0d);
117 * The immutable value of this Double.
119 * @serial the wrapped double
121 private final double value;
124 * Create a <code>Double</code> from the primitive <code>double</code>
125 * specified.
127 * @param value the <code>double</code> argument
129 public Double(double value)
131 this.value = value;
135 * Create a <code>Double</code> from the specified <code>String</code>.
136 * This method calls <code>Double.parseDouble()</code>.
138 * @param s the <code>String</code> to convert
139 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
140 * <code>double</code>
141 * @throws NullPointerException if <code>s</code> is null
142 * @see #parseDouble(String)
144 public Double(String s)
146 value = parseDouble(s);
150 * Convert the <code>double</code> to a <code>String</code>.
151 * Floating-point string representation is fairly complex: here is a
152 * rundown of the possible values. "<code>[-]</code>" indicates that a
153 * negative sign will be printed if the value (or exponent) is negative.
154 * "<code>&lt;number&gt;</code>" means a string of digits ('0' to '9').
155 * "<code>&lt;digit&gt;</code>" means a single digit ('0' to '9').<br>
157 * <table border=1>
158 * <tr><th>Value of Double</th><th>String Representation</th></tr>
159 * <tr><td>[+-] 0</td> <td><code>[-]0.0</code></td></tr>
160 * <tr><td>Between [+-] 10<sup>-3</sup> and 10<sup>7</sup>, exclusive</td>
161 * <td><code>[-]number.number</code></td></tr>
162 * <tr><td>Other numeric value</td>
163 * <td><code>[-]&lt;digit&gt;.&lt;number&gt;
164 * E[-]&lt;number&gt;</code></td></tr>
165 * <tr><td>[+-] infinity</td> <td><code>[-]Infinity</code></td></tr>
166 * <tr><td>NaN</td> <td><code>NaN</code></td></tr>
167 * </table>
169 * Yes, negative zero <em>is</em> a possible value. Note that there is
170 * <em>always</em> a <code>.</code> and at least one digit printed after
171 * it: even if the number is 3, it will be printed as <code>3.0</code>.
172 * After the ".", all digits will be printed except trailing zeros. The
173 * result is rounded to the shortest decimal number which will parse back
174 * to the same double.
176 * <p>To create other output formats, use {@link java.text.NumberFormat}.
178 * @XXX specify where we are not in accord with the spec.
180 * @param d the <code>double</code> to convert
181 * @return the <code>String</code> representing the <code>double</code>
183 public static String toString(double d)
185 return VMDouble.toString(d, false);
189 * Convert a double value to a hexadecimal string. This converts as
190 * follows:
191 * <ul>
192 * <li> A NaN value is converted to the string "NaN".
193 * <li> Positive infinity is converted to the string "Infinity".
194 * <li> Negative infinity is converted to the string "-Infinity".
195 * <li> For all other values, the first character of the result is '-'
196 * if the value is negative. This is followed by '0x1.' if the
197 * value is normal, and '0x0.' if the value is denormal. This is
198 * then followed by a (lower-case) hexadecimal representation of the
199 * mantissa, with leading zeros as required for denormal values.
200 * The next character is a 'p', and this is followed by a decimal
201 * representation of the unbiased exponent.
202 * </ul>
203 * @param d the double value
204 * @return the hexadecimal string representation
205 * @since 1.5
207 public static String toHexString(double d)
209 if (isNaN(d))
210 return "NaN";
211 if (isInfinite(d))
212 return d < 0 ? "-Infinity" : "Infinity";
214 long bits = doubleToLongBits(d);
215 CPStringBuilder result = new CPStringBuilder();
217 if (bits < 0)
218 result.append('-');
219 result.append("0x");
221 final int mantissaBits = 52;
222 final int exponentBits = 11;
223 long mantMask = (1L << mantissaBits) - 1;
224 long mantissa = bits & mantMask;
225 long expMask = (1L << exponentBits) - 1;
226 long exponent = (bits >>> mantissaBits) & expMask;
228 result.append(exponent == 0 ? '0' : '1');
229 result.append('.');
230 result.append(Long.toHexString(mantissa));
231 if (exponent == 0 && mantissa != 0)
233 // Treat denormal specially by inserting '0's to make
234 // the length come out right. The constants here are
235 // to account for things like the '0x'.
236 int offset = 4 + ((bits < 0) ? 1 : 0);
237 // The silly +3 is here to keep the code the same between
238 // the Float and Double cases. In Float the value is
239 // not a multiple of 4.
240 int desiredLength = offset + (mantissaBits + 3) / 4;
241 while (result.length() < desiredLength)
242 result.insert(offset, '0');
244 result.append('p');
245 if (exponent == 0 && mantissa == 0)
247 // Zero, so do nothing special.
249 else
251 // Apply bias.
252 boolean denormal = exponent == 0;
253 exponent -= (1 << (exponentBits - 1)) - 1;
254 // Handle denormal.
255 if (denormal)
256 ++exponent;
259 result.append(Long.toString(exponent));
260 return result.toString();
264 * Returns a <code>Double</code> object wrapping the value.
265 * In contrast to the <code>Double</code> constructor, this method
266 * may cache some values. It is used by boxing conversion.
268 * @param val the value to wrap
269 * @return the <code>Double</code>
270 * @since 1.5
272 public static Double valueOf(double val)
274 if ((val == 0.0) && (doubleToRawLongBits(val) == 0L))
275 return ZERO;
276 else if (val == 1.0)
277 return ONE;
278 else
279 return new Double(val);
283 * Create a new <code>Double</code> object using the <code>String</code>.
285 * @param s the <code>String</code> to convert
286 * @return the new <code>Double</code>
287 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
288 * <code>double</code>
289 * @throws NullPointerException if <code>s</code> is null.
290 * @see #parseDouble(String)
292 public static Double valueOf(String s)
294 return valueOf(parseDouble(s));
298 * Parse the specified <code>String</code> as a <code>double</code>. The
299 * extended BNF grammar is as follows:<br>
300 * <pre>
301 * <em>DecodableString</em>:
302 * ( [ <code>-</code> | <code>+</code> ] <code>NaN</code> )
303 * | ( [ <code>-</code> | <code>+</code> ] <code>Infinity</code> )
304 * | ( [ <code>-</code> | <code>+</code> ] <em>FloatingPoint</em>
305 * [ <code>f</code> | <code>F</code> | <code>d</code>
306 * | <code>D</code>] )
307 * <em>FloatingPoint</em>:
308 * ( { <em>Digit</em> }+ [ <code>.</code> { <em>Digit</em> } ]
309 * [ <em>Exponent</em> ] )
310 * | ( <code>.</code> { <em>Digit</em> }+ [ <em>Exponent</em> ] )
311 * <em>Exponent</em>:
312 * ( ( <code>e</code> | <code>E</code> )
313 * [ <code>-</code> | <code>+</code> ] { <em>Digit</em> }+ )
314 * <em>Digit</em>: <em><code>'0'</code> through <code>'9'</code></em>
315 * </pre>
317 * <p>NaN and infinity are special cases, to allow parsing of the output
318 * of toString. Otherwise, the result is determined by calculating
319 * <em>n * 10<sup>exponent</sup></em> to infinite precision, then rounding
320 * to the nearest double. Remember that many numbers cannot be precisely
321 * represented in floating point. In case of overflow, infinity is used,
322 * and in case of underflow, signed zero is used. Unlike Integer.parseInt,
323 * this does not accept Unicode digits outside the ASCII range.
325 * <p>If an unexpected character is found in the <code>String</code>, a
326 * <code>NumberFormatException</code> will be thrown. Leading and trailing
327 * 'whitespace' is ignored via <code>String.trim()</code>, but spaces
328 * internal to the actual number are not allowed.
330 * <p>To parse numbers according to another format, consider using
331 * {@link java.text.NumberFormat}.
333 * @XXX specify where/how we are not in accord with the spec.
335 * @param str the <code>String</code> to convert
336 * @return the <code>double</code> value of <code>s</code>
337 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
338 * <code>double</code>
339 * @throws NullPointerException if <code>s</code> is null
340 * @see #MIN_VALUE
341 * @see #MAX_VALUE
342 * @see #POSITIVE_INFINITY
343 * @see #NEGATIVE_INFINITY
344 * @since 1.2
346 public static double parseDouble(String str)
348 return VMDouble.parseDouble(str);
352 * Return <code>true</code> if the <code>double</code> has the same
353 * value as <code>NaN</code>, otherwise return <code>false</code>.
355 * @param v the <code>double</code> to compare
356 * @return whether the argument is <code>NaN</code>.
358 public static boolean isNaN(double v)
360 // This works since NaN != NaN is the only reflexive inequality
361 // comparison which returns true.
362 return v != v;
366 * Return <code>true</code> if the <code>double</code> has a value
367 * equal to either <code>NEGATIVE_INFINITY</code> or
368 * <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
370 * @param v the <code>double</code> to compare
371 * @return whether the argument is (-/+) infinity.
373 public static boolean isInfinite(double v)
375 return v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY;
379 * Return <code>true</code> if the value of this <code>Double</code>
380 * is the same as <code>NaN</code>, otherwise return <code>false</code>.
382 * @return whether this <code>Double</code> is <code>NaN</code>
384 public boolean isNaN()
386 return isNaN(value);
390 * Return <code>true</code> if the value of this <code>Double</code>
391 * is the same as <code>NEGATIVE_INFINITY</code> or
392 * <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
394 * @return whether this <code>Double</code> is (-/+) infinity
396 public boolean isInfinite()
398 return isInfinite(value);
402 * Convert the <code>double</code> value of this <code>Double</code>
403 * to a <code>String</code>. This method calls
404 * <code>Double.toString(double)</code> to do its dirty work.
406 * @return the <code>String</code> representation
407 * @see #toString(double)
409 public String toString()
411 return toString(value);
415 * Return the value of this <code>Double</code> as a <code>byte</code>.
417 * @return the byte value
418 * @since 1.1
420 public byte byteValue()
422 return (byte) value;
426 * Return the value of this <code>Double</code> as a <code>short</code>.
428 * @return the short value
429 * @since 1.1
431 public short shortValue()
433 return (short) value;
437 * Return the value of this <code>Double</code> as an <code>int</code>.
439 * @return the int value
441 public int intValue()
443 return (int) value;
447 * Return the value of this <code>Double</code> as a <code>long</code>.
449 * @return the long value
451 public long longValue()
453 return (long) value;
457 * Return the value of this <code>Double</code> as a <code>float</code>.
459 * @return the float value
461 public float floatValue()
463 return (float) value;
467 * Return the value of this <code>Double</code>.
469 * @return the double value
471 public double doubleValue()
473 return value;
477 * Return a hashcode representing this Object. <code>Double</code>'s hash
478 * code is calculated by:<br>
479 * <code>long v = Double.doubleToLongBits(doubleValue());<br>
480 * int hash = (int)(v^(v&gt;&gt;32))</code>.
482 * @return this Object's hash code
483 * @see #doubleToLongBits(double)
485 public int hashCode()
487 long v = doubleToLongBits(value);
488 return (int) (v ^ (v >>> 32));
492 * Returns <code>true</code> if <code>obj</code> is an instance of
493 * <code>Double</code> and represents the same double value. Unlike comparing
494 * two doubles with <code>==</code>, this treats two instances of
495 * <code>Double.NaN</code> as equal, but treats <code>0.0</code> and
496 * <code>-0.0</code> as unequal.
498 * <p>Note that <code>d1.equals(d2)</code> is identical to
499 * <code>doubleToLongBits(d1.doubleValue()) ==
500 * doubleToLongBits(d2.doubleValue())</code>.
502 * @param obj the object to compare
503 * @return whether the objects are semantically equal
505 public boolean equals(Object obj)
507 if (obj instanceof Double)
509 double d = ((Double) obj).value;
510 return (doubleToRawLongBits(value) == doubleToRawLongBits(d)) ||
511 (isNaN(value) && isNaN(d));
513 return false;
517 * Convert the double to the IEEE 754 floating-point "double format" bit
518 * layout. Bit 63 (the most significant) is the sign bit, bits 62-52
519 * (masked by 0x7ff0000000000000L) represent the exponent, and bits 51-0
520 * (masked by 0x000fffffffffffffL) are the mantissa. This function
521 * collapses all versions of NaN to 0x7ff8000000000000L. The result of this
522 * function can be used as the argument to
523 * <code>Double.longBitsToDouble(long)</code> to obtain the original
524 * <code>double</code> value.
526 * @param value the <code>double</code> to convert
527 * @return the bits of the <code>double</code>
528 * @see #longBitsToDouble(long)
530 public static long doubleToLongBits(double value)
532 if (isNaN(value))
533 return 0x7ff8000000000000L;
534 else
535 return VMDouble.doubleToRawLongBits(value);
539 * Convert the double to the IEEE 754 floating-point "double format" bit
540 * layout. Bit 63 (the most significant) is the sign bit, bits 62-52
541 * (masked by 0x7ff0000000000000L) represent the exponent, and bits 51-0
542 * (masked by 0x000fffffffffffffL) are the mantissa. This function
543 * leaves NaN alone, rather than collapsing to a canonical value. The
544 * result of this function can be used as the argument to
545 * <code>Double.longBitsToDouble(long)</code> to obtain the original
546 * <code>double</code> value.
548 * @param value the <code>double</code> to convert
549 * @return the bits of the <code>double</code>
550 * @see #longBitsToDouble(long)
552 public static long doubleToRawLongBits(double value)
554 return VMDouble.doubleToRawLongBits(value);
558 * Convert the argument in IEEE 754 floating-point "double format" bit
559 * layout to the corresponding float. Bit 63 (the most significant) is the
560 * sign bit, bits 62-52 (masked by 0x7ff0000000000000L) represent the
561 * exponent, and bits 51-0 (masked by 0x000fffffffffffffL) are the mantissa.
562 * This function leaves NaN alone, so that you can recover the bit pattern
563 * with <code>Double.doubleToRawLongBits(double)</code>.
565 * @param bits the bits to convert
566 * @return the <code>double</code> represented by the bits
567 * @see #doubleToLongBits(double)
568 * @see #doubleToRawLongBits(double)
570 public static double longBitsToDouble(long bits)
572 return VMDouble.longBitsToDouble(bits);
576 * Compare two Doubles numerically by comparing their <code>double</code>
577 * values. The result is positive if the first is greater, negative if the
578 * second is greater, and 0 if the two are equal. However, this special
579 * cases NaN and signed zero as follows: NaN is considered greater than
580 * all other doubles, including <code>POSITIVE_INFINITY</code>, and positive
581 * zero is considered greater than negative zero.
583 * @param d the Double to compare
584 * @return the comparison
585 * @since 1.2
587 public int compareTo(Double d)
589 return compare(value, d.value);
593 * Behaves like <code>new Double(x).compareTo(new Double(y))</code>; in
594 * other words this compares two doubles, special casing NaN and zero,
595 * without the overhead of objects.
597 * @param x the first double to compare
598 * @param y the second double to compare
599 * @return the comparison
600 * @since 1.4
602 public static int compare(double x, double y)
604 // handle the easy cases:
605 if (x < y)
606 return -1;
607 if (x > y)
608 return 1;
610 // handle equality respecting that 0.0 != -0.0 (hence not using x == y):
611 long lx = doubleToRawLongBits(x);
612 long ly = doubleToRawLongBits(y);
613 if (lx == ly)
614 return 0;
616 // handle NaNs:
617 if (x != x)
618 return (y != y) ? 0 : 1;
619 else if (y != y)
620 return -1;
622 // handle +/- 0.0
623 return (lx < ly) ? -1 : 1;