Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/lang/Double.java
38829 views
/*1* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.lang;2627import sun.misc.FloatingDecimal;28import sun.misc.FpUtils;29import sun.misc.DoubleConsts;3031/**32* The {@code Double} class wraps a value of the primitive type33* {@code double} in an object. An object of type34* {@code Double} contains a single field whose type is35* {@code double}.36*37* <p>In addition, this class provides several methods for converting a38* {@code double} to a {@code String} and a39* {@code String} to a {@code double}, as well as other40* constants and methods useful when dealing with a41* {@code double}.42*43* @author Lee Boynton44* @author Arthur van Hoff45* @author Joseph D. Darcy46* @since JDK1.047*/48public final class Double extends Number implements Comparable<Double> {49/**50* A constant holding the positive infinity of type51* {@code double}. It is equal to the value returned by52* {@code Double.longBitsToDouble(0x7ff0000000000000L)}.53*/54public static final double POSITIVE_INFINITY = 1.0 / 0.0;5556/**57* A constant holding the negative infinity of type58* {@code double}. It is equal to the value returned by59* {@code Double.longBitsToDouble(0xfff0000000000000L)}.60*/61public static final double NEGATIVE_INFINITY = -1.0 / 0.0;6263/**64* A constant holding a Not-a-Number (NaN) value of type65* {@code double}. It is equivalent to the value returned by66* {@code Double.longBitsToDouble(0x7ff8000000000000L)}.67*/68public static final double NaN = 0.0d / 0.0;6970/**71* A constant holding the largest positive finite value of type72* {@code double},73* (2-2<sup>-52</sup>)·2<sup>1023</sup>. It is equal to74* the hexadecimal floating-point literal75* {@code 0x1.fffffffffffffP+1023} and also equal to76* {@code Double.longBitsToDouble(0x7fefffffffffffffL)}.77*/78public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+3087980/**81* A constant holding the smallest positive normal value of type82* {@code double}, 2<sup>-1022</sup>. It is equal to the83* hexadecimal floating-point literal {@code 0x1.0p-1022} and also84* equal to {@code Double.longBitsToDouble(0x0010000000000000L)}.85*86* @since 1.687*/88public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-3088990/**91* A constant holding the smallest positive nonzero value of type92* {@code double}, 2<sup>-1074</sup>. It is equal to the93* hexadecimal floating-point literal94* {@code 0x0.0000000000001P-1022} and also equal to95* {@code Double.longBitsToDouble(0x1L)}.96*/97public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-3249899/**100* Maximum exponent a finite {@code double} variable may have.101* It is equal to the value returned by102* {@code Math.getExponent(Double.MAX_VALUE)}.103*104* @since 1.6105*/106public static final int MAX_EXPONENT = 1023;107108/**109* Minimum exponent a normalized {@code double} variable may110* have. It is equal to the value returned by111* {@code Math.getExponent(Double.MIN_NORMAL)}.112*113* @since 1.6114*/115public static final int MIN_EXPONENT = -1022;116117/**118* The number of bits used to represent a {@code double} value.119*120* @since 1.5121*/122public static final int SIZE = 64;123124/**125* The number of bytes used to represent a {@code double} value.126*127* @since 1.8128*/129public static final int BYTES = SIZE / Byte.SIZE;130131/**132* The {@code Class} instance representing the primitive type133* {@code double}.134*135* @since JDK1.1136*/137@SuppressWarnings("unchecked")138public static final Class<Double> TYPE = (Class<Double>) Class.getPrimitiveClass("double");139140/**141* Returns a string representation of the {@code double}142* argument. All characters mentioned below are ASCII characters.143* <ul>144* <li>If the argument is NaN, the result is the string145* "{@code NaN}".146* <li>Otherwise, the result is a string that represents the sign and147* magnitude (absolute value) of the argument. If the sign is negative,148* the first character of the result is '{@code -}'149* ({@code '\u005Cu002D'}); if the sign is positive, no sign character150* appears in the result. As for the magnitude <i>m</i>:151* <ul>152* <li>If <i>m</i> is infinity, it is represented by the characters153* {@code "Infinity"}; thus, positive infinity produces the result154* {@code "Infinity"} and negative infinity produces the result155* {@code "-Infinity"}.156*157* <li>If <i>m</i> is zero, it is represented by the characters158* {@code "0.0"}; thus, negative zero produces the result159* {@code "-0.0"} and positive zero produces the result160* {@code "0.0"}.161*162* <li>If <i>m</i> is greater than or equal to 10<sup>-3</sup> but less163* than 10<sup>7</sup>, then it is represented as the integer part of164* <i>m</i>, in decimal form with no leading zeroes, followed by165* '{@code .}' ({@code '\u005Cu002E'}), followed by one or166* more decimal digits representing the fractional part of <i>m</i>.167*168* <li>If <i>m</i> is less than 10<sup>-3</sup> or greater than or169* equal to 10<sup>7</sup>, then it is represented in so-called170* "computerized scientific notation." Let <i>n</i> be the unique171* integer such that 10<sup><i>n</i></sup> ≤ <i>m</i> {@literal <}172* 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the173* mathematically exact quotient of <i>m</i> and174* 10<sup><i>n</i></sup> so that 1 ≤ <i>a</i> {@literal <} 10. The175* magnitude is then represented as the integer part of <i>a</i>,176* as a single decimal digit, followed by '{@code .}'177* ({@code '\u005Cu002E'}), followed by decimal digits178* representing the fractional part of <i>a</i>, followed by the179* letter '{@code E}' ({@code '\u005Cu0045'}), followed180* by a representation of <i>n</i> as a decimal integer, as181* produced by the method {@link Integer#toString(int)}.182* </ul>183* </ul>184* How many digits must be printed for the fractional part of185* <i>m</i> or <i>a</i>? There must be at least one digit to represent186* the fractional part, and beyond that as many, but only as many, more187* digits as are needed to uniquely distinguish the argument value from188* adjacent values of type {@code double}. That is, suppose that189* <i>x</i> is the exact mathematical value represented by the decimal190* representation produced by this method for a finite nonzero argument191* <i>d</i>. Then <i>d</i> must be the {@code double} value nearest192* to <i>x</i>; or if two {@code double} values are equally close193* to <i>x</i>, then <i>d</i> must be one of them and the least194* significant bit of the significand of <i>d</i> must be {@code 0}.195*196* <p>To create localized string representations of a floating-point197* value, use subclasses of {@link java.text.NumberFormat}.198*199* @param d the {@code double} to be converted.200* @return a string representation of the argument.201*/202public static String toString(double d) {203return FloatingDecimal.toJavaFormatString(d);204}205206/**207* Returns a hexadecimal string representation of the208* {@code double} argument. All characters mentioned below209* are ASCII characters.210*211* <ul>212* <li>If the argument is NaN, the result is the string213* "{@code NaN}".214* <li>Otherwise, the result is a string that represents the sign215* and magnitude of the argument. If the sign is negative, the216* first character of the result is '{@code -}'217* ({@code '\u005Cu002D'}); if the sign is positive, no sign218* character appears in the result. As for the magnitude <i>m</i>:219*220* <ul>221* <li>If <i>m</i> is infinity, it is represented by the string222* {@code "Infinity"}; thus, positive infinity produces the223* result {@code "Infinity"} and negative infinity produces224* the result {@code "-Infinity"}.225*226* <li>If <i>m</i> is zero, it is represented by the string227* {@code "0x0.0p0"}; thus, negative zero produces the result228* {@code "-0x0.0p0"} and positive zero produces the result229* {@code "0x0.0p0"}.230*231* <li>If <i>m</i> is a {@code double} value with a232* normalized representation, substrings are used to represent the233* significand and exponent fields. The significand is234* represented by the characters {@code "0x1."}235* followed by a lowercase hexadecimal representation of the rest236* of the significand as a fraction. Trailing zeros in the237* hexadecimal representation are removed unless all the digits238* are zero, in which case a single zero is used. Next, the239* exponent is represented by {@code "p"} followed240* by a decimal string of the unbiased exponent as if produced by241* a call to {@link Integer#toString(int) Integer.toString} on the242* exponent value.243*244* <li>If <i>m</i> is a {@code double} value with a subnormal245* representation, the significand is represented by the246* characters {@code "0x0."} followed by a247* hexadecimal representation of the rest of the significand as a248* fraction. Trailing zeros in the hexadecimal representation are249* removed. Next, the exponent is represented by250* {@code "p-1022"}. Note that there must be at251* least one nonzero digit in a subnormal significand.252*253* </ul>254*255* </ul>256*257* <table border>258* <caption>Examples</caption>259* <tr><th>Floating-point Value</th><th>Hexadecimal String</th>260* <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td>261* <tr><td>{@code -1.0}</td> <td>{@code -0x1.0p0}</td>262* <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>263* <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>264* <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>265* <tr><td>{@code 0.25}</td> <td>{@code 0x1.0p-2}</td>266* <tr><td>{@code Double.MAX_VALUE}</td>267* <td>{@code 0x1.fffffffffffffp1023}</td>268* <tr><td>{@code Minimum Normal Value}</td>269* <td>{@code 0x1.0p-1022}</td>270* <tr><td>{@code Maximum Subnormal Value}</td>271* <td>{@code 0x0.fffffffffffffp-1022}</td>272* <tr><td>{@code Double.MIN_VALUE}</td>273* <td>{@code 0x0.0000000000001p-1022}</td>274* </table>275* @param d the {@code double} to be converted.276* @return a hex string representation of the argument.277* @since 1.5278* @author Joseph D. Darcy279*/280public static String toHexString(double d) {281/*282* Modeled after the "a" conversion specifier in C99, section283* 7.19.6.1; however, the output of this method is more284* tightly specified.285*/286if (!isFinite(d) )287// For infinity and NaN, use the decimal output.288return Double.toString(d);289else {290// Initialized to maximum size of output.291StringBuilder answer = new StringBuilder(24);292293if (Math.copySign(1.0, d) == -1.0) // value is negative,294answer.append("-"); // so append sign info295296answer.append("0x");297298d = Math.abs(d);299300if(d == 0.0) {301answer.append("0.0p0");302} else {303boolean subnormal = (d < DoubleConsts.MIN_NORMAL);304305// Isolate significand bits and OR in a high-order bit306// so that the string representation has a known307// length.308long signifBits = (Double.doubleToLongBits(d)309& DoubleConsts.SIGNIF_BIT_MASK) |3100x1000000000000000L;311312// Subnormal values have a 0 implicit bit; normal313// values have a 1 implicit bit.314answer.append(subnormal ? "0." : "1.");315316// Isolate the low-order 13 digits of the hex317// representation. If all the digits are zero,318// replace with a single 0; otherwise, remove all319// trailing zeros.320String signif = Long.toHexString(signifBits).substring(3,16);321answer.append(signif.equals("0000000000000") ? // 13 zeros322"0":323signif.replaceFirst("0{1,12}$", ""));324325answer.append('p');326// If the value is subnormal, use the E_min exponent327// value for double; otherwise, extract and report d's328// exponent (the representation of a subnormal uses329// E_min -1).330answer.append(subnormal ?331DoubleConsts.MIN_EXPONENT:332Math.getExponent(d));333}334return answer.toString();335}336}337338/**339* Returns a {@code Double} object holding the340* {@code double} value represented by the argument string341* {@code s}.342*343* <p>If {@code s} is {@code null}, then a344* {@code NullPointerException} is thrown.345*346* <p>Leading and trailing whitespace characters in {@code s}347* are ignored. Whitespace is removed as if by the {@link348* String#trim} method; that is, both ASCII space and control349* characters are removed. The rest of {@code s} should350* constitute a <i>FloatValue</i> as described by the lexical351* syntax rules:352*353* <blockquote>354* <dl>355* <dt><i>FloatValue:</i>356* <dd><i>Sign<sub>opt</sub></i> {@code NaN}357* <dd><i>Sign<sub>opt</sub></i> {@code Infinity}358* <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>359* <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>360* <dd><i>SignedInteger</i>361* </dl>362*363* <dl>364* <dt><i>HexFloatingPointLiteral</i>:365* <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>366* </dl>367*368* <dl>369* <dt><i>HexSignificand:</i>370* <dd><i>HexNumeral</i>371* <dd><i>HexNumeral</i> {@code .}372* <dd>{@code 0x} <i>HexDigits<sub>opt</sub>373* </i>{@code .}<i> HexDigits</i>374* <dd>{@code 0X}<i> HexDigits<sub>opt</sub>375* </i>{@code .} <i>HexDigits</i>376* </dl>377*378* <dl>379* <dt><i>BinaryExponent:</i>380* <dd><i>BinaryExponentIndicator SignedInteger</i>381* </dl>382*383* <dl>384* <dt><i>BinaryExponentIndicator:</i>385* <dd>{@code p}386* <dd>{@code P}387* </dl>388*389* </blockquote>390*391* where <i>Sign</i>, <i>FloatingPointLiteral</i>,392* <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and393* <i>FloatTypeSuffix</i> are as defined in the lexical structure394* sections of395* <cite>The Java™ Language Specification</cite>,396* except that underscores are not accepted between digits.397* If {@code s} does not have the form of398* a <i>FloatValue</i>, then a {@code NumberFormatException}399* is thrown. Otherwise, {@code s} is regarded as400* representing an exact decimal value in the usual401* "computerized scientific notation" or as an exact402* hexadecimal value; this exact numerical value is then403* conceptually converted to an "infinitely precise"404* binary value that is then rounded to type {@code double}405* by the usual round-to-nearest rule of IEEE 754 floating-point406* arithmetic, which includes preserving the sign of a zero407* value.408*409* Note that the round-to-nearest rule also implies overflow and410* underflow behaviour; if the exact value of {@code s} is large411* enough in magnitude (greater than or equal to ({@link412* #MAX_VALUE} + {@link Math#ulp(double) ulp(MAX_VALUE)}/2),413* rounding to {@code double} will result in an infinity and if the414* exact value of {@code s} is small enough in magnitude (less415* than or equal to {@link #MIN_VALUE}/2), rounding to float will416* result in a zero.417*418* Finally, after rounding a {@code Double} object representing419* this {@code double} value is returned.420*421* <p> To interpret localized string representations of a422* floating-point value, use subclasses of {@link423* java.text.NumberFormat}.424*425* <p>Note that trailing format specifiers, specifiers that426* determine the type of a floating-point literal427* ({@code 1.0f} is a {@code float} value;428* {@code 1.0d} is a {@code double} value), do429* <em>not</em> influence the results of this method. In other430* words, the numerical value of the input string is converted431* directly to the target floating-point type. The two-step432* sequence of conversions, string to {@code float} followed433* by {@code float} to {@code double}, is <em>not</em>434* equivalent to converting a string directly to435* {@code double}. For example, the {@code float}436* literal {@code 0.1f} is equal to the {@code double}437* value {@code 0.10000000149011612}; the {@code float}438* literal {@code 0.1f} represents a different numerical439* value than the {@code double} literal440* {@code 0.1}. (The numerical value 0.1 cannot be exactly441* represented in a binary floating-point number.)442*443* <p>To avoid calling this method on an invalid string and having444* a {@code NumberFormatException} be thrown, the regular445* expression below can be used to screen the input string:446*447* <pre>{@code448* final String Digits = "(\\p{Digit}+)";449* final String HexDigits = "(\\p{XDigit}+)";450* // an exponent is 'e' or 'E' followed by an optionally451* // signed decimal integer.452* final String Exp = "[eE][+-]?"+Digits;453* final String fpRegex =454* ("[\\x00-\\x20]*"+ // Optional leading "whitespace"455* "[+-]?(" + // Optional sign character456* "NaN|" + // "NaN" string457* "Infinity|" + // "Infinity" string458*459* // A decimal floating-point string representing a finite positive460* // number without a leading sign has at most five basic pieces:461* // Digits . Digits ExponentPart FloatTypeSuffix462* //463* // Since this method allows integer-only strings as input464* // in addition to strings of floating-point literals, the465* // two sub-patterns below are simplifications of the grammar466* // productions from section 3.10.2 of467* // The Java Language Specification.468*469* // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt470* "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+471*472* // . Digits ExponentPart_opt FloatTypeSuffix_opt473* "(\\.("+Digits+")("+Exp+")?)|"+474*475* // Hexadecimal strings476* "((" +477* // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt478* "(0[xX]" + HexDigits + "(\\.)?)|" +479*480* // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt481* "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +482*483* ")[pP][+-]?" + Digits + "))" +484* "[fFdD]?))" +485* "[\\x00-\\x20]*");// Optional trailing "whitespace"486*487* if (Pattern.matches(fpRegex, myString))488* Double.valueOf(myString); // Will not throw NumberFormatException489* else {490* // Perform suitable alternative action491* }492* }</pre>493*494* @param s the string to be parsed.495* @return a {@code Double} object holding the value496* represented by the {@code String} argument.497* @throws NumberFormatException if the string does not contain a498* parsable number.499*/500public static Double valueOf(String s) throws NumberFormatException {501return new Double(parseDouble(s));502}503504/**505* Returns a {@code Double} instance representing the specified506* {@code double} value.507* If a new {@code Double} instance is not required, this method508* should generally be used in preference to the constructor509* {@link #Double(double)}, as this method is likely to yield510* significantly better space and time performance by caching511* frequently requested values.512*513* @param d a double value.514* @return a {@code Double} instance representing {@code d}.515* @since 1.5516*/517public static Double valueOf(double d) {518return new Double(d);519}520521/**522* Returns a new {@code double} initialized to the value523* represented by the specified {@code String}, as performed524* by the {@code valueOf} method of class525* {@code Double}.526*527* @param s the string to be parsed.528* @return the {@code double} value represented by the string529* argument.530* @throws NullPointerException if the string is null531* @throws NumberFormatException if the string does not contain532* a parsable {@code double}.533* @see java.lang.Double#valueOf(String)534* @since 1.2535*/536public static double parseDouble(String s) throws NumberFormatException {537return FloatingDecimal.parseDouble(s);538}539540/**541* Returns {@code true} if the specified number is a542* Not-a-Number (NaN) value, {@code false} otherwise.543*544* @param v the value to be tested.545* @return {@code true} if the value of the argument is NaN;546* {@code false} otherwise.547*/548public static boolean isNaN(double v) {549return (v != v);550}551552/**553* Returns {@code true} if the specified number is infinitely554* large in magnitude, {@code false} otherwise.555*556* @param v the value to be tested.557* @return {@code true} if the value of the argument is positive558* infinity or negative infinity; {@code false} otherwise.559*/560public static boolean isInfinite(double v) {561return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);562}563564/**565* Returns {@code true} if the argument is a finite floating-point566* value; returns {@code false} otherwise (for NaN and infinity567* arguments).568*569* @param d the {@code double} value to be tested570* @return {@code true} if the argument is a finite571* floating-point value, {@code false} otherwise.572* @since 1.8573*/574public static boolean isFinite(double d) {575return Math.abs(d) <= DoubleConsts.MAX_VALUE;576}577578/**579* The value of the Double.580*581* @serial582*/583private final double value;584585/**586* Constructs a newly allocated {@code Double} object that587* represents the primitive {@code double} argument.588*589* @param value the value to be represented by the {@code Double}.590*/591public Double(double value) {592this.value = value;593}594595/**596* Constructs a newly allocated {@code Double} object that597* represents the floating-point value of type {@code double}598* represented by the string. The string is converted to a599* {@code double} value as if by the {@code valueOf} method.600*601* @param s a string to be converted to a {@code Double}.602* @throws NumberFormatException if the string does not contain a603* parsable number.604* @see java.lang.Double#valueOf(java.lang.String)605*/606public Double(String s) throws NumberFormatException {607value = parseDouble(s);608}609610/**611* Returns {@code true} if this {@code Double} value is612* a Not-a-Number (NaN), {@code false} otherwise.613*614* @return {@code true} if the value represented by this object is615* NaN; {@code false} otherwise.616*/617public boolean isNaN() {618return isNaN(value);619}620621/**622* Returns {@code true} if this {@code Double} value is623* infinitely large in magnitude, {@code false} otherwise.624*625* @return {@code true} if the value represented by this object is626* positive infinity or negative infinity;627* {@code false} otherwise.628*/629public boolean isInfinite() {630return isInfinite(value);631}632633/**634* Returns a string representation of this {@code Double} object.635* The primitive {@code double} value represented by this636* object is converted to a string exactly as if by the method637* {@code toString} of one argument.638*639* @return a {@code String} representation of this object.640* @see java.lang.Double#toString(double)641*/642public String toString() {643return toString(value);644}645646/**647* Returns the value of this {@code Double} as a {@code byte}648* after a narrowing primitive conversion.649*650* @return the {@code double} value represented by this object651* converted to type {@code byte}652* @jls 5.1.3 Narrowing Primitive Conversions653* @since JDK1.1654*/655public byte byteValue() {656return (byte)value;657}658659/**660* Returns the value of this {@code Double} as a {@code short}661* after a narrowing primitive conversion.662*663* @return the {@code double} value represented by this object664* converted to type {@code short}665* @jls 5.1.3 Narrowing Primitive Conversions666* @since JDK1.1667*/668public short shortValue() {669return (short)value;670}671672/**673* Returns the value of this {@code Double} as an {@code int}674* after a narrowing primitive conversion.675* @jls 5.1.3 Narrowing Primitive Conversions676*677* @return the {@code double} value represented by this object678* converted to type {@code int}679*/680public int intValue() {681return (int)value;682}683684/**685* Returns the value of this {@code Double} as a {@code long}686* after a narrowing primitive conversion.687*688* @return the {@code double} value represented by this object689* converted to type {@code long}690* @jls 5.1.3 Narrowing Primitive Conversions691*/692public long longValue() {693return (long)value;694}695696/**697* Returns the value of this {@code Double} as a {@code float}698* after a narrowing primitive conversion.699*700* @return the {@code double} value represented by this object701* converted to type {@code float}702* @jls 5.1.3 Narrowing Primitive Conversions703* @since JDK1.0704*/705public float floatValue() {706return (float)value;707}708709/**710* Returns the {@code double} value of this {@code Double} object.711*712* @return the {@code double} value represented by this object713*/714public double doubleValue() {715return value;716}717718/**719* Returns a hash code for this {@code Double} object. The720* result is the exclusive OR of the two halves of the721* {@code long} integer bit representation, exactly as722* produced by the method {@link #doubleToLongBits(double)}, of723* the primitive {@code double} value represented by this724* {@code Double} object. That is, the hash code is the value725* of the expression:726*727* <blockquote>728* {@code (int)(v^(v>>>32))}729* </blockquote>730*731* where {@code v} is defined by:732*733* <blockquote>734* {@code long v = Double.doubleToLongBits(this.doubleValue());}735* </blockquote>736*737* @return a {@code hash code} value for this object.738*/739@Override740public int hashCode() {741return Double.hashCode(value);742}743744/**745* Returns a hash code for a {@code double} value; compatible with746* {@code Double.hashCode()}.747*748* @param value the value to hash749* @return a hash code value for a {@code double} value.750* @since 1.8751*/752public static int hashCode(double value) {753long bits = doubleToLongBits(value);754return (int)(bits ^ (bits >>> 32));755}756757/**758* Compares this object against the specified object. The result759* is {@code true} if and only if the argument is not760* {@code null} and is a {@code Double} object that761* represents a {@code double} that has the same value as the762* {@code double} represented by this object. For this763* purpose, two {@code double} values are considered to be764* the same if and only if the method {@link765* #doubleToLongBits(double)} returns the identical766* {@code long} value when applied to each.767*768* <p>Note that in most cases, for two instances of class769* {@code Double}, {@code d1} and {@code d2}, the770* value of {@code d1.equals(d2)} is {@code true} if and771* only if772*773* <blockquote>774* {@code d1.doubleValue() == d2.doubleValue()}775* </blockquote>776*777* <p>also has the value {@code true}. However, there are two778* exceptions:779* <ul>780* <li>If {@code d1} and {@code d2} both represent781* {@code Double.NaN}, then the {@code equals} method782* returns {@code true}, even though783* {@code Double.NaN==Double.NaN} has the value784* {@code false}.785* <li>If {@code d1} represents {@code +0.0} while786* {@code d2} represents {@code -0.0}, or vice versa,787* the {@code equal} test has the value {@code false},788* even though {@code +0.0==-0.0} has the value {@code true}.789* </ul>790* This definition allows hash tables to operate properly.791* @param obj the object to compare with.792* @return {@code true} if the objects are the same;793* {@code false} otherwise.794* @see java.lang.Double#doubleToLongBits(double)795*/796public boolean equals(Object obj) {797return (obj instanceof Double)798&& (doubleToLongBits(((Double)obj).value) ==799doubleToLongBits(value));800}801802/**803* Returns a representation of the specified floating-point value804* according to the IEEE 754 floating-point "double805* format" bit layout.806*807* <p>Bit 63 (the bit that is selected by the mask808* {@code 0x8000000000000000L}) represents the sign of the809* floating-point number. Bits810* 62-52 (the bits that are selected by the mask811* {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0812* (the bits that are selected by the mask813* {@code 0x000fffffffffffffL}) represent the significand814* (sometimes called the mantissa) of the floating-point number.815*816* <p>If the argument is positive infinity, the result is817* {@code 0x7ff0000000000000L}.818*819* <p>If the argument is negative infinity, the result is820* {@code 0xfff0000000000000L}.821*822* <p>If the argument is NaN, the result is823* {@code 0x7ff8000000000000L}.824*825* <p>In all cases, the result is a {@code long} integer that, when826* given to the {@link #longBitsToDouble(long)} method, will produce a827* floating-point value the same as the argument to828* {@code doubleToLongBits} (except all NaN values are829* collapsed to a single "canonical" NaN value).830*831* @param value a {@code double} precision floating-point number.832* @return the bits that represent the floating-point number.833*/834public static long doubleToLongBits(double value) {835long result = doubleToRawLongBits(value);836// Check for NaN based on values of bit fields, maximum837// exponent and nonzero significand.838if ( ((result & DoubleConsts.EXP_BIT_MASK) ==839DoubleConsts.EXP_BIT_MASK) &&840(result & DoubleConsts.SIGNIF_BIT_MASK) != 0L)841result = 0x7ff8000000000000L;842return result;843}844845/**846* Returns a representation of the specified floating-point value847* according to the IEEE 754 floating-point "double848* format" bit layout, preserving Not-a-Number (NaN) values.849*850* <p>Bit 63 (the bit that is selected by the mask851* {@code 0x8000000000000000L}) represents the sign of the852* floating-point number. Bits853* 62-52 (the bits that are selected by the mask854* {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0855* (the bits that are selected by the mask856* {@code 0x000fffffffffffffL}) represent the significand857* (sometimes called the mantissa) of the floating-point number.858*859* <p>If the argument is positive infinity, the result is860* {@code 0x7ff0000000000000L}.861*862* <p>If the argument is negative infinity, the result is863* {@code 0xfff0000000000000L}.864*865* <p>If the argument is NaN, the result is the {@code long}866* integer representing the actual NaN value. Unlike the867* {@code doubleToLongBits} method,868* {@code doubleToRawLongBits} does not collapse all the bit869* patterns encoding a NaN to a single "canonical" NaN870* value.871*872* <p>In all cases, the result is a {@code long} integer that,873* when given to the {@link #longBitsToDouble(long)} method, will874* produce a floating-point value the same as the argument to875* {@code doubleToRawLongBits}.876*877* @param value a {@code double} precision floating-point number.878* @return the bits that represent the floating-point number.879* @since 1.3880*/881public static native long doubleToRawLongBits(double value);882883/**884* Returns the {@code double} value corresponding to a given885* bit representation.886* The argument is considered to be a representation of a887* floating-point value according to the IEEE 754 floating-point888* "double format" bit layout.889*890* <p>If the argument is {@code 0x7ff0000000000000L}, the result891* is positive infinity.892*893* <p>If the argument is {@code 0xfff0000000000000L}, the result894* is negative infinity.895*896* <p>If the argument is any value in the range897* {@code 0x7ff0000000000001L} through898* {@code 0x7fffffffffffffffL} or in the range899* {@code 0xfff0000000000001L} through900* {@code 0xffffffffffffffffL}, the result is a NaN. No IEEE901* 754 floating-point operation provided by Java can distinguish902* between two NaN values of the same type with different bit903* patterns. Distinct values of NaN are only distinguishable by904* use of the {@code Double.doubleToRawLongBits} method.905*906* <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three907* values that can be computed from the argument:908*909* <blockquote><pre>{@code910* int s = ((bits >> 63) == 0) ? 1 : -1;911* int e = (int)((bits >> 52) & 0x7ffL);912* long m = (e == 0) ?913* (bits & 0xfffffffffffffL) << 1 :914* (bits & 0xfffffffffffffL) | 0x10000000000000L;915* }</pre></blockquote>916*917* Then the floating-point result equals the value of the mathematical918* expression <i>s</i>·<i>m</i>·2<sup><i>e</i>-1075</sup>.919*920* <p>Note that this method may not be able to return a921* {@code double} NaN with exactly same bit pattern as the922* {@code long} argument. IEEE 754 distinguishes between two923* kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The924* differences between the two kinds of NaN are generally not925* visible in Java. Arithmetic operations on signaling NaNs turn926* them into quiet NaNs with a different, but often similar, bit927* pattern. However, on some processors merely copying a928* signaling NaN also performs that conversion. In particular,929* copying a signaling NaN to return it to the calling method930* may perform this conversion. So {@code longBitsToDouble}931* may not be able to return a {@code double} with a932* signaling NaN bit pattern. Consequently, for some933* {@code long} values,934* {@code doubleToRawLongBits(longBitsToDouble(start))} may935* <i>not</i> equal {@code start}. Moreover, which936* particular bit patterns represent signaling NaNs is platform937* dependent; although all NaN bit patterns, quiet or signaling,938* must be in the NaN range identified above.939*940* @param bits any {@code long} integer.941* @return the {@code double} floating-point value with the same942* bit pattern.943*/944public static native double longBitsToDouble(long bits);945946/**947* Compares two {@code Double} objects numerically. There948* are two ways in which comparisons performed by this method949* differ from those performed by the Java language numerical950* comparison operators ({@code <, <=, ==, >=, >})951* when applied to primitive {@code double} values:952* <ul><li>953* {@code Double.NaN} is considered by this method954* to be equal to itself and greater than all other955* {@code double} values (including956* {@code Double.POSITIVE_INFINITY}).957* <li>958* {@code 0.0d} is considered by this method to be greater959* than {@code -0.0d}.960* </ul>961* This ensures that the <i>natural ordering</i> of962* {@code Double} objects imposed by this method is <i>consistent963* with equals</i>.964*965* @param anotherDouble the {@code Double} to be compared.966* @return the value {@code 0} if {@code anotherDouble} is967* numerically equal to this {@code Double}; a value968* less than {@code 0} if this {@code Double}969* is numerically less than {@code anotherDouble};970* and a value greater than {@code 0} if this971* {@code Double} is numerically greater than972* {@code anotherDouble}.973*974* @since 1.2975*/976public int compareTo(Double anotherDouble) {977return Double.compare(value, anotherDouble.value);978}979980/**981* Compares the two specified {@code double} values. The sign982* of the integer value returned is the same as that of the983* integer that would be returned by the call:984* <pre>985* new Double(d1).compareTo(new Double(d2))986* </pre>987*988* @param d1 the first {@code double} to compare989* @param d2 the second {@code double} to compare990* @return the value {@code 0} if {@code d1} is991* numerically equal to {@code d2}; a value less than992* {@code 0} if {@code d1} is numerically less than993* {@code d2}; and a value greater than {@code 0}994* if {@code d1} is numerically greater than995* {@code d2}.996* @since 1.4997*/998public static int compare(double d1, double d2) {999if (d1 < d2)1000return -1; // Neither val is NaN, thisVal is smaller1001if (d1 > d2)1002return 1; // Neither val is NaN, thisVal is larger10031004// Cannot use doubleToRawLongBits because of possibility of NaNs.1005long thisBits = Double.doubleToLongBits(d1);1006long anotherBits = Double.doubleToLongBits(d2);10071008return (thisBits == anotherBits ? 0 : // Values are equal1009(thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)10101)); // (0.0, -0.0) or (NaN, !NaN)1011}10121013/**1014* Adds two {@code double} values together as per the + operator.1015*1016* @param a the first operand1017* @param b the second operand1018* @return the sum of {@code a} and {@code b}1019* @jls 4.2.4 Floating-Point Operations1020* @see java.util.function.BinaryOperator1021* @since 1.81022*/1023public static double sum(double a, double b) {1024return a + b;1025}10261027/**1028* Returns the greater of two {@code double} values1029* as if by calling {@link Math#max(double, double) Math.max}.1030*1031* @param a the first operand1032* @param b the second operand1033* @return the greater of {@code a} and {@code b}1034* @see java.util.function.BinaryOperator1035* @since 1.81036*/1037public static double max(double a, double b) {1038return Math.max(a, b);1039}10401041/**1042* Returns the smaller of two {@code double} values1043* as if by calling {@link Math#min(double, double) Math.min}.1044*1045* @param a the first operand1046* @param b the second operand1047* @return the smaller of {@code a} and {@code b}.1048* @see java.util.function.BinaryOperator1049* @since 1.81050*/1051public static double min(double a, double b) {1052return Math.min(a, b);1053}10541055/** use serialVersionUID from JDK 1.0.2 for interoperability */1056private static final long serialVersionUID = -9172774392245257468L;1057}105810591060