KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > lang > Double


1 /*
2  * @(#)Double.java 1.94 04/05/11
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.lang;
9
10 import sun.misc.FloatingDecimal;
11 import sun.misc.FpUtils;
12 import sun.misc.DoubleConsts;
13
14 /**
15  * The <code>Double</code> class wraps a value of the primitive type
16  * <code>double</code> in an object. An object of type
17  * <code>Double</code> contains a single field whose type is
18  * <code>double</code>.
19  * <p>
20  * In addition, this class provides several methods for converting a
21  * <code>double</code> to a <code>String</code> and a
22  * <code>String</code> to a <code>double</code>, as well as other
23  * constants and methods useful when dealing with a
24  * <code>double</code>.
25  *
26  * @author Lee Boynton
27  * @author Arthur van Hoff
28  * @author Joseph D. Darcy
29  * @version 1.94, 05/11/04
30  * @since JDK1.0
31  */

32 public final class Double extends Number JavaDoc implements Comparable JavaDoc<Double JavaDoc> {
33     /**
34      * A constant holding the positive infinity of type
35      * <code>double</code>. It is equal to the value returned by
36      * <code>Double.longBitsToDouble(0x7ff0000000000000L)</code>.
37      */

38     public static final double POSITIVE_INFINITY = 1.0 / 0.0;
39
40     /**
41      * A constant holding the negative infinity of type
42      * <code>double</code>. It is equal to the value returned by
43      * <code>Double.longBitsToDouble(0xfff0000000000000L)</code>.
44      */

45     public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
46
47     /**
48      * A constant holding a Not-a-Number (NaN) value of type
49      * <code>double</code>. It is equivalent to the value returned by
50      * <code>Double.longBitsToDouble(0x7ff8000000000000L)</code>.
51      */

52     public static final double NaN = 0.0d / 0.0;
53
54     /**
55      * A constant holding the largest positive finite value of type
56      * <code>double</code>,
57      * (2-2<sup>-52</sup>)&middot;2<sup>1023</sup>. It is equal to
58      * the hexadecimal floating-point literal
59      * <code>0x1.fffffffffffffP+1023</code> and also equal to
60      * <code>Double.longBitsToDouble(0x7fefffffffffffffL)</code>.
61      */

62     public static final double MAX_VALUE = 1.7976931348623157e+308; // 0x1.fffffffffffffP+1023
63

64     /**
65      * A constant holding the smallest positive nonzero value of type
66      * <code>double</code>, 2<sup>-1074</sup>. It is equal to the
67      * hexadecimal floating-point literal
68      * <code>0x0.0000000000001P-1022</code> and also equal to
69      * <code>Double.longBitsToDouble(0x1L)</code>.
70      */

71     public static final double MIN_VALUE = 4.9e-324; // 0x0.0000000000001P-1022
72

73     /**
74      * The number of bits used to represent a <tt>double</tt> value.
75      *
76      * @since 1.5
77      */

78     public static final int SIZE = 64;
79
80     /**
81      * The <code>Class</code> instance representing the primitive type
82      * <code>double</code>.
83      *
84      * @since JDK1.1
85      */

86     public static final Class JavaDoc<Double JavaDoc> TYPE = (Class JavaDoc<Double JavaDoc>) Class.getPrimitiveClass("double");
87
88     /**
89      * Returns a string representation of the <code>double</code>
90      * argument. All characters mentioned below are ASCII characters.
91      * <ul>
92      * <li>If the argument is NaN, the result is the string
93      * &quot;<code>NaN</code>&quot;.
94      * <li>Otherwise, the result is a string that represents the sign and
95      * magnitude (absolute value) of the argument. If the sign is negative,
96      * the first character of the result is '<code>-</code>'
97      * (<code>'&#92;u002D'</code>); if the sign is positive, no sign character
98      * appears in the result. As for the magnitude <i>m</i>:
99      * <ul>
100      * <li>If <i>m</i> is infinity, it is represented by the characters
101      * <code>"Infinity"</code>; thus, positive infinity produces the result
102      * <code>"Infinity"</code> and negative infinity produces the result
103      * <code>"-Infinity"</code>.
104      *
105      * <li>If <i>m</i> is zero, it is represented by the characters
106      * <code>"0.0"</code>; thus, negative zero produces the result
107      * <code>"-0.0"</code> and positive zero produces the result
108      * <code>"0.0"</code>.
109      *
110      * <li>If <i>m</i> is greater than or equal to 10<sup>-3</sup> but less
111      * than 10<sup>7</sup>, then it is represented as the integer part of
112      * <i>m</i>, in decimal form with no leading zeroes, followed by
113      * '<code>.</code>' (<code>'&#92;u002E'</code>), followed by one or
114      * more decimal digits representing the fractional part of <i>m</i>.
115      *
116      * <li>If <i>m</i> is less than 10<sup>-3</sup> or greater than or
117      * equal to 10<sup>7</sup>, then it is represented in so-called
118      * "computerized scientific notation." Let <i>n</i> be the unique
119      * integer such that 10<sup><i>n</i></sup> &lt;= <i>m</i> &lt;
120      * 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
121      * mathematically exact quotient of <i>m</i> and
122      * 10<sup><i>n</i></sup> so that 1 &lt;= <i>a</i> &lt; 10. The
123      * magnitude is then represented as the integer part of <i>a</i>,
124      * as a single decimal digit, followed by '<code>.</code>'
125      * (<code>'&#92;u002E'</code>), followed by decimal digits
126      * representing the fractional part of <i>a</i>, followed by the
127      * letter '<code>E</code>' (<code>'&#92;u0045'</code>), followed
128      * by a representation of <i>n</i> as a decimal integer, as
129      * produced by the method {@link Integer#toString(int)}.
130      * </ul>
131      * </ul>
132      * How many digits must be printed for the fractional part of
133      * <i>m</i> or <i>a</i>? There must be at least one digit to represent
134      * the fractional part, and beyond that as many, but only as many, more
135      * digits as are needed to uniquely distinguish the argument value from
136      * adjacent values of type <code>double</code>. That is, suppose that
137      * <i>x</i> is the exact mathematical value represented by the decimal
138      * representation produced by this method for a finite nonzero argument
139      * <i>d</i>. Then <i>d</i> must be the <code>double</code> value nearest
140      * to <i>x</i>; or if two <code>double</code> values are equally close
141      * to <i>x</i>, then <i>d</i> must be one of them and the least
142      * significant bit of the significand of <i>d</i> must be <code>0</code>.
143      * <p>
144      * To create localized string representations of a floating-point
145      * value, use subclasses of {@link java.text.NumberFormat}.
146      *
147      * @param d the <code>double</code> to be converted.
148      * @return a string representation of the argument.
149      */

150     public static String JavaDoc toString(double d) {
151     return new FloatingDecimal(d).toJavaFormatString();
152     }
153
154     /**
155      * Returns a hexadecimal string representation of the
156      * <code>double</code> argument. All characters mentioned below
157      * are ASCII characters.
158      *
159      * <ul>
160      * <li>If the argument is NaN, the result is the string
161      * &quot;<code>NaN</code>&quot;.
162      * <li>Otherwise, the result is a string that represents the sign
163      * and magnitude of the argument. If the sign is negative, the
164      * first character of the result is '<code>-</code>'
165      * (<code>'&#92;u002D'</code>); if the sign is positive, no sign
166      * character appears in the result. As for the magnitude <i>m</i>:
167      *
168      * <ul>
169      * <li>If <i>m</i> is infinity, it is represented by the string
170      * <code>"Infinity"</code>; thus, positive infinity produces the
171      * result <code>"Infinity"</code> and negative infinity produces
172      * the result <code>"-Infinity"</code>.
173      *
174      * <li>If <i>m</i> is zero, it is represented by the string
175      * <code>"0x0.0p0"</code>; thus, negative zero produces the result
176      * <code>"-0x0.0p0"</code> and positive zero produces the result
177      * <code>"0x0.0p0"</code>.
178      *
179      * <li>If <i>m</i> is a <code>double</code> value with a
180      * normalized representation, substrings are used to represent the
181      * significand and exponent fields. The significand is
182      * represented by the characters <code>&quot;0x1.&quot;</code>
183      * followed by a lowercase hexadecimal representation of the rest
184      * of the significand as a fraction. Trailing zeros in the
185      * hexadecimal representation are removed unless all the digits
186      * are zero, in which case a single zero is used. Next, the
187      * exponent is represented by <code>&quot;p&quot;</code> followed
188      * by a decimal string of the unbiased exponent as if produced by
189      * a call to {@link Integer#toString(int) Integer.toString} on the
190      * exponent value.
191      *
192      * <li>If <i>m</i> is a <code>double</code> value with a subnormal
193      * representation, the significand is represented by the
194      * characters <code>&quot;0x0.&quot;</code> followed by a
195      * hexadecimal representation of the rest of the significand as a
196      * fraction. Trailing zeros in the hexadecimal representation are
197      * removed. Next, the exponent is represented by
198      * <code>&quot;p-1022&quot;</code>. Note that there must be at
199      * least one nonzero digit in a subnormal significand.
200      *
201      * </ul>
202      *
203      * </ul>
204      *
205      * <table border>
206      * <caption><h3>Examples</h3></caption>
207      * <tr><th>Floating-point Value</th><th>Hexadecimal String</th>
208      * <tr><td><code>1.0</code></td> <td><code>0x1.0p0</code></td>
209      * <tr><td><code>-1.0</code></td> <td><code>-0x1.0p0</code></td>
210      * <tr><td><code>2.0</code></td> <td><code>0x1.0p1</code></td>
211      * <tr><td><code>3.0</code></td> <td><code>0x1.8p1</code></td>
212      * <tr><td><code>0.5</code></td> <td><code>0x1.0p-1</code></td>
213      * <tr><td><code>0.25</code></td> <td><code>0x1.0p-2</code></td>
214      * <tr><td><code>Double.MAX_VALUE</code></td>
215      * <td><code>0x1.fffffffffffffp1023</code></td>
216      * <tr><td><code>Minimum Normal Value</code></td>
217      * <td><code>0x1.0p-1022</code></td>
218      * <tr><td><code>Maximum Subnormal Value</code></td>
219      * <td><code>0x0.fffffffffffffp-1022</code></td>
220      * <tr><td><code>Double.MIN_VALUE</code></td>
221      * <td><code>0x0.0000000000001p-1022</code></td>
222      * </table>
223      * @param d the <code>double</code> to be converted.
224      * @return a hex string representation of the argument.
225      * @since 1.5
226      * @author Joseph D. Darcy
227      */

228     public static String JavaDoc toHexString(double d) {
229     /*
230      * Modeled after the "a" conversion specifier in C99, section
231      * 7.19.6.1; however, the output of this method is more
232      * tightly specified.
233      */

234     if (!FpUtils.isFinite(d) )
235         // For infinity and NaN, use the decimal output.
236
return Double.toString(d);
237     else {
238         // Initialized to maximum size of output.
239
StringBuffer JavaDoc answer = new StringBuffer JavaDoc(24);
240         
241         if (FpUtils.rawCopySign(1.0, d) == -1.0) // value is negative,
242
answer.append("-"); // so append sign info
243

244         answer.append("0x");
245
246         d = Math.abs(d);
247
248         if(d == 0.0) {
249         answer.append("0.0p0");
250         }
251         else {
252         boolean subnormal = (d < DoubleConsts.MIN_NORMAL);
253
254         // Isolate significand bits and OR in a high-order bit
255
// so that the string representation has a known
256
// length.
257
long signifBits = (Double.doubleToLongBits(d)
258                    & DoubleConsts.SIGNIF_BIT_MASK) |
259             0x1000000000000000L;
260
261         // Subnormal values have a 0 implicit bit; normal
262
// values have a a 1 implicit bit.
263
answer.append(subnormal ? "0." : "1.");
264
265         // Isolate the low-order 13 digits of the hex
266
// representation. If all the digits are zero,
267
// replace with a single 0; otherwise, remove all
268
// trailing zeros.
269
String JavaDoc signif = Long.toHexString(signifBits).substring(3,16);
270         answer.append(signif.equals("0000000000000") ? // 13 zeros
271
"0":
272                   signif.replaceFirst("0{1,12}$", ""));
273
274         // If the value is subnormal, use the E_min exponent
275
// value for double; otherwise, extract and report d's
276
// exponent (the representation of a subnormal uses
277
// E_min -1).
278
answer.append("p" + (subnormal ?
279                    DoubleConsts.MIN_EXPONENT:
280                    FpUtils.getExponent(d) ));
281         }
282         return answer.toString();
283     }
284     }
285     
286     /**
287      * Returns a <code>Double</code> object holding the
288      * <code>double</code> value represented by the argument string
289      * <code>s</code>.
290      *
291      * <p>If <code>s</code> is <code>null</code>, then a
292      * <code>NullPointerException</code> is thrown.
293      *
294      * <p>Leading and trailing whitespace characters in <code>s</code>
295      * are ignored. Whitespace is removed as if by the {@link
296      * String#trim} method; that is, both ASCII space and control
297      * characters are removed. The rest of <code>s</code> should
298      * constitute a <i>FloatValue</i> as described by the lexical
299      * syntax rules:
300      *
301      * <blockquote>
302      * <dl>
303      * <dt><i>FloatValue:</i>
304      * <dd><i>Sign<sub>opt</sub></i> <code>NaN</code>
305      * <dd><i>Sign<sub>opt</sub></i> <code>Infinity</code>
306      * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
307      * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
308      * <dd><i>SignedInteger</i>
309      * </dl>
310      *
311      * <p>
312      *
313      * <dl>
314      * <dt><i>HexFloatingPointLiteral</i>:
315      * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
316      * </dl>
317      *
318      * <p>
319      *
320      * <dl>
321      * <dt><i>HexSignificand:</i>
322      * <dd><i>HexNumeral</i>
323      * <dd><i>HexNumeral</i> <code>.</code>
324      * <dd><code>0x</code> <i>HexDigits<sub>opt</sub>
325      * </i><code>.</code><i> HexDigits</i>
326      * <dd><code>0X</code><i> HexDigits<sub>opt</sub>
327      * </i><code>.</code> <i>HexDigits</i>
328      * </dl>
329      *
330      * <p>
331      *
332      * <dl>
333      * <dt><i>BinaryExponent:</i>
334      * <dd><i>BinaryExponentIndicator SignedInteger</i>
335      * </dl>
336      *
337      * <p>
338      *
339      * <dl>
340      * <dt><i>BinaryExponentIndicator:</i>
341      * <dd><code>p</code>
342      * <dd><code>P</code>
343      * </dl>
344      *
345      * </blockquote>
346      *
347      * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
348      * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
349      * <i>FloatTypeSuffix</i> are as defined in the lexical structure
350      * sections of the of the <a
351      * HREF="http://java.sun.com/docs/books/jls/html/">Java Language
352      * Specification</a>. If <code>s</code> does not have the form of
353      * a <i>FloatValue</i>, then a <code>NumberFormatException</code>
354      * is thrown. Otherwise, <code>s</code> is regarded as
355      * representing an exact decimal value in the usual
356      * &quot;computerized scientific notation&quot; or as an exact
357      * hexadecimal value; this exact numerical value is then
358      * conceptually converted to an &quot;infinitely precise&quot;
359      * binary value that is then rounded to type <code>double</code>
360      * by the usual round-to-nearest rule of IEEE 754 floating-point
361      * arithmetic, which includes preserving the sign of a zero
362      * value. Finally, a <code>Double</code> object representing this
363      * <code>double</code> value is returned.
364      *
365      * <p> To interpret localized string representations of a
366      * floating-point value, use subclasses of {@link
367      * java.text.NumberFormat}.
368      *
369      * <p>Note that trailing format specifiers, specifiers that
370      * determine the type of a floating-point literal
371      * (<code>1.0f</code> is a <code>float</code> value;
372      * <code>1.0d</code> is a <code>double</code> value), do
373      * <em>not</em> influence the results of this method. In other
374      * words, the numerical value of the input string is converted
375      * directly to the target floating-point type. The two-step
376      * sequence of conversions, string to <code>float</code> followed
377      * by <code>float</code> to <code>double</code>, is <em>not</em>
378      * equivalent to converting a string directly to
379      * <code>double</code>. For example, the <code>float</code>
380      * literal <code>0.1f</code> is equal to the <code>double</code>
381      * value <code>0.10000000149011612</code>; the <code>float</code>
382      * literal <code>0.1f</code> represents a different numerical
383      * value than the <code>double</code> literal
384      * <code>0.1</code>. (The numerical value 0.1 cannot be exactly
385      * represented in a binary floating-point number.)
386      *
387      * <p>To avoid calling this method on a invalid string and having
388      * a <code>NumberFormatException</code> be thrown, the regular
389      * expression below can be used to screen the input string:
390      *
391      * <code>
392      * <pre>
393      * final String Digits = "(\\p{Digit}+)";
394      * final String HexDigits = "(\\p{XDigit}+)";
395      * // an exponent is 'e' or 'E' followed by an optionally
396      * // signed decimal integer.
397      * final String Exp = "[eE][+-]?"+Digits;
398      * final String fpRegex =
399      * ("[\\x00-\\x20]*"+ // Optional leading &quot;whitespace&quot;
400      * "[+-]?(" + // Optional sign character
401      * "NaN|" + // "NaN" string
402      * "Infinity|" + // "Infinity" string
403      *
404      * // A decimal floating-point string representing a finite positive
405      * // number without a leading sign has at most five basic pieces:
406      * // Digits . Digits ExponentPart FloatTypeSuffix
407      * //
408      * // Since this method allows integer-only strings as input
409      * // in addition to strings of floating-point literals, the
410      * // two sub-patterns below are simplifications of the grammar
411      * // productions from the Java Language Specification, 2nd
412      * // edition, section 3.10.2.
413      *
414      * // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
415      * "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
416      *
417      * // . Digits ExponentPart_opt FloatTypeSuffix_opt
418      * "(\\.("+Digits+")("+Exp+")?)|"+
419      *
420      * // Hexadecimal strings
421      * "((" +
422      * // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
423      * "(0[xX]" + HexDigits + "(\\.)?)|" +
424      *
425      * // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
426      * "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
427      *
428      * ")[pP][+-]?" + Digits + "))" +
429      * "[fFdD]?))" +
430      * "[\\x00-\\x20]*");// Optional trailing &quot;whitespace&quot;
431      *
432      * if (Pattern.matches(fpRegex, myString))
433      * Double.valueOf(myString); // Will not throw NumberFormatException
434      * else {
435      * // Perform suitable alternative action
436      * }
437      * </pre>
438      * </code>
439      *
440      * @param s the string to be parsed.
441      * @return a <code>Double</code> object holding the value
442      * represented by the <code>String</code> argument.
443      * @exception NumberFormatException if the string does not contain a
444      * parsable number.
445      */

446     public static Double JavaDoc valueOf(String JavaDoc s) throws NumberFormatException JavaDoc {
447     return new Double JavaDoc(FloatingDecimal.readJavaFormatString(s).doubleValue());
448     }
449
450     /**
451      * Returns a <tt>Double</tt> instance representing the specified
452      * <tt>double</tt> value.
453      * If a new <tt>Double</tt> instance is not required, this method
454      * should generally be used in preference to the constructor
455      * {@link #Double(double)}, as this method is likely to yield
456      * significantly better space and time performance by caching
457      * frequently requested values.
458      *
459      * @param d a double value.
460      * @return a <tt>Double</tt> instance representing <tt>d</tt>.
461      * @since 1.5
462      */

463     public static Double JavaDoc valueOf(double d) {
464         return new Double JavaDoc(d);
465     }
466
467     /**
468      * Returns a new <code>double</code> initialized to the value
469      * represented by the specified <code>String</code>, as performed
470      * by the <code>valueOf</code> method of class
471      * <code>Double</code>.
472      *
473      * @param s the string to be parsed.
474      * @return the <code>double</code> value represented by the string
475      * argument.
476      * @exception NumberFormatException if the string does not contain
477      * a parsable <code>double</code>.
478      * @see java.lang.Double#valueOf(String)
479      * @since 1.2
480      */

481     public static double parseDouble(String JavaDoc s) throws NumberFormatException JavaDoc {
482     return FloatingDecimal.readJavaFormatString(s).doubleValue();
483     }
484
485     /**
486      * Returns <code>true</code> if the specified number is a
487      * Not-a-Number (NaN) value, <code>false</code> otherwise.
488      *
489      * @param v the value to be tested.
490      * @return <code>true</code> if the value of the argument is NaN;
491      * <code>false</code> otherwise.
492      */

493     static public boolean isNaN(double v) {
494     return (v != v);
495     }
496
497     /**
498      * Returns <code>true</code> if the specified number is infinitely
499      * large in magnitude, <code>false</code> otherwise.
500      *
501      * @param v the value to be tested.
502      * @return <code>true</code> if the value of the argument is positive
503      * infinity or negative infinity; <code>false</code> otherwise.
504      */

505     static public boolean isInfinite(double v) {
506     return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
507     }
508
509     /**
510      * The value of the Double.
511      *
512      * @serial
513      */

514     private final double value;
515
516     /**
517      * Constructs a newly allocated <code>Double</code> object that
518      * represents the primitive <code>double</code> argument.
519      *
520      * @param value the value to be represented by the <code>Double</code>.
521      */

522     public Double(double value) {
523     this.value = value;
524     }
525
526     /**
527      * Constructs a newly allocated <code>Double</code> object that
528      * represents the floating-point value of type <code>double</code>
529      * represented by the string. The string is converted to a
530      * <code>double</code> value as if by the <code>valueOf</code> method.
531      *
532      * @param s a string to be converted to a <code>Double</code>.
533      * @exception NumberFormatException if the string does not contain a
534      * parsable number.
535      * @see java.lang.Double#valueOf(java.lang.String)
536      */

537     public Double(String JavaDoc s) throws NumberFormatException JavaDoc {
538     // REMIND: this is inefficient
539
this(valueOf(s).doubleValue());
540     }
541
542     /**
543      * Returns <code>true</code> if this <code>Double</code> value is
544      * a Not-a-Number (NaN), <code>false</code> otherwise.
545      *
546      * @return <code>true</code> if the value represented by this object is
547      * NaN; <code>false</code> otherwise.
548      */

549     public boolean isNaN() {
550     return isNaN(value);
551     }
552
553     /**
554      * Returns <code>true</code> if this <code>Double</code> value is
555      * infinitely large in magnitude, <code>false</code> otherwise.
556      *
557      * @return <code>true</code> if the value represented by this object is
558      * positive infinity or negative infinity;
559      * <code>false</code> otherwise.
560      */

561     public boolean isInfinite() {
562     return isInfinite(value);
563     }
564
565     /**
566      * Returns a string representation of this <code>Double</code> object.
567      * The primitive <code>double</code> value represented by this
568      * object is converted to a string exactly as if by the method
569      * <code>toString</code> of one argument.
570      *
571      * @return a <code>String</code> representation of this object.
572      * @see java.lang.Double#toString(double)
573      */

574     public String JavaDoc toString() {
575     return String.valueOf(value);
576     }
577
578     /**
579      * Returns the value of this <code>Double</code> as a <code>byte</code> (by
580      * casting to a <code>byte</code>).
581      *
582      * @return the <code>double</code> value represented by this object
583      * converted to type <code>byte</code>
584      * @since JDK1.1
585      */

586     public byte byteValue() {
587     return (byte)value;
588     }
589
590     /**
591      * Returns the value of this <code>Double</code> as a
592      * <code>short</code> (by casting to a <code>short</code>).
593      *
594      * @return the <code>double</code> value represented by this object
595      * converted to type <code>short</code>
596      * @since JDK1.1
597      */

598     public short shortValue() {
599     return (short)value;
600     }
601
602     /**
603      * Returns the value of this <code>Double</code> as an
604      * <code>int</code> (by casting to type <code>int</code>).
605      *
606      * @return the <code>double</code> value represented by this object
607      * converted to type <code>int</code>
608      */

609     public int intValue() {
610     return (int)value;
611     }
612
613     /**
614      * Returns the value of this <code>Double</code> as a
615      * <code>long</code> (by casting to type <code>long</code>).
616      *
617      * @return the <code>double</code> value represented by this object
618      * converted to type <code>long</code>
619      */

620     public long longValue() {
621     return (long)value;
622     }
623
624     /**
625      * Returns the <code>float</code> value of this
626      * <code>Double</code> object.
627      *
628      * @return the <code>double</code> value represented by this object
629      * converted to type <code>float</code>
630      * @since JDK1.0
631      */

632     public float floatValue() {
633     return (float)value;
634     }
635
636     /**
637      * Returns the <code>double</code> value of this
638      * <code>Double</code> object.
639      *
640      * @return the <code>double</code> value represented by this object
641      */

642     public double doubleValue() {
643     return (double)value;
644     }
645
646     /**
647      * Returns a hash code for this <code>Double</code> object. The
648      * result is the exclusive OR of the two halves of the
649      * <code>long</code> integer bit representation, exactly as
650      * produced by the method {@link #doubleToLongBits(double)}, of
651      * the primitive <code>double</code> value represented by this
652      * <code>Double</code> object. That is, the hash code is the value
653      * of the expression:
654      * <blockquote><pre>
655      * (int)(v^(v&gt;&gt;&gt;32))
656      * </pre></blockquote>
657      * where <code>v</code> is defined by:
658      * <blockquote><pre>
659      * long v = Double.doubleToLongBits(this.doubleValue());
660      * </pre></blockquote>
661      *
662      * @return a <code>hash code</code> value for this object.
663      */

664     public int hashCode() {
665     long bits = doubleToLongBits(value);
666     return (int)(bits ^ (bits >>> 32));
667     }
668
669     /**
670      * Compares this object against the specified object. The result
671      * is <code>true</code> if and only if the argument is not
672      * <code>null</code> and is a <code>Double</code> object that
673      * represents a <code>double</code> that has the same value as the
674      * <code>double</code> represented by this object. For this
675      * purpose, two <code>double</code> values are considered to be
676      * the same if and only if the method {@link
677      * #doubleToLongBits(double)} returns the identical
678      * <code>long</code> value when applied to each.
679      * <p>
680      * Note that in most cases, for two instances of class
681      * <code>Double</code>, <code>d1</code> and <code>d2</code>, the
682      * value of <code>d1.equals(d2)</code> is <code>true</code> if and
683      * only if
684      * <blockquote><pre>
685      * d1.doubleValue()&nbsp;== d2.doubleValue()
686      * </pre></blockquote>
687      * <p>
688      * also has the value <code>true</code>. However, there are two
689      * exceptions:
690      * <ul>
691      * <li>If <code>d1</code> and <code>d2</code> both represent
692      * <code>Double.NaN</code>, then the <code>equals</code> method
693      * returns <code>true</code>, even though
694      * <code>Double.NaN==Double.NaN</code> has the value
695      * <code>false</code>.
696      * <li>If <code>d1</code> represents <code>+0.0</code> while
697      * <code>d2</code> represents <code>-0.0</code>, or vice versa,
698      * the <code>equal</code> test has the value <code>false</code>,
699      * even though <code>+0.0==-0.0</code> has the value <code>true</code>.
700      * </ul>
701      * This definition allows hash tables to operate properly.
702      * @param obj the object to compare with.
703      * @return <code>true</code> if the objects are the same;
704      * <code>false</code> otherwise.
705      * @see java.lang.Double#doubleToLongBits(double)
706      */

707     public boolean equals(Object JavaDoc obj) {
708     return (obj instanceof Double JavaDoc)
709            && (doubleToLongBits(((Double JavaDoc)obj).value) ==
710               doubleToLongBits(value));
711     }
712
713     /**
714      * Returns a representation of the specified floating-point value
715      * according to the IEEE 754 floating-point "double
716      * format" bit layout.
717      * <p>
718      * Bit 63 (the bit that is selected by the mask
719      * <code>0x8000000000000000L</code>) represents the sign of the
720      * floating-point number. Bits
721      * 62-52 (the bits that are selected by the mask
722      * <code>0x7ff0000000000000L</code>) represent the exponent. Bits 51-0
723      * (the bits that are selected by the mask
724      * <code>0x000fffffffffffffL</code>) represent the significand
725      * (sometimes called the mantissa) of the floating-point number.
726      * <p>
727      * If the argument is positive infinity, the result is
728      * <code>0x7ff0000000000000L</code>.
729      * <p>
730      * If the argument is negative infinity, the result is
731      * <code>0xfff0000000000000L</code>.
732      * <p>
733      * If the argument is NaN, the result is
734      * <code>0x7ff8000000000000L</code>.
735      * <p>
736      * In all cases, the result is a <code>long</code> integer that, when
737      * given to the {@link #longBitsToDouble(long)} method, will produce a
738      * floating-point value the same as the argument to
739      * <code>doubleToLongBits</code> (except all NaN values are
740      * collapsed to a single &quot;canonical&quot; NaN value).
741      *
742      * @param value a <code>double</code> precision floating-point number.
743      * @return the bits that represent the floating-point number.
744      */

745     public static native long doubleToLongBits(double value);
746
747     /**
748      * Returns a representation of the specified floating-point value
749      * according to the IEEE 754 floating-point "double
750      * format" bit layout, preserving Not-a-Number (NaN) values.
751      * <p>
752      * Bit 63 (the bit that is selected by the mask
753      * <code>0x8000000000000000L</code>) represents the sign of the
754      * floating-point number. Bits
755      * 62-52 (the bits that are selected by the mask
756      * <code>0x7ff0000000000000L</code>) represent the exponent. Bits 51-0
757      * (the bits that are selected by the mask
758      * <code>0x000fffffffffffffL</code>) represent the significand
759      * (sometimes called the mantissa) of the floating-point number.
760      * <p>
761      * If the argument is positive infinity, the result is
762      * <code>0x7ff0000000000000L</code>.
763      * <p>
764      * If the argument is negative infinity, the result is
765      * <code>0xfff0000000000000L</code>.
766      * <p>
767      * If the argument is NaN, the result is the <code>long</code>
768      * integer representing the actual NaN value. Unlike the
769      * <code>doubleToLongBits</code> method,
770      * <code>doubleToRawLongBits</code> does not collapse all the bit
771      * patterns encoding a NaN to a single &quot;canonical&quot; NaN
772      * value.
773      * <p>
774      * In all cases, the result is a <code>long</code> integer that,
775      * when given to the {@link #longBitsToDouble(long)} method, will
776      * produce a floating-point value the same as the argument to
777      * <code>doubleToRawLongBits</code>.
778      *
779      * @param value a <code>double</code> precision floating-point number.
780      * @return the bits that represent the floating-point number.
781      */

782     public static native long doubleToRawLongBits(double value);
783
784     /**
785      * Returns the <code>double</code> value corresponding to a given
786      * bit representation.
787      * The argument is considered to be a representation of a
788      * floating-point value according to the IEEE 754 floating-point
789      * "double format" bit layout.
790      * <p>
791      * If the argument is <code>0x7ff0000000000000L</code>, the result
792      * is positive infinity.
793      * <p>
794      * If the argument is <code>0xfff0000000000000L</code>, the result
795      * is negative infinity.
796      * <p>
797      * If the argument is any value in the range
798      * <code>0x7ff0000000000001L</code> through
799      * <code>0x7fffffffffffffffL</code> or in the range
800      * <code>0xfff0000000000001L</code> through
801      * <code>0xffffffffffffffffL</code>, the result is a NaN. No IEEE
802      * 754 floating-point operation provided by Java can distinguish
803      * between two NaN values of the same type with different bit
804      * patterns. Distinct values of NaN are only distinguishable by
805      * use of the <code>Double.doubleToRawLongBits</code> method.
806      * <p>
807      * In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
808      * values that can be computed from the argument:
809      * <blockquote><pre>
810      * int s = ((bits &gt;&gt; 63) == 0) ? 1 : -1;
811      * int e = (int)((bits &gt;&gt; 52) & 0x7ffL);
812      * long m = (e == 0) ?
813      * (bits & 0xfffffffffffffL) &lt;&lt; 1 :
814      * (bits & 0xfffffffffffffL) | 0x10000000000000L;
815      * </pre></blockquote>
816      * Then the floating-point result equals the value of the mathematical
817      * expression <i>s</i>&middot;<i>m</i>&middot;2<sup><i>e</i>-1075</sup>.
818      *<p>
819      * Note that this method may not be able to return a
820      * <code>double</code> NaN with exactly same bit pattern as the
821      * <code>long</code> argument. IEEE 754 distinguishes between two
822      * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The
823      * differences between the two kinds of NaN are generally not
824      * visible in Java. Arithmetic operations on signaling NaNs turn
825      * them into quiet NaNs with a different, but often similar, bit
826      * pattern. However, on some processors merely copying a
827      * signaling NaN also performs that conversion. In particular,
828      * copying a signaling NaN to return it to the calling method
829      * may perform this conversion. So <code>longBitsToDouble</code>
830      * may not be able to return a <code>double</code> with a
831      * signaling NaN bit pattern. Consequently, for some
832      * <code>long</code> values,
833      * <code>doubleToRawLongBits(longBitsToDouble(start))</code> may
834      * <i>not</i> equal <code>start</code>. Moreover, which
835      * particular bit patterns represent signaling NaNs is platform
836      * dependent; although all NaN bit patterns, quiet or signaling,
837      * must be in the NaN range identified above.
838      *
839      * @param bits any <code>long</code> integer.
840      * @return the <code>double</code> floating-point value with the same
841      * bit pattern.
842      */

843     public static native double longBitsToDouble(long bits);
844
845     /**
846      * Compares two <code>Double</code> objects numerically. There
847      * are two ways in which comparisons performed by this method
848      * differ from those performed by the Java language numerical
849      * comparison operators (<code>&lt;, &lt;=, ==, &gt;= &gt;</code>)
850      * when applied to primitive <code>double</code> values:
851      * <ul><li>
852      * <code>Double.NaN</code> is considered by this method
853      * to be equal to itself and greater than all other
854      * <code>double</code> values (including
855      * <code>Double.POSITIVE_INFINITY</code>).
856      * <li>
857      * <code>0.0d</code> is considered by this method to be greater
858      * than <code>-0.0d</code>.
859      * </ul>
860      * This ensures that the <i>natural ordering</i> of
861      * <tt>Double</tt> objects imposed by this method is <i>consistent
862      * with equals</i>.
863      *
864      * @param anotherDouble the <code>Double</code> to be compared.
865      * @return the value <code>0</code> if <code>anotherDouble</code> is
866      * numerically equal to this <code>Double</code>; a value
867      * less than <code>0</code> if this <code>Double</code>
868      * is numerically less than <code>anotherDouble</code>;
869      * and a value greater than <code>0</code> if this
870      * <code>Double</code> is numerically greater than
871      * <code>anotherDouble</code>.
872      *
873      * @since 1.2
874      */

875     public int compareTo(Double JavaDoc anotherDouble) {
876         return Double.compare(value, anotherDouble.value);
877     }
878
879     /**
880      * Compares the two specified <code>double</code> values. The sign
881      * of the integer value returned is the same as that of the
882      * integer that would be returned by the call:
883      * <pre>
884      * new Double(d1).compareTo(new Double(d2))
885      * </pre>
886      *
887      * @param d1 the first <code>double</code> to compare
888      * @param d2 the second <code>double</code> to compare
889      * @return the value <code>0</code> if <code>d1</code> is
890      * numerically equal to <code>d2</code>; a value less than
891      * <code>0</code> if <code>d1</code> is numerically less than
892      * <code>d2</code>; and a value greater than <code>0</code>
893      * if <code>d1</code> is numerically greater than
894      * <code>d2</code>.
895      * @since 1.4
896      */

897     public static int compare(double d1, double d2) {
898         if (d1 < d2)
899             return -1; // Neither val is NaN, thisVal is smaller
900
if (d1 > d2)
901             return 1; // Neither val is NaN, thisVal is larger
902

903         long thisBits = Double.doubleToLongBits(d1);
904         long anotherBits = Double.doubleToLongBits(d2);
905
906         return (thisBits == anotherBits ? 0 : // Values are equal
907
(thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
908
1)); // (0.0, -0.0) or (NaN, !NaN)
909
}
910
911     /** use serialVersionUID from JDK 1.0.2 for interoperability */
912     private static final long serialVersionUID = -9172774392245257468L;
913 }
914
Popular Tags