2 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
28 import org.apidesign.bck2brwsr.core.JavaScriptBody;
31 * The {@code Double} class wraps a value of the primitive type
32 * {@code double} in an object. An object of type
33 * {@code Double} contains a single field whose type is
36 * <p>In addition, this class provides several methods for converting a
37 * {@code double} to a {@code String} and a
38 * {@code String} to a {@code double}, as well as other
39 * constants and methods useful when dealing with a
43 * @author Arthur van Hoff
44 * @author Joseph D. Darcy
47 public final class Double extends Number implements Comparable<Double> {
49 * A constant holding the positive infinity of type
50 * {@code double}. It is equal to the value returned by
51 * {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
53 public static final double POSITIVE_INFINITY = 1.0 / 0.0;
56 * A constant holding the negative infinity of type
57 * {@code double}. It is equal to the value returned by
58 * {@code Double.longBitsToDouble(0xfff0000000000000L)}.
60 public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
63 * A constant holding a Not-a-Number (NaN) value of type
64 * {@code double}. It is equivalent to the value returned by
65 * {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
67 public static final double NaN = 0.0d / 0.0;
70 * A constant holding the largest positive finite value of type
72 * (2-2<sup>-52</sup>)·2<sup>1023</sup>. It is equal to
73 * the hexadecimal floating-point literal
74 * {@code 0x1.fffffffffffffP+1023} and also equal to
75 * {@code Double.longBitsToDouble(0x7fefffffffffffffL)}.
77 public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308
80 * A constant holding the smallest positive normal value of type
81 * {@code double}, 2<sup>-1022</sup>. It is equal to the
82 * hexadecimal floating-point literal {@code 0x1.0p-1022} and also
83 * equal to {@code Double.longBitsToDouble(0x0010000000000000L)}.
87 public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308
90 * A constant holding the smallest positive nonzero value of type
91 * {@code double}, 2<sup>-1074</sup>. It is equal to the
92 * hexadecimal floating-point literal
93 * {@code 0x0.0000000000001P-1022} and also equal to
94 * {@code Double.longBitsToDouble(0x1L)}.
96 public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324
99 * Maximum exponent a finite {@code double} variable may have.
100 * It is equal to the value returned by
101 * {@code Math.getExponent(Double.MAX_VALUE)}.
105 public static final int MAX_EXPONENT = 1023;
108 * Minimum exponent a normalized {@code double} variable may
109 * have. It is equal to the value returned by
110 * {@code Math.getExponent(Double.MIN_NORMAL)}.
114 public static final int MIN_EXPONENT = -1022;
117 * The number of bits used to represent a {@code double} value.
121 public static final int SIZE = 64;
124 * The {@code Class} instance representing the primitive type
129 public static final Class<Double> TYPE = (Class<Double>) Class.getPrimitiveClass("double");
132 * Returns a string representation of the {@code double}
133 * argument. All characters mentioned below are ASCII characters.
135 * <li>If the argument is NaN, the result is the string
137 * <li>Otherwise, the result is a string that represents the sign and
138 * magnitude (absolute value) of the argument. If the sign is negative,
139 * the first character of the result is '{@code -}'
140 * (<code>'\u002D'</code>); if the sign is positive, no sign character
141 * appears in the result. As for the magnitude <i>m</i>:
143 * <li>If <i>m</i> is infinity, it is represented by the characters
144 * {@code "Infinity"}; thus, positive infinity produces the result
145 * {@code "Infinity"} and negative infinity produces the result
146 * {@code "-Infinity"}.
148 * <li>If <i>m</i> is zero, it is represented by the characters
149 * {@code "0.0"}; thus, negative zero produces the result
150 * {@code "-0.0"} and positive zero produces the result
153 * <li>If <i>m</i> is greater than or equal to 10<sup>-3</sup> but less
154 * than 10<sup>7</sup>, then it is represented as the integer part of
155 * <i>m</i>, in decimal form with no leading zeroes, followed by
156 * '{@code .}' (<code>'\u002E'</code>), followed by one or
157 * more decimal digits representing the fractional part of <i>m</i>.
159 * <li>If <i>m</i> is less than 10<sup>-3</sup> or greater than or
160 * equal to 10<sup>7</sup>, then it is represented in so-called
161 * "computerized scientific notation." Let <i>n</i> be the unique
162 * integer such that 10<sup><i>n</i></sup> ≤ <i>m</i> {@literal <}
163 * 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
164 * mathematically exact quotient of <i>m</i> and
165 * 10<sup><i>n</i></sup> so that 1 ≤ <i>a</i> {@literal <} 10. The
166 * magnitude is then represented as the integer part of <i>a</i>,
167 * as a single decimal digit, followed by '{@code .}'
168 * (<code>'\u002E'</code>), followed by decimal digits
169 * representing the fractional part of <i>a</i>, followed by the
170 * letter '{@code E}' (<code>'\u0045'</code>), followed
171 * by a representation of <i>n</i> as a decimal integer, as
172 * produced by the method {@link Integer#toString(int)}.
175 * How many digits must be printed for the fractional part of
176 * <i>m</i> or <i>a</i>? There must be at least one digit to represent
177 * the fractional part, and beyond that as many, but only as many, more
178 * digits as are needed to uniquely distinguish the argument value from
179 * adjacent values of type {@code double}. That is, suppose that
180 * <i>x</i> is the exact mathematical value represented by the decimal
181 * representation produced by this method for a finite nonzero argument
182 * <i>d</i>. Then <i>d</i> must be the {@code double} value nearest
183 * to <i>x</i>; or if two {@code double} values are equally close
184 * to <i>x</i>, then <i>d</i> must be one of them and the least
185 * significant bit of the significand of <i>d</i> must be {@code 0}.
187 * <p>To create localized string representations of a floating-point
188 * value, use subclasses of {@link java.text.NumberFormat}.
190 * @param d the {@code double} to be converted.
191 * @return a string representation of the argument.
193 @JavaScriptBody(args="d", body="var f = Math.floor(d);\n" +
194 "var r = d.toString();" +
195 "if (f === d && isFinite(d) && r.indexOf('e') === -1) return r + '.0';\n" +
198 public static native String toString(double d);
201 * Returns a hexadecimal string representation of the
202 * {@code double} argument. All characters mentioned below
203 * are ASCII characters.
206 * <li>If the argument is NaN, the result is the string
208 * <li>Otherwise, the result is a string that represents the sign
209 * and magnitude of the argument. If the sign is negative, the
210 * first character of the result is '{@code -}'
211 * (<code>'\u002D'</code>); if the sign is positive, no sign
212 * character appears in the result. As for the magnitude <i>m</i>:
215 * <li>If <i>m</i> is infinity, it is represented by the string
216 * {@code "Infinity"}; thus, positive infinity produces the
217 * result {@code "Infinity"} and negative infinity produces
218 * the result {@code "-Infinity"}.
220 * <li>If <i>m</i> is zero, it is represented by the string
221 * {@code "0x0.0p0"}; thus, negative zero produces the result
222 * {@code "-0x0.0p0"} and positive zero produces the result
225 * <li>If <i>m</i> is a {@code double} value with a
226 * normalized representation, substrings are used to represent the
227 * significand and exponent fields. The significand is
228 * represented by the characters {@code "0x1."}
229 * followed by a lowercase hexadecimal representation of the rest
230 * of the significand as a fraction. Trailing zeros in the
231 * hexadecimal representation are removed unless all the digits
232 * are zero, in which case a single zero is used. Next, the
233 * exponent is represented by {@code "p"} followed
234 * by a decimal string of the unbiased exponent as if produced by
235 * a call to {@link Integer#toString(int) Integer.toString} on the
238 * <li>If <i>m</i> is a {@code double} value with a subnormal
239 * representation, the significand is represented by the
240 * characters {@code "0x0."} followed by a
241 * hexadecimal representation of the rest of the significand as a
242 * fraction. Trailing zeros in the hexadecimal representation are
243 * removed. Next, the exponent is represented by
244 * {@code "p-1022"}. Note that there must be at
245 * least one nonzero digit in a subnormal significand.
252 * <caption><h3>Examples</h3></caption>
253 * <tr><th>Floating-point Value</th><th>Hexadecimal String</th>
254 * <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td>
255 * <tr><td>{@code -1.0}</td> <td>{@code -0x1.0p0}</td>
256 * <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>
257 * <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>
258 * <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>
259 * <tr><td>{@code 0.25}</td> <td>{@code 0x1.0p-2}</td>
260 * <tr><td>{@code Double.MAX_VALUE}</td>
261 * <td>{@code 0x1.fffffffffffffp1023}</td>
262 * <tr><td>{@code Minimum Normal Value}</td>
263 * <td>{@code 0x1.0p-1022}</td>
264 * <tr><td>{@code Maximum Subnormal Value}</td>
265 * <td>{@code 0x0.fffffffffffffp-1022}</td>
266 * <tr><td>{@code Double.MIN_VALUE}</td>
267 * <td>{@code 0x0.0000000000001p-1022}</td>
269 * @param d the {@code double} to be converted.
270 * @return a hex string representation of the argument.
272 * @author Joseph D. Darcy
274 public static String toHexString(double d) {
275 throw new UnsupportedOperationException();
277 // * Modeled after the "a" conversion specifier in C99, section
278 // * 7.19.6.1; however, the output of this method is more
279 // * tightly specified.
281 // if (!FpUtils.isFinite(d) )
282 // // For infinity and NaN, use the decimal output.
283 // return Double.toString(d);
285 // // Initialized to maximum size of output.
286 // StringBuffer answer = new StringBuffer(24);
288 // if (FpUtils.rawCopySign(1.0, d) == -1.0) // value is negative,
289 // answer.append("-"); // so append sign info
291 // answer.append("0x");
296 // answer.append("0.0p0");
299 // boolean subnormal = (d < DoubleConsts.MIN_NORMAL);
301 // // Isolate significand bits and OR in a high-order bit
302 // // so that the string representation has a known
304 // long signifBits = (Double.doubleToLongBits(d)
305 // & DoubleConsts.SIGNIF_BIT_MASK) |
306 // 0x1000000000000000L;
308 // // Subnormal values have a 0 implicit bit; normal
309 // // values have a 1 implicit bit.
310 // answer.append(subnormal ? "0." : "1.");
312 // // Isolate the low-order 13 digits of the hex
313 // // representation. If all the digits are zero,
314 // // replace with a single 0; otherwise, remove all
315 // // trailing zeros.
316 // String signif = Long.toHexString(signifBits).substring(3,16);
317 // answer.append(signif.equals("0000000000000") ? // 13 zeros
319 // signif.replaceFirst("0{1,12}$", ""));
321 // // If the value is subnormal, use the E_min exponent
322 // // value for double; otherwise, extract and report d's
323 // // exponent (the representation of a subnormal uses
325 // answer.append("p" + (subnormal ?
326 // DoubleConsts.MIN_EXPONENT:
327 // FpUtils.getExponent(d) ));
329 // return answer.toString();
334 * Returns a {@code Double} object holding the
335 * {@code double} value represented by the argument string
338 * <p>If {@code s} is {@code null}, then a
339 * {@code NullPointerException} is thrown.
341 * <p>Leading and trailing whitespace characters in {@code s}
342 * are ignored. Whitespace is removed as if by the {@link
343 * String#trim} method; that is, both ASCII space and control
344 * characters are removed. The rest of {@code s} should
345 * constitute a <i>FloatValue</i> as described by the lexical
350 * <dt><i>FloatValue:</i>
351 * <dd><i>Sign<sub>opt</sub></i> {@code NaN}
352 * <dd><i>Sign<sub>opt</sub></i> {@code Infinity}
353 * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
354 * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
355 * <dd><i>SignedInteger</i>
361 * <dt><i>HexFloatingPointLiteral</i>:
362 * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
368 * <dt><i>HexSignificand:</i>
369 * <dd><i>HexNumeral</i>
370 * <dd><i>HexNumeral</i> {@code .}
371 * <dd>{@code 0x} <i>HexDigits<sub>opt</sub>
372 * </i>{@code .}<i> HexDigits</i>
373 * <dd>{@code 0X}<i> HexDigits<sub>opt</sub>
374 * </i>{@code .} <i>HexDigits</i>
380 * <dt><i>BinaryExponent:</i>
381 * <dd><i>BinaryExponentIndicator SignedInteger</i>
387 * <dt><i>BinaryExponentIndicator:</i>
394 * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
395 * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
396 * <i>FloatTypeSuffix</i> are as defined in the lexical structure
398 * <cite>The Java™ Language Specification</cite>,
399 * except that underscores are not accepted between digits.
400 * If {@code s} does not have the form of
401 * a <i>FloatValue</i>, then a {@code NumberFormatException}
402 * is thrown. Otherwise, {@code s} is regarded as
403 * representing an exact decimal value in the usual
404 * "computerized scientific notation" or as an exact
405 * hexadecimal value; this exact numerical value is then
406 * conceptually converted to an "infinitely precise"
407 * binary value that is then rounded to type {@code double}
408 * by the usual round-to-nearest rule of IEEE 754 floating-point
409 * arithmetic, which includes preserving the sign of a zero
412 * Note that the round-to-nearest rule also implies overflow and
413 * underflow behaviour; if the exact value of {@code s} is large
414 * enough in magnitude (greater than or equal to ({@link
415 * #MAX_VALUE} + {@link Math#ulp(double) ulp(MAX_VALUE)}/2),
416 * rounding to {@code double} will result in an infinity and if the
417 * exact value of {@code s} is small enough in magnitude (less
418 * than or equal to {@link #MIN_VALUE}/2), rounding to float will
421 * Finally, after rounding a {@code Double} object representing
422 * this {@code double} value is returned.
424 * <p> To interpret localized string representations of a
425 * floating-point value, use subclasses of {@link
426 * java.text.NumberFormat}.
428 * <p>Note that trailing format specifiers, specifiers that
429 * determine the type of a floating-point literal
430 * ({@code 1.0f} is a {@code float} value;
431 * {@code 1.0d} is a {@code double} value), do
432 * <em>not</em> influence the results of this method. In other
433 * words, the numerical value of the input string is converted
434 * directly to the target floating-point type. The two-step
435 * sequence of conversions, string to {@code float} followed
436 * by {@code float} to {@code double}, is <em>not</em>
437 * equivalent to converting a string directly to
438 * {@code double}. For example, the {@code float}
439 * literal {@code 0.1f} is equal to the {@code double}
440 * value {@code 0.10000000149011612}; the {@code float}
441 * literal {@code 0.1f} represents a different numerical
442 * value than the {@code double} literal
443 * {@code 0.1}. (The numerical value 0.1 cannot be exactly
444 * represented in a binary floating-point number.)
446 * <p>To avoid calling this method on an invalid string and having
447 * a {@code NumberFormatException} be thrown, the regular
448 * expression below can be used to screen the input string:
452 * final String Digits = "(\\p{Digit}+)";
453 * final String HexDigits = "(\\p{XDigit}+)";
454 * // an exponent is 'e' or 'E' followed by an optionally
455 * // signed decimal integer.
456 * final String Exp = "[eE][+-]?"+Digits;
457 * final String fpRegex =
458 * ("[\\x00-\\x20]*"+ // Optional leading "whitespace"
459 * "[+-]?(" + // Optional sign character
460 * "NaN|" + // "NaN" string
461 * "Infinity|" + // "Infinity" string
463 * // A decimal floating-point string representing a finite positive
464 * // number without a leading sign has at most five basic pieces:
465 * // Digits . Digits ExponentPart FloatTypeSuffix
467 * // Since this method allows integer-only strings as input
468 * // in addition to strings of floating-point literals, the
469 * // two sub-patterns below are simplifications of the grammar
470 * // productions from section 3.10.2 of
471 * // <cite>The Java™ Language Specification</cite>.
473 * // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
474 * "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
476 * // . Digits ExponentPart_opt FloatTypeSuffix_opt
477 * "(\\.("+Digits+")("+Exp+")?)|"+
479 * // Hexadecimal strings
481 * // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
482 * "(0[xX]" + HexDigits + "(\\.)?)|" +
484 * // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
485 * "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
487 * ")[pP][+-]?" + Digits + "))" +
489 * "[\\x00-\\x20]*");// Optional trailing "whitespace"
491 * if (Pattern.matches(fpRegex, myString))
492 * Double.valueOf(myString); // Will not throw NumberFormatException
494 * // Perform suitable alternative action
499 * @param s the string to be parsed.
500 * @return a {@code Double} object holding the value
501 * represented by the {@code String} argument.
502 * @throws NumberFormatException if the string does not contain a
505 public static Double valueOf(String s) throws NumberFormatException {
506 return new Double(parseDouble(s));
510 * Returns a {@code Double} instance representing the specified
511 * {@code double} value.
512 * If a new {@code Double} instance is not required, this method
513 * should generally be used in preference to the constructor
514 * {@link #Double(double)}, as this method is likely to yield
515 * significantly better space and time performance by caching
516 * frequently requested values.
518 * @param d a double value.
519 * @return a {@code Double} instance representing {@code d}.
522 public static Double valueOf(double d) {
523 return new Double(d);
527 * Returns a new {@code double} initialized to the value
528 * represented by the specified {@code String}, as performed
529 * by the {@code valueOf} method of class
532 * @param s the string to be parsed.
533 * @return the {@code double} value represented by the string
535 * @throws NullPointerException if the string is null
536 * @throws NumberFormatException if the string does not contain
537 * a parsable {@code double}.
538 * @see java.lang.Double#valueOf(String)
541 @JavaScriptBody(args="s", body="return parseFloat(s);")
542 public static double parseDouble(String s) throws NumberFormatException {
547 * Returns {@code true} if the specified number is a
548 * Not-a-Number (NaN) value, {@code false} otherwise.
550 * @param v the value to be tested.
551 * @return {@code true} if the value of the argument is NaN;
552 * {@code false} otherwise.
554 @JavaScriptBody(args = { "v" }, body = "return isNaN(v);")
555 static native public boolean isNaN(double v);
558 * Returns {@code true} if the specified number is infinitely
559 * large in magnitude, {@code false} otherwise.
561 * @param v the value to be tested.
562 * @return {@code true} if the value of the argument is positive
563 * infinity or negative infinity; {@code false} otherwise.
565 static public boolean isInfinite(double v) {
566 return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
570 * The value of the Double.
574 private final double value;
577 * Constructs a newly allocated {@code Double} object that
578 * represents the primitive {@code double} argument.
580 * @param value the value to be represented by the {@code Double}.
582 public Double(double value) {
587 * Constructs a newly allocated {@code Double} object that
588 * represents the floating-point value of type {@code double}
589 * represented by the string. The string is converted to a
590 * {@code double} value as if by the {@code valueOf} method.
592 * @param s a string to be converted to a {@code Double}.
593 * @throws NumberFormatException if the string does not contain a
595 * @see java.lang.Double#valueOf(java.lang.String)
597 public Double(String s) throws NumberFormatException {
598 // REMIND: this is inefficient
599 this(valueOf(s).doubleValue());
603 * Returns {@code true} if this {@code Double} value is
604 * a Not-a-Number (NaN), {@code false} otherwise.
606 * @return {@code true} if the value represented by this object is
607 * NaN; {@code false} otherwise.
609 public boolean isNaN() {
614 * Returns {@code true} if this {@code Double} value is
615 * infinitely large in magnitude, {@code false} otherwise.
617 * @return {@code true} if the value represented by this object is
618 * positive infinity or negative infinity;
619 * {@code false} otherwise.
621 public boolean isInfinite() {
622 return isInfinite(value);
626 * Returns a string representation of this {@code Double} object.
627 * The primitive {@code double} value represented by this
628 * object is converted to a string exactly as if by the method
629 * {@code toString} of one argument.
631 * @return a {@code String} representation of this object.
632 * @see java.lang.Double#toString(double)
634 public String toString() {
635 return toString(value);
639 * Returns the value of this {@code Double} as a {@code byte} (by
640 * casting to a {@code byte}).
642 * @return the {@code double} value represented by this object
643 * converted to type {@code byte}
646 public byte byteValue() {
651 * Returns the value of this {@code Double} as a
652 * {@code short} (by casting to a {@code short}).
654 * @return the {@code double} value represented by this object
655 * converted to type {@code short}
658 public short shortValue() {
663 * Returns the value of this {@code Double} as an
664 * {@code int} (by casting to type {@code int}).
666 * @return the {@code double} value represented by this object
667 * converted to type {@code int}
669 public int intValue() {
674 * Returns the value of this {@code Double} as a
675 * {@code long} (by casting to type {@code long}).
677 * @return the {@code double} value represented by this object
678 * converted to type {@code long}
680 public long longValue() {
685 * Returns the {@code float} value of this
686 * {@code Double} object.
688 * @return the {@code double} value represented by this object
689 * converted to type {@code float}
692 public float floatValue() {
697 * Returns the {@code double} value of this
698 * {@code Double} object.
700 * @return the {@code double} value represented by this object
702 public double doubleValue() {
703 return (double)value;
707 * Returns a hash code for this {@code Double} object. The
708 * result is the exclusive OR of the two halves of the
709 * {@code long} integer bit representation, exactly as
710 * produced by the method {@link #doubleToLongBits(double)}, of
711 * the primitive {@code double} value represented by this
712 * {@code Double} object. That is, the hash code is the value
716 * {@code (int)(v^(v>>>32))}
719 * where {@code v} is defined by:
722 * {@code long v = Double.doubleToLongBits(this.doubleValue());}
725 * @return a {@code hash code} value for this object.
727 public int hashCode() {
728 long bits = doubleToLongBits(value);
729 return (int)(bits ^ (bits >>> 32));
733 * Compares this object against the specified object. The result
734 * is {@code true} if and only if the argument is not
735 * {@code null} and is a {@code Double} object that
736 * represents a {@code double} that has the same value as the
737 * {@code double} represented by this object. For this
738 * purpose, two {@code double} values are considered to be
739 * the same if and only if the method {@link
740 * #doubleToLongBits(double)} returns the identical
741 * {@code long} value when applied to each.
743 * <p>Note that in most cases, for two instances of class
744 * {@code Double}, {@code d1} and {@code d2}, the
745 * value of {@code d1.equals(d2)} is {@code true} if and
749 * {@code d1.doubleValue() == d2.doubleValue()}
752 * <p>also has the value {@code true}. However, there are two
755 * <li>If {@code d1} and {@code d2} both represent
756 * {@code Double.NaN}, then the {@code equals} method
757 * returns {@code true}, even though
758 * {@code Double.NaN==Double.NaN} has the value
760 * <li>If {@code d1} represents {@code +0.0} while
761 * {@code d2} represents {@code -0.0}, or vice versa,
762 * the {@code equal} test has the value {@code false},
763 * even though {@code +0.0==-0.0} has the value {@code true}.
765 * This definition allows hash tables to operate properly.
766 * @param obj the object to compare with.
767 * @return {@code true} if the objects are the same;
768 * {@code false} otherwise.
769 * @see java.lang.Double#doubleToLongBits(double)
771 public boolean equals(Object obj) {
772 return (obj instanceof Double)
773 && (((Double)obj).value) == value;
777 * Returns a representation of the specified floating-point value
778 * according to the IEEE 754 floating-point "double
779 * format" bit layout.
781 * <p>Bit 63 (the bit that is selected by the mask
782 * {@code 0x8000000000000000L}) represents the sign of the
783 * floating-point number. Bits
784 * 62-52 (the bits that are selected by the mask
785 * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
786 * (the bits that are selected by the mask
787 * {@code 0x000fffffffffffffL}) represent the significand
788 * (sometimes called the mantissa) of the floating-point number.
790 * <p>If the argument is positive infinity, the result is
791 * {@code 0x7ff0000000000000L}.
793 * <p>If the argument is negative infinity, the result is
794 * {@code 0xfff0000000000000L}.
796 * <p>If the argument is NaN, the result is
797 * {@code 0x7ff8000000000000L}.
799 * <p>In all cases, the result is a {@code long} integer that, when
800 * given to the {@link #longBitsToDouble(long)} method, will produce a
801 * floating-point value the same as the argument to
802 * {@code doubleToLongBits} (except all NaN values are
803 * collapsed to a single "canonical" NaN value).
805 * @param value a {@code double} precision floating-point number.
806 * @return the bits that represent the floating-point number.
808 public static long doubleToLongBits(double value) {
809 final long EXP_BIT_MASK = 9218868437227405312L;
810 final long SIGNIF_BIT_MASK = 4503599627370495L;
812 long result = doubleToRawLongBits(value);
813 // Check for NaN based on values of bit fields, maximum
814 // exponent and nonzero significand.
815 if ( ((result & EXP_BIT_MASK) ==
817 (result & SIGNIF_BIT_MASK) != 0L)
818 result = 0x7ff8000000000000L;
823 * Returns a representation of the specified floating-point value
824 * according to the IEEE 754 floating-point "double
825 * format" bit layout, preserving Not-a-Number (NaN) values.
827 * <p>Bit 63 (the bit that is selected by the mask
828 * {@code 0x8000000000000000L}) represents the sign of the
829 * floating-point number. Bits
830 * 62-52 (the bits that are selected by the mask
831 * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
832 * (the bits that are selected by the mask
833 * {@code 0x000fffffffffffffL}) represent the significand
834 * (sometimes called the mantissa) of the floating-point number.
836 * <p>If the argument is positive infinity, the result is
837 * {@code 0x7ff0000000000000L}.
839 * <p>If the argument is negative infinity, the result is
840 * {@code 0xfff0000000000000L}.
842 * <p>If the argument is NaN, the result is the {@code long}
843 * integer representing the actual NaN value. Unlike the
844 * {@code doubleToLongBits} method,
845 * {@code doubleToRawLongBits} does not collapse all the bit
846 * patterns encoding a NaN to a single "canonical" NaN
849 * <p>In all cases, the result is a {@code long} integer that,
850 * when given to the {@link #longBitsToDouble(long)} method, will
851 * produce a floating-point value the same as the argument to
852 * {@code doubleToRawLongBits}.
854 * @param value a {@code double} precision floating-point number.
855 * @return the bits that represent the floating-point number.
858 public static long doubleToRawLongBits(double value) {
859 int[] arr = { 0, 0 };
860 doubleToRawLongBits(value, arr);
862 return (l << 32) | arr[0];
865 @JavaScriptBody(args = { "value", "arr" }, body = ""
866 + "var a = new ArrayBuffer(8);"
867 + "new Float64Array(a)[0] = value;"
868 + "var out = new Int32Array(a);"
872 private static native void doubleToRawLongBits(double value, int[] arr);
875 * Returns the {@code double} value corresponding to a given
876 * bit representation.
877 * The argument is considered to be a representation of a
878 * floating-point value according to the IEEE 754 floating-point
879 * "double format" bit layout.
881 * <p>If the argument is {@code 0x7ff0000000000000L}, the result
882 * is positive infinity.
884 * <p>If the argument is {@code 0xfff0000000000000L}, the result
885 * is negative infinity.
887 * <p>If the argument is any value in the range
888 * {@code 0x7ff0000000000001L} through
889 * {@code 0x7fffffffffffffffL} or in the range
890 * {@code 0xfff0000000000001L} through
891 * {@code 0xffffffffffffffffL}, the result is a NaN. No IEEE
892 * 754 floating-point operation provided by Java can distinguish
893 * between two NaN values of the same type with different bit
894 * patterns. Distinct values of NaN are only distinguishable by
895 * use of the {@code Double.doubleToRawLongBits} method.
897 * <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
898 * values that can be computed from the argument:
901 * int s = ((bits >> 63) == 0) ? 1 : -1;
902 * int e = (int)((bits >> 52) & 0x7ffL);
903 * long m = (e == 0) ?
904 * (bits & 0xfffffffffffffL) << 1 :
905 * (bits & 0xfffffffffffffL) | 0x10000000000000L;
906 * </pre></blockquote>
908 * Then the floating-point result equals the value of the mathematical
909 * expression <i>s</i>·<i>m</i>·2<sup><i>e</i>-1075</sup>.
911 * <p>Note that this method may not be able to return a
912 * {@code double} NaN with exactly same bit pattern as the
913 * {@code long} argument. IEEE 754 distinguishes between two
914 * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The
915 * differences between the two kinds of NaN are generally not
916 * visible in Java. Arithmetic operations on signaling NaNs turn
917 * them into quiet NaNs with a different, but often similar, bit
918 * pattern. However, on some processors merely copying a
919 * signaling NaN also performs that conversion. In particular,
920 * copying a signaling NaN to return it to the calling method
921 * may perform this conversion. So {@code longBitsToDouble}
922 * may not be able to return a {@code double} with a
923 * signaling NaN bit pattern. Consequently, for some
924 * {@code long} values,
925 * {@code doubleToRawLongBits(longBitsToDouble(start))} may
926 * <i>not</i> equal {@code start}. Moreover, which
927 * particular bit patterns represent signaling NaNs is platform
928 * dependent; although all NaN bit patterns, quiet or signaling,
929 * must be in the NaN range identified above.
931 * @param bits any {@code long} integer.
932 * @return the {@code double} floating-point value with the same
935 @JavaScriptBody(args={ "bits" },
937 "var hi = bits.high32();\n"
938 + "var s = (hi & 0x80000000) === 0 ? 1 : -1;\n"
939 + "var e = (hi >> 20) & 0x7ff;\n"
940 + "if (e === 0x7ff) {\n"
941 + " if ((bits == 0) && ((hi & 0xfffff) === 0)) {\n"
942 + " return (s > 0) ? Number.POSITIVE_INFINITY"
943 + " : Number.NEGATIVE_INFINITY;\n"
945 + " return Number.NaN;\n"
947 + "var m = (hi & 0xfffff).next32(bits);\n"
949 + " m = m.shl64(1);\n"
951 + " m.hi = m.high32() | 0x100000;\n"
953 + "return s * m.toFP() * Math.pow(2.0, e - 1075);\n"
955 public static native double longBitsToDouble(long bits);
958 * Compares two {@code Double} objects numerically. There
959 * are two ways in which comparisons performed by this method
960 * differ from those performed by the Java language numerical
961 * comparison operators ({@code <, <=, ==, >=, >})
962 * when applied to primitive {@code double} values:
964 * {@code Double.NaN} is considered by this method
965 * to be equal to itself and greater than all other
966 * {@code double} values (including
967 * {@code Double.POSITIVE_INFINITY}).
969 * {@code 0.0d} is considered by this method to be greater
970 * than {@code -0.0d}.
972 * This ensures that the <i>natural ordering</i> of
973 * {@code Double} objects imposed by this method is <i>consistent
976 * @param anotherDouble the {@code Double} to be compared.
977 * @return the value {@code 0} if {@code anotherDouble} is
978 * numerically equal to this {@code Double}; a value
979 * less than {@code 0} if this {@code Double}
980 * is numerically less than {@code anotherDouble};
981 * and a value greater than {@code 0} if this
982 * {@code Double} is numerically greater than
983 * {@code anotherDouble}.
987 public int compareTo(Double anotherDouble) {
988 return Double.compare(value, anotherDouble.value);
992 * Compares the two specified {@code double} values. The sign
993 * of the integer value returned is the same as that of the
994 * integer that would be returned by the call:
996 * new Double(d1).compareTo(new Double(d2))
999 * @param d1 the first {@code double} to compare
1000 * @param d2 the second {@code double} to compare
1001 * @return the value {@code 0} if {@code d1} is
1002 * numerically equal to {@code d2}; a value less than
1003 * {@code 0} if {@code d1} is numerically less than
1004 * {@code d2}; and a value greater than {@code 0}
1005 * if {@code d1} is numerically greater than
1009 public static int compare(double d1, double d2) {
1011 return -1; // Neither val is NaN, thisVal is smaller
1013 return 1; // Neither val is NaN, thisVal is larger
1015 // Cannot use doubleToRawLongBits because of possibility of NaNs.
1016 long thisBits = Double.doubleToLongBits(d1);
1017 long anotherBits = Double.doubleToLongBits(d2);
1019 return (thisBits == anotherBits ? 0 : // Values are equal
1020 (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1021 1)); // (0.0, -0.0) or (NaN, !NaN)
1024 /** use serialVersionUID from JDK 1.0.2 for interoperability */
1025 private static final long serialVersionUID = -9172774392245257468L;