1 /*
   2  * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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.
  10  *
  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).
  16  *
  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.
  20  *
  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
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import jdk.internal.math.FloatingDecimal;
  29 import jdk.internal.math.DoubleConsts;
  30 import jdk.internal.HotSpotIntrinsicCandidate;
  31 
  32 /**
  33  * The {@code Double} class wraps a value of the primitive type
  34  * {@code double} in an object. An object of type
  35  * {@code Double} contains a single field whose type is
  36  * {@code double}.
  37  *
  38  * <p>In addition, this class provides several methods for converting a
  39  * {@code double} to a {@code String} and a
  40  * {@code String} to a {@code double}, as well as other
  41  * constants and methods useful when dealing with a
  42  * {@code double}.
  43  *
  44  * @author  Lee Boynton
  45  * @author  Arthur van Hoff
  46  * @author  Joseph D. Darcy
  47  * @since 1.0
  48  */
  49 public final class Double extends Number implements Comparable<Double> {
  50     /**
  51      * A constant holding the positive infinity of type
  52      * {@code double}. It is equal to the value returned by
  53      * {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
  54      */
  55     public static final double POSITIVE_INFINITY = 1.0 / 0.0;
  56 
  57     /**
  58      * A constant holding the negative infinity of type
  59      * {@code double}. It is equal to the value returned by
  60      * {@code Double.longBitsToDouble(0xfff0000000000000L)}.
  61      */
  62     public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
  63 
  64     /**
  65      * A constant holding a Not-a-Number (NaN) value of type
  66      * {@code double}. It is equivalent to the value returned by
  67      * {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
  68      */
  69     public static final double NaN = 0.0d / 0.0;
  70 
  71     /**
  72      * A constant holding the largest positive finite value of type
  73      * {@code double},
  74      * (2-2<sup>-52</sup>)&middot;2<sup>1023</sup>.  It is equal to
  75      * the hexadecimal floating-point literal
  76      * {@code 0x1.fffffffffffffP+1023} and also equal to
  77      * {@code Double.longBitsToDouble(0x7fefffffffffffffL)}.
  78      */
  79     public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308
  80 
  81     /**
  82      * A constant holding the smallest positive normal value of type
  83      * {@code double}, 2<sup>-1022</sup>.  It is equal to the
  84      * hexadecimal floating-point literal {@code 0x1.0p-1022} and also
  85      * equal to {@code Double.longBitsToDouble(0x0010000000000000L)}.
  86      *
  87      * @since 1.6
  88      */
  89     public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308
  90 
  91     /**
  92      * A constant holding the smallest positive nonzero value of type
  93      * {@code double}, 2<sup>-1074</sup>. It is equal to the
  94      * hexadecimal floating-point literal
  95      * {@code 0x0.0000000000001P-1022} and also equal to
  96      * {@code Double.longBitsToDouble(0x1L)}.
  97      */
  98     public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324
  99 
 100     /**
 101      * Maximum exponent a finite {@code double} variable may have.
 102      * It is equal to the value returned by
 103      * {@code Math.getExponent(Double.MAX_VALUE)}.
 104      *
 105      * @since 1.6
 106      */
 107     public static final int MAX_EXPONENT = 1023;
 108 
 109     /**
 110      * Minimum exponent a normalized {@code double} variable may
 111      * have.  It is equal to the value returned by
 112      * {@code Math.getExponent(Double.MIN_NORMAL)}.
 113      *
 114      * @since 1.6
 115      */
 116     public static final int MIN_EXPONENT = -1022;
 117 
 118     /**
 119      * The number of bits used to represent a {@code double} value.
 120      *
 121      * @since 1.5
 122      */
 123     public static final int SIZE = 64;
 124 
 125     /**
 126      * The number of bytes used to represent a {@code double} value.
 127      *
 128      * @since 1.8
 129      */
 130     public static final int BYTES = SIZE / Byte.SIZE;
 131 
 132     /**
 133      * The {@code Class} instance representing the primitive type
 134      * {@code double}.
 135      *
 136      * @since 1.1
 137      */
 138     @SuppressWarnings("unchecked")
 139     public static final Class<Double>   TYPE = (Class<Double>) Class.getPrimitiveClass("double");
 140 
 141     /**
 142      * Returns a string representation of the {@code double}
 143      * argument. All characters mentioned below are ASCII characters.
 144      * <ul>
 145      * <li>If the argument is NaN, the result is the string
 146      *     "{@code NaN}".
 147      * <li>Otherwise, the result is a string that represents the sign and
 148      * magnitude (absolute value) of the argument. If the sign is negative,
 149      * the first character of the result is '{@code -}'
 150      * ({@code '\u005Cu002D'}); if the sign is positive, no sign character
 151      * appears in the result. As for the magnitude <i>m</i>:
 152      * <ul>
 153      * <li>If <i>m</i> is infinity, it is represented by the characters
 154      * {@code "Infinity"}; thus, positive infinity produces the result
 155      * {@code "Infinity"} and negative infinity produces the result
 156      * {@code "-Infinity"}.
 157      *
 158      * <li>If <i>m</i> is zero, it is represented by the characters
 159      * {@code "0.0"}; thus, negative zero produces the result
 160      * {@code "-0.0"} and positive zero produces the result
 161      * {@code "0.0"}.
 162      *
 163      * <li>If <i>m</i> is greater than or equal to 10<sup>-3</sup> but less
 164      * than 10<sup>7</sup>, then it is represented as the integer part of
 165      * <i>m</i>, in decimal form with no leading zeroes, followed by
 166      * '{@code .}' ({@code '\u005Cu002E'}), followed by one or
 167      * more decimal digits representing the fractional part of <i>m</i>.
 168      *
 169      * <li>If <i>m</i> is less than 10<sup>-3</sup> or greater than or
 170      * equal to 10<sup>7</sup>, then it is represented in so-called
 171      * "computerized scientific notation." Let <i>n</i> be the unique
 172      * integer such that 10<sup><i>n</i></sup> &le; <i>m</i> {@literal <}
 173      * 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
 174      * mathematically exact quotient of <i>m</i> and
 175      * 10<sup><i>n</i></sup> so that 1 &le; <i>a</i> {@literal <} 10. The
 176      * magnitude is then represented as the integer part of <i>a</i>,
 177      * as a single decimal digit, followed by '{@code .}'
 178      * ({@code '\u005Cu002E'}), followed by decimal digits
 179      * representing the fractional part of <i>a</i>, followed by the
 180      * letter '{@code E}' ({@code '\u005Cu0045'}), followed
 181      * by a representation of <i>n</i> as a decimal integer, as
 182      * produced by the method {@link Integer#toString(int)}.
 183      * </ul>
 184      * </ul>
 185      * How many digits must be printed for the fractional part of
 186      * <i>m</i> or <i>a</i>? There must be at least one digit to represent
 187      * the fractional part, and beyond that as many, but only as many, more
 188      * digits as are needed to uniquely distinguish the argument value from
 189      * adjacent values of type {@code double}. That is, suppose that
 190      * <i>x</i> is the exact mathematical value represented by the decimal
 191      * representation produced by this method for a finite nonzero argument
 192      * <i>d</i>. Then <i>d</i> must be the {@code double} value nearest
 193      * to <i>x</i>; or if two {@code double} values are equally close
 194      * to <i>x</i>, then <i>d</i> must be one of them and the least
 195      * significant bit of the significand of <i>d</i> must be {@code 0}.
 196      *
 197      * <p>To create localized string representations of a floating-point
 198      * value, use subclasses of {@link java.text.NumberFormat}.
 199      *
 200      * @param   d   the {@code double} to be converted.
 201      * @return a string representation of the argument.
 202      */
 203     public static String toString(double d) {
 204         return FloatingDecimal.toJavaFormatString(d);
 205     }
 206 
 207     /**
 208      * Returns a hexadecimal string representation of the
 209      * {@code double} argument. All characters mentioned below
 210      * are ASCII characters.
 211      *
 212      * <ul>
 213      * <li>If the argument is NaN, the result is the string
 214      *     "{@code NaN}".
 215      * <li>Otherwise, the result is a string that represents the sign
 216      * and magnitude of the argument. If the sign is negative, the
 217      * first character of the result is '{@code -}'
 218      * ({@code '\u005Cu002D'}); if the sign is positive, no sign
 219      * character appears in the result. As for the magnitude <i>m</i>:
 220      *
 221      * <ul>
 222      * <li>If <i>m</i> is infinity, it is represented by the string
 223      * {@code "Infinity"}; thus, positive infinity produces the
 224      * result {@code "Infinity"} and negative infinity produces
 225      * the result {@code "-Infinity"}.
 226      *
 227      * <li>If <i>m</i> is zero, it is represented by the string
 228      * {@code "0x0.0p0"}; thus, negative zero produces the result
 229      * {@code "-0x0.0p0"} and positive zero produces the result
 230      * {@code "0x0.0p0"}.
 231      *
 232      * <li>If <i>m</i> is a {@code double} value with a
 233      * normalized representation, substrings are used to represent the
 234      * significand and exponent fields.  The significand is
 235      * represented by the characters {@code "0x1."}
 236      * followed by a lowercase hexadecimal representation of the rest
 237      * of the significand as a fraction.  Trailing zeros in the
 238      * hexadecimal representation are removed unless all the digits
 239      * are zero, in which case a single zero is used. Next, the
 240      * exponent is represented by {@code "p"} followed
 241      * by a decimal string of the unbiased exponent as if produced by
 242      * a call to {@link Integer#toString(int) Integer.toString} on the
 243      * exponent value.
 244      *
 245      * <li>If <i>m</i> is a {@code double} value with a subnormal
 246      * representation, the significand is represented by the
 247      * characters {@code "0x0."} followed by a
 248      * hexadecimal representation of the rest of the significand as a
 249      * fraction.  Trailing zeros in the hexadecimal representation are
 250      * removed. Next, the exponent is represented by
 251      * {@code "p-1022"}.  Note that there must be at
 252      * least one nonzero digit in a subnormal significand.
 253      *
 254      * </ul>
 255      *
 256      * </ul>
 257      *
 258      * <table class="striped">
 259      * <caption>Examples</caption>
 260      * <thead>
 261      * <tr><th scope="col">Floating-point Value</th><th scope="col">Hexadecimal String</th>
 262      * </thead>
 263      * <tbody style="text-align:right">
 264      * <tr><th scope="row">{@code 1.0}</th> <td>{@code 0x1.0p0}</td>
 265      * <tr><th scope="row">{@code -1.0}</th>        <td>{@code -0x1.0p0}</td>
 266      * <tr><th scope="row">{@code 2.0}</th> <td>{@code 0x1.0p1}</td>
 267      * <tr><th scope="row">{@code 3.0}</th> <td>{@code 0x1.8p1}</td>
 268      * <tr><th scope="row">{@code 0.5}</th> <td>{@code 0x1.0p-1}</td>
 269      * <tr><th scope="row">{@code 0.25}</th>        <td>{@code 0x1.0p-2}</td>
 270      * <tr><th scope="row">{@code Double.MAX_VALUE}</th>
 271      *     <td>{@code 0x1.fffffffffffffp1023}</td>
 272      * <tr><th scope="row">{@code Minimum Normal Value}</th>
 273      *     <td>{@code 0x1.0p-1022}</td>
 274      * <tr><th scope="row">{@code Maximum Subnormal Value}</th>
 275      *     <td>{@code 0x0.fffffffffffffp-1022}</td>
 276      * <tr><th scope="row">{@code Double.MIN_VALUE}</th>
 277      *     <td>{@code 0x0.0000000000001p-1022}</td>
 278      * </tbody>
 279      * </table>
 280      * @param   d   the {@code double} to be converted.
 281      * @return a hex string representation of the argument.
 282      * @since 1.5
 283      * @author Joseph D. Darcy
 284      */
 285     public static String toHexString(double d) {
 286         /*
 287          * Modeled after the "a" conversion specifier in C99, section
 288          * 7.19.6.1; however, the output of this method is more
 289          * tightly specified.
 290          */
 291         if (!isFinite(d) )
 292             // For infinity and NaN, use the decimal output.
 293             return Double.toString(d);
 294         else {
 295             // Initialized to maximum size of output.
 296             StringBuilder answer = new StringBuilder(24);
 297 
 298             if (Math.copySign(1.0, d) == -1.0)    // value is negative,
 299                 answer.append("-");                  // so append sign info
 300 
 301             answer.append("0x");
 302 
 303             d = Math.abs(d);
 304 
 305             if(d == 0.0) {
 306                 answer.append("0.0p0");
 307             } else {
 308                 boolean subnormal = (d < Double.MIN_NORMAL);
 309 
 310                 // Isolate significand bits and OR in a high-order bit
 311                 // so that the string representation has a known
 312                 // length.
 313                 long signifBits = (Double.doubleToLongBits(d)
 314                                    & DoubleConsts.SIGNIF_BIT_MASK) |
 315                     0x1000000000000000L;
 316 
 317                 // Subnormal values have a 0 implicit bit; normal
 318                 // values have a 1 implicit bit.
 319                 answer.append(subnormal ? "0." : "1.");
 320 
 321                 // Isolate the low-order 13 digits of the hex
 322                 // representation.  If all the digits are zero,
 323                 // replace with a single 0; otherwise, remove all
 324                 // trailing zeros.
 325                 String signif = Long.toHexString(signifBits).substring(3,16);
 326                 answer.append(signif.equals("0000000000000") ? // 13 zeros
 327                               "0":
 328                               signif.replaceFirst("0{1,12}$", ""));
 329 
 330                 answer.append('p');
 331                 // If the value is subnormal, use the E_min exponent
 332                 // value for double; otherwise, extract and report d's
 333                 // exponent (the representation of a subnormal uses
 334                 // E_min -1).
 335                 answer.append(subnormal ?
 336                               Double.MIN_EXPONENT:
 337                               Math.getExponent(d));
 338             }
 339             return answer.toString();
 340         }
 341     }
 342 
 343     /**
 344      * Returns a {@code Double} object holding the
 345      * {@code double} value represented by the argument string
 346      * {@code s}.
 347      *
 348      * <p>If {@code s} is {@code null}, then a
 349      * {@code NullPointerException} is thrown.
 350      *
 351      * <p>Leading and trailing whitespace characters in {@code s}
 352      * are ignored.  Whitespace is removed as if by the {@link
 353      * String#trim} method; that is, both ASCII space and control
 354      * characters are removed. The rest of {@code s} should
 355      * constitute a <i>FloatValue</i> as described by the lexical
 356      * syntax rules:
 357      *
 358      * <blockquote>
 359      * <dl>
 360      * <dt><i>FloatValue:</i>
 361      * <dd><i>Sign<sub>opt</sub></i> {@code NaN}
 362      * <dd><i>Sign<sub>opt</sub></i> {@code Infinity}
 363      * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
 364      * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
 365      * <dd><i>SignedInteger</i>
 366      * </dl>
 367      *
 368      * <dl>
 369      * <dt><i>HexFloatingPointLiteral</i>:
 370      * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
 371      * </dl>
 372      *
 373      * <dl>
 374      * <dt><i>HexSignificand:</i>
 375      * <dd><i>HexNumeral</i>
 376      * <dd><i>HexNumeral</i> {@code .}
 377      * <dd>{@code 0x} <i>HexDigits<sub>opt</sub>
 378      *     </i>{@code .}<i> HexDigits</i>
 379      * <dd>{@code 0X}<i> HexDigits<sub>opt</sub>
 380      *     </i>{@code .} <i>HexDigits</i>
 381      * </dl>
 382      *
 383      * <dl>
 384      * <dt><i>BinaryExponent:</i>
 385      * <dd><i>BinaryExponentIndicator SignedInteger</i>
 386      * </dl>
 387      *
 388      * <dl>
 389      * <dt><i>BinaryExponentIndicator:</i>
 390      * <dd>{@code p}
 391      * <dd>{@code P}
 392      * </dl>
 393      *
 394      * </blockquote>
 395      *
 396      * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
 397      * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
 398      * <i>FloatTypeSuffix</i> are as defined in the lexical structure
 399      * sections of
 400      * <cite>The Java&trade; Language Specification</cite>,
 401      * except that underscores are not accepted between digits.
 402      * If {@code s} does not have the form of
 403      * a <i>FloatValue</i>, then a {@code NumberFormatException}
 404      * is thrown. Otherwise, {@code s} is regarded as
 405      * representing an exact decimal value in the usual
 406      * "computerized scientific notation" or as an exact
 407      * hexadecimal value; this exact numerical value is then
 408      * conceptually converted to an "infinitely precise"
 409      * binary value that is then rounded to type {@code double}
 410      * by the usual round-to-nearest rule of IEEE 754 floating-point
 411      * arithmetic, which includes preserving the sign of a zero
 412      * value.
 413      *
 414      * Note that the round-to-nearest rule also implies overflow and
 415      * underflow behaviour; if the exact value of {@code s} is large
 416      * enough in magnitude (greater than or equal to ({@link
 417      * #MAX_VALUE} + {@link Math#ulp(double) ulp(MAX_VALUE)}/2),
 418      * rounding to {@code double} will result in an infinity and if the
 419      * exact value of {@code s} is small enough in magnitude (less
 420      * than or equal to {@link #MIN_VALUE}/2), rounding to float will
 421      * result in a zero.
 422      *
 423      * Finally, after rounding a {@code Double} object representing
 424      * this {@code double} value is returned.
 425      *
 426      * <p> To interpret localized string representations of a
 427      * floating-point value, use subclasses of {@link
 428      * java.text.NumberFormat}.
 429      *
 430      * <p>Note that trailing format specifiers, specifiers that
 431      * determine the type of a floating-point literal
 432      * ({@code 1.0f} is a {@code float} value;
 433      * {@code 1.0d} is a {@code double} value), do
 434      * <em>not</em> influence the results of this method.  In other
 435      * words, the numerical value of the input string is converted
 436      * directly to the target floating-point type.  The two-step
 437      * sequence of conversions, string to {@code float} followed
 438      * by {@code float} to {@code double}, is <em>not</em>
 439      * equivalent to converting a string directly to
 440      * {@code double}. For example, the {@code float}
 441      * literal {@code 0.1f} is equal to the {@code double}
 442      * value {@code 0.10000000149011612}; the {@code float}
 443      * literal {@code 0.1f} represents a different numerical
 444      * value than the {@code double} literal
 445      * {@code 0.1}. (The numerical value 0.1 cannot be exactly
 446      * represented in a binary floating-point number.)
 447      *
 448      * <p>To avoid calling this method on an invalid string and having
 449      * a {@code NumberFormatException} be thrown, the regular
 450      * expression below can be used to screen the input string:
 451      *
 452      * <pre>{@code
 453      *  final String Digits     = "(\\p{Digit}+)";
 454      *  final String HexDigits  = "(\\p{XDigit}+)";
 455      *  // an exponent is 'e' or 'E' followed by an optionally
 456      *  // signed decimal integer.
 457      *  final String Exp        = "[eE][+-]?"+Digits;
 458      *  final String fpRegex    =
 459      *      ("[\\x00-\\x20]*"+  // Optional leading "whitespace"
 460      *       "[+-]?(" + // Optional sign character
 461      *       "NaN|" +           // "NaN" string
 462      *       "Infinity|" +      // "Infinity" string
 463      *
 464      *       // A decimal floating-point string representing a finite positive
 465      *       // number without a leading sign has at most five basic pieces:
 466      *       // Digits . Digits ExponentPart FloatTypeSuffix
 467      *       //
 468      *       // Since this method allows integer-only strings as input
 469      *       // in addition to strings of floating-point literals, the
 470      *       // two sub-patterns below are simplifications of the grammar
 471      *       // productions from section 3.10.2 of
 472      *       // The Java Language Specification.
 473      *
 474      *       // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
 475      *       "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
 476      *
 477      *       // . Digits ExponentPart_opt FloatTypeSuffix_opt
 478      *       "(\\.("+Digits+")("+Exp+")?)|"+
 479      *
 480      *       // Hexadecimal strings
 481      *       "((" +
 482      *        // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
 483      *        "(0[xX]" + HexDigits + "(\\.)?)|" +
 484      *
 485      *        // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
 486      *        "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
 487      *
 488      *        ")[pP][+-]?" + Digits + "))" +
 489      *       "[fFdD]?))" +
 490      *       "[\\x00-\\x20]*");// Optional trailing "whitespace"
 491      *
 492      *  if (Pattern.matches(fpRegex, myString))
 493      *      Double.valueOf(myString); // Will not throw NumberFormatException
 494      *  else {
 495      *      // Perform suitable alternative action
 496      *  }
 497      * }</pre>
 498      *
 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
 503      *             parsable number.
 504      */
 505     public static Double valueOf(String s) throws NumberFormatException {
 506         return new Double(parseDouble(s));
 507     }
 508 
 509     /**
 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.
 517      *
 518      * @param  d a double value.
 519      * @return a {@code Double} instance representing {@code d}.
 520      * @since  1.5
 521      */
 522     @HotSpotIntrinsicCandidate
 523     public static Double valueOf(double d) {
 524         return new Double(d);
 525     }
 526 
 527     /**
 528      * Returns a new {@code double} initialized to the value
 529      * represented by the specified {@code String}, as performed
 530      * by the {@code valueOf} method of class
 531      * {@code Double}.
 532      *
 533      * @param  s   the string to be parsed.
 534      * @return the {@code double} value represented by the string
 535      *         argument.
 536      * @throws NullPointerException  if the string is null
 537      * @throws NumberFormatException if the string does not contain
 538      *         a parsable {@code double}.
 539      * @see    java.lang.Double#valueOf(String)
 540      * @since 1.2
 541      */
 542     public static double parseDouble(String s) throws NumberFormatException {
 543         return FloatingDecimal.parseDouble(s);
 544     }
 545 
 546     /**
 547      * Returns {@code true} if the specified number is a
 548      * Not-a-Number (NaN) value, {@code false} otherwise.
 549      *
 550      * @param   v   the value to be tested.
 551      * @return  {@code true} if the value of the argument is NaN;
 552      *          {@code false} otherwise.
 553      */
 554     public static boolean isNaN(double v) {
 555         return (v != v);
 556     }
 557 
 558     /**
 559      * Returns {@code true} if the specified number is infinitely
 560      * large in magnitude, {@code false} otherwise.
 561      *
 562      * @param   v   the value to be tested.
 563      * @return  {@code true} if the value of the argument is positive
 564      *          infinity or negative infinity; {@code false} otherwise.
 565      */
 566     public static boolean isInfinite(double v) {
 567         return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
 568     }
 569 
 570     /**
 571      * Returns {@code true} if the argument is a finite floating-point
 572      * value; returns {@code false} otherwise (for NaN and infinity
 573      * arguments).
 574      *
 575      * @param d the {@code double} value to be tested
 576      * @return {@code true} if the argument is a finite
 577      * floating-point value, {@code false} otherwise.
 578      * @since 1.8
 579      */
 580     public static boolean isFinite(double d) {
 581         return Math.abs(d) <= Double.MAX_VALUE;
 582     }
 583 
 584     /**
 585      * The value of the Double.
 586      *
 587      * @serial
 588      */
 589     private final double value;
 590 
 591     /**
 592      * Constructs a newly allocated {@code Double} object that
 593      * represents the primitive {@code double} argument.
 594      *
 595      * @param   value   the value to be represented by the {@code Double}.
 596      *
 597      * @deprecated
 598      * It is rarely appropriate to use this constructor. The static factory
 599      * {@link #valueOf(double)} is generally a better choice, as it is
 600      * likely to yield significantly better space and time performance.
 601      */
 602     @Deprecated(since="9")
 603     public Double(double value) {
 604         this.value = value;
 605     }
 606 
 607     /**
 608      * Constructs a newly allocated {@code Double} object that
 609      * represents the floating-point value of type {@code double}
 610      * represented by the string. The string is converted to a
 611      * {@code double} value as if by the {@code valueOf} method.
 612      *
 613      * @param  s  a string to be converted to a {@code Double}.
 614      * @throws    NumberFormatException if the string does not contain a
 615      *            parsable number.
 616      *
 617      * @deprecated
 618      * It is rarely appropriate to use this constructor.
 619      * Use {@link #parseDouble(String)} to convert a string to a
 620      * {@code double} primitive, or use {@link #valueOf(String)}
 621      * to convert a string to a {@code Double} object.
 622      */
 623     @Deprecated(since="9")
 624     public Double(String s) throws NumberFormatException {
 625         value = parseDouble(s);
 626     }
 627 
 628     /**
 629      * Returns {@code true} if this {@code Double} value is
 630      * a Not-a-Number (NaN), {@code false} otherwise.
 631      *
 632      * @return  {@code true} if the value represented by this object is
 633      *          NaN; {@code false} otherwise.
 634      */
 635     public boolean isNaN() {
 636         return isNaN(value);
 637     }
 638 
 639     /**
 640      * Returns {@code true} if this {@code Double} value is
 641      * infinitely large in magnitude, {@code false} otherwise.
 642      *
 643      * @return  {@code true} if the value represented by this object is
 644      *          positive infinity or negative infinity;
 645      *          {@code false} otherwise.
 646      */
 647     public boolean isInfinite() {
 648         return isInfinite(value);
 649     }
 650 
 651     /**
 652      * Returns a string representation of this {@code Double} object.
 653      * The primitive {@code double} value represented by this
 654      * object is converted to a string exactly as if by the method
 655      * {@code toString} of one argument.
 656      *
 657      * @return  a {@code String} representation of this object.
 658      * @see java.lang.Double#toString(double)
 659      */
 660     public String toString() {
 661         return toString(value);
 662     }
 663 
 664     /**
 665      * Returns the value of this {@code Double} as a {@code byte}
 666      * after a narrowing primitive conversion.
 667      *
 668      * @return  the {@code double} value represented by this object
 669      *          converted to type {@code byte}
 670      * @jls 5.1.3 Narrowing Primitive Conversions
 671      * @since 1.1
 672      */
 673     public byte byteValue() {
 674         return (byte)value;
 675     }
 676 
 677     /**
 678      * Returns the value of this {@code Double} as a {@code short}
 679      * after a narrowing primitive conversion.
 680      *
 681      * @return  the {@code double} value represented by this object
 682      *          converted to type {@code short}
 683      * @jls 5.1.3 Narrowing Primitive Conversions
 684      * @since 1.1
 685      */
 686     public short shortValue() {
 687         return (short)value;
 688     }
 689 
 690     /**
 691      * Returns the value of this {@code Double} as an {@code int}
 692      * after a narrowing primitive conversion.
 693      * @jls 5.1.3 Narrowing Primitive Conversions
 694      *
 695      * @return  the {@code double} value represented by this object
 696      *          converted to type {@code int}
 697      */
 698     public int intValue() {
 699         return (int)value;
 700     }
 701 
 702     /**
 703      * Returns the value of this {@code Double} as a {@code long}
 704      * after a narrowing primitive conversion.
 705      *
 706      * @return  the {@code double} value represented by this object
 707      *          converted to type {@code long}
 708      * @jls 5.1.3 Narrowing Primitive Conversions
 709      */
 710     public long longValue() {
 711         return (long)value;
 712     }
 713 
 714     /**
 715      * Returns the value of this {@code Double} as a {@code float}
 716      * after a narrowing primitive conversion.
 717      *
 718      * @return  the {@code double} value represented by this object
 719      *          converted to type {@code float}
 720      * @jls 5.1.3 Narrowing Primitive Conversions
 721      * @since 1.0
 722      */
 723     public float floatValue() {
 724         return (float)value;
 725     }
 726 
 727     /**
 728      * Returns the {@code double} value of this {@code Double} object.
 729      *
 730      * @return the {@code double} value represented by this object
 731      */
 732     @HotSpotIntrinsicCandidate
 733     public double doubleValue() {
 734         return value;
 735     }
 736 
 737     /**
 738      * Returns a hash code for this {@code Double} object. The
 739      * result is the exclusive OR of the two halves of the
 740      * {@code long} integer bit representation, exactly as
 741      * produced by the method {@link #doubleToLongBits(double)}, of
 742      * the primitive {@code double} value represented by this
 743      * {@code Double} object. That is, the hash code is the value
 744      * of the expression:
 745      *
 746      * <blockquote>
 747      *  {@code (int)(v^(v>>>32))}
 748      * </blockquote>
 749      *
 750      * where {@code v} is defined by:
 751      *
 752      * <blockquote>
 753      *  {@code long v = Double.doubleToLongBits(this.doubleValue());}
 754      * </blockquote>
 755      *
 756      * @return  a {@code hash code} value for this object.
 757      */
 758     @Override
 759     public int hashCode() {
 760         return Double.hashCode(value);
 761     }
 762 
 763     /**
 764      * Returns a hash code for a {@code double} value; compatible with
 765      * {@code Double.hashCode()}.
 766      *
 767      * @param value the value to hash
 768      * @return a hash code value for a {@code double} value.
 769      * @since 1.8
 770      */
 771     public static int hashCode(double value) {
 772         long bits = doubleToLongBits(value);
 773         return (int)(bits ^ (bits >>> 32));
 774     }
 775 
 776     /**
 777      * Compares this object against the specified object.  The result
 778      * is {@code true} if and only if the argument is not
 779      * {@code null} and is a {@code Double} object that
 780      * represents a {@code double} that has the same value as the
 781      * {@code double} represented by this object. For this
 782      * purpose, two {@code double} values are considered to be
 783      * the same if and only if the method {@link
 784      * #doubleToLongBits(double)} returns the identical
 785      * {@code long} value when applied to each.
 786      *
 787      * <p>Note that in most cases, for two instances of class
 788      * {@code Double}, {@code d1} and {@code d2}, the
 789      * value of {@code d1.equals(d2)} is {@code true} if and
 790      * only if
 791      *
 792      * <blockquote>
 793      *  {@code d1.doubleValue() == d2.doubleValue()}
 794      * </blockquote>
 795      *
 796      * <p>also has the value {@code true}. However, there are two
 797      * exceptions:
 798      * <ul>
 799      * <li>If {@code d1} and {@code d2} both represent
 800      *     {@code Double.NaN}, then the {@code equals} method
 801      *     returns {@code true}, even though
 802      *     {@code Double.NaN==Double.NaN} has the value
 803      *     {@code false}.
 804      * <li>If {@code d1} represents {@code +0.0} while
 805      *     {@code d2} represents {@code -0.0}, or vice versa,
 806      *     the {@code equal} test has the value {@code false},
 807      *     even though {@code +0.0==-0.0} has the value {@code true}.
 808      * </ul>
 809      * This definition allows hash tables to operate properly.
 810      * @param   obj   the object to compare with.
 811      * @return  {@code true} if the objects are the same;
 812      *          {@code false} otherwise.
 813      * @see java.lang.Double#doubleToLongBits(double)
 814      */
 815     public boolean equals(Object obj) {
 816         return (obj instanceof Double)
 817                && (doubleToLongBits(((Double)obj).value) ==
 818                       doubleToLongBits(value));
 819     }
 820 
 821     /**
 822      * Returns a representation of the specified floating-point value
 823      * according to the IEEE 754 floating-point "double
 824      * format" bit layout.
 825      *
 826      * <p>Bit 63 (the bit that is selected by the mask
 827      * {@code 0x8000000000000000L}) represents the sign of the
 828      * floating-point number. Bits
 829      * 62-52 (the bits that are selected by the mask
 830      * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
 831      * (the bits that are selected by the mask
 832      * {@code 0x000fffffffffffffL}) represent the significand
 833      * (sometimes called the mantissa) of the floating-point number.
 834      *
 835      * <p>If the argument is positive infinity, the result is
 836      * {@code 0x7ff0000000000000L}.
 837      *
 838      * <p>If the argument is negative infinity, the result is
 839      * {@code 0xfff0000000000000L}.
 840      *
 841      * <p>If the argument is NaN, the result is
 842      * {@code 0x7ff8000000000000L}.
 843      *
 844      * <p>In all cases, the result is a {@code long} integer that, when
 845      * given to the {@link #longBitsToDouble(long)} method, will produce a
 846      * floating-point value the same as the argument to
 847      * {@code doubleToLongBits} (except all NaN values are
 848      * collapsed to a single "canonical" NaN value).
 849      *
 850      * @param   value   a {@code double} precision floating-point number.
 851      * @return the bits that represent the floating-point number.
 852      */
 853     @HotSpotIntrinsicCandidate
 854     public static long doubleToLongBits(double value) {
 855         if (!isNaN(value)) {
 856             return doubleToRawLongBits(value);
 857         }
 858         return 0x7ff8000000000000L;
 859     }
 860 
 861     /**
 862      * Returns a representation of the specified floating-point value
 863      * according to the IEEE 754 floating-point "double
 864      * format" bit layout, preserving Not-a-Number (NaN) values.
 865      *
 866      * <p>Bit 63 (the bit that is selected by the mask
 867      * {@code 0x8000000000000000L}) represents the sign of the
 868      * floating-point number. Bits
 869      * 62-52 (the bits that are selected by the mask
 870      * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
 871      * (the bits that are selected by the mask
 872      * {@code 0x000fffffffffffffL}) represent the significand
 873      * (sometimes called the mantissa) of the floating-point number.
 874      *
 875      * <p>If the argument is positive infinity, the result is
 876      * {@code 0x7ff0000000000000L}.
 877      *
 878      * <p>If the argument is negative infinity, the result is
 879      * {@code 0xfff0000000000000L}.
 880      *
 881      * <p>If the argument is NaN, the result is the {@code long}
 882      * integer representing the actual NaN value.  Unlike the
 883      * {@code doubleToLongBits} method,
 884      * {@code doubleToRawLongBits} does not collapse all the bit
 885      * patterns encoding a NaN to a single "canonical" NaN
 886      * value.
 887      *
 888      * <p>In all cases, the result is a {@code long} integer that,
 889      * when given to the {@link #longBitsToDouble(long)} method, will
 890      * produce a floating-point value the same as the argument to
 891      * {@code doubleToRawLongBits}.
 892      *
 893      * @param   value   a {@code double} precision floating-point number.
 894      * @return the bits that represent the floating-point number.
 895      * @since 1.3
 896      */
 897     @HotSpotIntrinsicCandidate
 898     public static native long doubleToRawLongBits(double value);
 899 
 900     /**
 901      * Returns the {@code double} value corresponding to a given
 902      * bit representation.
 903      * The argument is considered to be a representation of a
 904      * floating-point value according to the IEEE 754 floating-point
 905      * "double format" bit layout.
 906      *
 907      * <p>If the argument is {@code 0x7ff0000000000000L}, the result
 908      * is positive infinity.
 909      *
 910      * <p>If the argument is {@code 0xfff0000000000000L}, the result
 911      * is negative infinity.
 912      *
 913      * <p>If the argument is any value in the range
 914      * {@code 0x7ff0000000000001L} through
 915      * {@code 0x7fffffffffffffffL} or in the range
 916      * {@code 0xfff0000000000001L} through
 917      * {@code 0xffffffffffffffffL}, the result is a NaN.  No IEEE
 918      * 754 floating-point operation provided by Java can distinguish
 919      * between two NaN values of the same type with different bit
 920      * patterns.  Distinct values of NaN are only distinguishable by
 921      * use of the {@code Double.doubleToRawLongBits} method.
 922      *
 923      * <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
 924      * values that can be computed from the argument:
 925      *
 926      * <blockquote><pre>{@code
 927      * int s = ((bits >> 63) == 0) ? 1 : -1;
 928      * int e = (int)((bits >> 52) & 0x7ffL);
 929      * long m = (e == 0) ?
 930      *                 (bits & 0xfffffffffffffL) << 1 :
 931      *                 (bits & 0xfffffffffffffL) | 0x10000000000000L;
 932      * }</pre></blockquote>
 933      *
 934      * Then the floating-point result equals the value of the mathematical
 935      * expression <i>s</i>&middot;<i>m</i>&middot;2<sup><i>e</i>-1075</sup>.
 936      *
 937      * <p>Note that this method may not be able to return a
 938      * {@code double} NaN with exactly same bit pattern as the
 939      * {@code long} argument.  IEEE 754 distinguishes between two
 940      * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>.  The
 941      * differences between the two kinds of NaN are generally not
 942      * visible in Java.  Arithmetic operations on signaling NaNs turn
 943      * them into quiet NaNs with a different, but often similar, bit
 944      * pattern.  However, on some processors merely copying a
 945      * signaling NaN also performs that conversion.  In particular,
 946      * copying a signaling NaN to return it to the calling method
 947      * may perform this conversion.  So {@code longBitsToDouble}
 948      * may not be able to return a {@code double} with a
 949      * signaling NaN bit pattern.  Consequently, for some
 950      * {@code long} values,
 951      * {@code doubleToRawLongBits(longBitsToDouble(start))} may
 952      * <i>not</i> equal {@code start}.  Moreover, which
 953      * particular bit patterns represent signaling NaNs is platform
 954      * dependent; although all NaN bit patterns, quiet or signaling,
 955      * must be in the NaN range identified above.
 956      *
 957      * @param   bits   any {@code long} integer.
 958      * @return  the {@code double} floating-point value with the same
 959      *          bit pattern.
 960      */
 961     @HotSpotIntrinsicCandidate
 962     public static native double longBitsToDouble(long bits);
 963 
 964     /**
 965      * Compares two {@code Double} objects numerically.  There
 966      * are two ways in which comparisons performed by this method
 967      * differ from those performed by the Java language numerical
 968      * comparison operators ({@code <, <=, ==, >=, >})
 969      * when applied to primitive {@code double} values:
 970      * <ul><li>
 971      *          {@code Double.NaN} is considered by this method
 972      *          to be equal to itself and greater than all other
 973      *          {@code double} values (including
 974      *          {@code Double.POSITIVE_INFINITY}).
 975      * <li>
 976      *          {@code 0.0d} is considered by this method to be greater
 977      *          than {@code -0.0d}.
 978      * </ul>
 979      * This ensures that the <i>natural ordering</i> of
 980      * {@code Double} objects imposed by this method is <i>consistent
 981      * with equals</i>.
 982      *
 983      * @param   anotherDouble   the {@code Double} to be compared.
 984      * @return  the value {@code 0} if {@code anotherDouble} is
 985      *          numerically equal to this {@code Double}; a value
 986      *          less than {@code 0} if this {@code Double}
 987      *          is numerically less than {@code anotherDouble};
 988      *          and a value greater than {@code 0} if this
 989      *          {@code Double} is numerically greater than
 990      *          {@code anotherDouble}.
 991      *
 992      * @since   1.2
 993      */
 994     public int compareTo(Double anotherDouble) {
 995         return Double.compare(value, anotherDouble.value);
 996     }
 997 
 998     /**
 999      * Compares the two specified {@code double} values. The sign
1000      * of the integer value returned is the same as that of the
1001      * integer that would be returned by the call:
1002      * <pre>
1003      *    new Double(d1).compareTo(new Double(d2))
1004      * </pre>
1005      *
1006      * @param   d1        the first {@code double} to compare
1007      * @param   d2        the second {@code double} to compare
1008      * @return  the value {@code 0} if {@code d1} is
1009      *          numerically equal to {@code d2}; a value less than
1010      *          {@code 0} if {@code d1} is numerically less than
1011      *          {@code d2}; and a value greater than {@code 0}
1012      *          if {@code d1} is numerically greater than
1013      *          {@code d2}.
1014      * @since 1.4
1015      */
1016     public static int compare(double d1, double d2) {
1017         if (d1 < d2)
1018             return -1;           // Neither val is NaN, thisVal is smaller
1019         if (d1 > d2)
1020             return 1;            // Neither val is NaN, thisVal is larger
1021 
1022         // Cannot use doubleToRawLongBits because of possibility of NaNs.
1023         long thisBits    = Double.doubleToLongBits(d1);
1024         long anotherBits = Double.doubleToLongBits(d2);
1025 
1026         return (thisBits == anotherBits ?  0 : // Values are equal
1027                 (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1028                  1));                          // (0.0, -0.0) or (NaN, !NaN)
1029     }
1030 
1031     /**
1032      * Adds two {@code double} values together as per the + operator.
1033      *
1034      * @param a the first operand
1035      * @param b the second operand
1036      * @return the sum of {@code a} and {@code b}
1037      * @jls 4.2.4 Floating-Point Operations
1038      * @see java.util.function.BinaryOperator
1039      * @since 1.8
1040      */
1041     public static double sum(double a, double b) {
1042         return a + b;
1043     }
1044 
1045     /**
1046      * Returns the greater of two {@code double} values
1047      * as if by calling {@link Math#max(double, double) Math.max}.
1048      *
1049      * @param a the first operand
1050      * @param b the second operand
1051      * @return the greater of {@code a} and {@code b}
1052      * @see java.util.function.BinaryOperator
1053      * @since 1.8
1054      */
1055     public static double max(double a, double b) {
1056         return Math.max(a, b);
1057     }
1058 
1059     /**
1060      * Returns the smaller of two {@code double} values
1061      * as if by calling {@link Math#min(double, double) Math.min}.
1062      *
1063      * @param a the first operand
1064      * @param b the second operand
1065      * @return the smaller of {@code a} and {@code b}.
1066      * @see java.util.function.BinaryOperator
1067      * @since 1.8
1068      */
1069     public static double min(double a, double b) {
1070         return Math.min(a, b);
1071     }
1072 
1073     /** use serialVersionUID from JDK 1.0.2 for interoperability */
1074     private static final long serialVersionUID = -9172774392245257468L;
1075 }