0N/A/*
3202N/A * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.lang;
0N/A
0N/Aimport sun.misc.FloatingDecimal;
0N/Aimport sun.misc.FpUtils;
0N/Aimport sun.misc.DoubleConsts;
0N/A
0N/A/**
0N/A * The {@code Double} class wraps a value of the primitive type
0N/A * {@code double} in an object. An object of type
0N/A * {@code Double} contains a single field whose type is
0N/A * {@code double}.
0N/A *
0N/A * <p>In addition, this class provides several methods for converting a
0N/A * {@code double} to a {@code String} and a
0N/A * {@code String} to a {@code double}, as well as other
0N/A * constants and methods useful when dealing with a
0N/A * {@code double}.
0N/A *
0N/A * @author Lee Boynton
0N/A * @author Arthur van Hoff
0N/A * @author Joseph D. Darcy
0N/A * @since JDK1.0
0N/A */
0N/Apublic final class Double extends Number implements Comparable<Double> {
0N/A /**
0N/A * A constant holding the positive infinity of type
0N/A * {@code double}. It is equal to the value returned by
0N/A * {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
0N/A */
0N/A public static final double POSITIVE_INFINITY = 1.0 / 0.0;
0N/A
0N/A /**
0N/A * A constant holding the negative infinity of type
0N/A * {@code double}. It is equal to the value returned by
0N/A * {@code Double.longBitsToDouble(0xfff0000000000000L)}.
0N/A */
0N/A public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
0N/A
0N/A /**
0N/A * A constant holding a Not-a-Number (NaN) value of type
0N/A * {@code double}. It is equivalent to the value returned by
0N/A * {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
0N/A */
0N/A public static final double NaN = 0.0d / 0.0;
0N/A
0N/A /**
0N/A * A constant holding the largest positive finite value of type
0N/A * {@code double},
0N/A * (2-2<sup>-52</sup>)&middot;2<sup>1023</sup>. It is equal to
0N/A * the hexadecimal floating-point literal
0N/A * {@code 0x1.fffffffffffffP+1023} and also equal to
0N/A * {@code Double.longBitsToDouble(0x7fefffffffffffffL)}.
0N/A */
0N/A public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308
0N/A
0N/A /**
0N/A * A constant holding the smallest positive normal value of type
0N/A * {@code double}, 2<sup>-1022</sup>. It is equal to the
0N/A * hexadecimal floating-point literal {@code 0x1.0p-1022} and also
0N/A * equal to {@code Double.longBitsToDouble(0x0010000000000000L)}.
0N/A *
0N/A * @since 1.6
0N/A */
0N/A public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308
0N/A
0N/A /**
0N/A * A constant holding the smallest positive nonzero value of type
0N/A * {@code double}, 2<sup>-1074</sup>. It is equal to the
0N/A * hexadecimal floating-point literal
0N/A * {@code 0x0.0000000000001P-1022} and also equal to
0N/A * {@code Double.longBitsToDouble(0x1L)}.
0N/A */
0N/A public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324
0N/A
0N/A /**
0N/A * Maximum exponent a finite {@code double} variable may have.
0N/A * It is equal to the value returned by
0N/A * {@code Math.getExponent(Double.MAX_VALUE)}.
0N/A *
0N/A * @since 1.6
0N/A */
0N/A public static final int MAX_EXPONENT = 1023;
0N/A
0N/A /**
0N/A * Minimum exponent a normalized {@code double} variable may
0N/A * have. It is equal to the value returned by
0N/A * {@code Math.getExponent(Double.MIN_NORMAL)}.
0N/A *
0N/A * @since 1.6
0N/A */
0N/A public static final int MIN_EXPONENT = -1022;
0N/A
0N/A /**
0N/A * The number of bits used to represent a {@code double} value.
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public static final int SIZE = 64;
0N/A
0N/A /**
0N/A * The {@code Class} instance representing the primitive type
0N/A * {@code double}.
0N/A *
0N/A * @since JDK1.1
0N/A */
0N/A public static final Class<Double> TYPE = (Class<Double>) Class.getPrimitiveClass("double");
0N/A
0N/A /**
0N/A * Returns a string representation of the {@code double}
0N/A * argument. All characters mentioned below are ASCII characters.
0N/A * <ul>
0N/A * <li>If the argument is NaN, the result is the string
0N/A * "{@code NaN}".
0N/A * <li>Otherwise, the result is a string that represents the sign and
0N/A * magnitude (absolute value) of the argument. If the sign is negative,
0N/A * the first character of the result is '{@code -}'
0N/A * (<code>'&#92;u002D'</code>); if the sign is positive, no sign character
0N/A * appears in the result. As for the magnitude <i>m</i>:
0N/A * <ul>
0N/A * <li>If <i>m</i> is infinity, it is represented by the characters
0N/A * {@code "Infinity"}; thus, positive infinity produces the result
0N/A * {@code "Infinity"} and negative infinity produces the result
0N/A * {@code "-Infinity"}.
0N/A *
0N/A * <li>If <i>m</i> is zero, it is represented by the characters
0N/A * {@code "0.0"}; thus, negative zero produces the result
0N/A * {@code "-0.0"} and positive zero produces the result
0N/A * {@code "0.0"}.
0N/A *
0N/A * <li>If <i>m</i> is greater than or equal to 10<sup>-3</sup> but less
0N/A * than 10<sup>7</sup>, then it is represented as the integer part of
0N/A * <i>m</i>, in decimal form with no leading zeroes, followed by
0N/A * '{@code .}' (<code>'&#92;u002E'</code>), followed by one or
0N/A * more decimal digits representing the fractional part of <i>m</i>.
0N/A *
0N/A * <li>If <i>m</i> is less than 10<sup>-3</sup> or greater than or
0N/A * equal to 10<sup>7</sup>, then it is represented in so-called
0N/A * "computerized scientific notation." Let <i>n</i> be the unique
0N/A * integer such that 10<sup><i>n</i></sup> &le; <i>m</i> {@literal <}
0N/A * 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
0N/A * mathematically exact quotient of <i>m</i> and
0N/A * 10<sup><i>n</i></sup> so that 1 &le; <i>a</i> {@literal <} 10. The
0N/A * magnitude is then represented as the integer part of <i>a</i>,
0N/A * as a single decimal digit, followed by '{@code .}'
0N/A * (<code>'&#92;u002E'</code>), followed by decimal digits
0N/A * representing the fractional part of <i>a</i>, followed by the
0N/A * letter '{@code E}' (<code>'&#92;u0045'</code>), followed
0N/A * by a representation of <i>n</i> as a decimal integer, as
0N/A * produced by the method {@link Integer#toString(int)}.
0N/A * </ul>
0N/A * </ul>
0N/A * How many digits must be printed for the fractional part of
0N/A * <i>m</i> or <i>a</i>? There must be at least one digit to represent
0N/A * the fractional part, and beyond that as many, but only as many, more
0N/A * digits as are needed to uniquely distinguish the argument value from
0N/A * adjacent values of type {@code double}. That is, suppose that
0N/A * <i>x</i> is the exact mathematical value represented by the decimal
0N/A * representation produced by this method for a finite nonzero argument
0N/A * <i>d</i>. Then <i>d</i> must be the {@code double} value nearest
0N/A * to <i>x</i>; or if two {@code double} values are equally close
0N/A * to <i>x</i>, then <i>d</i> must be one of them and the least
0N/A * significant bit of the significand of <i>d</i> must be {@code 0}.
0N/A *
0N/A * <p>To create localized string representations of a floating-point
0N/A * value, use subclasses of {@link java.text.NumberFormat}.
0N/A *
0N/A * @param d the {@code double} to be converted.
0N/A * @return a string representation of the argument.
0N/A */
0N/A public static String toString(double d) {
0N/A return new FloatingDecimal(d).toJavaFormatString();
0N/A }
0N/A
0N/A /**
0N/A * Returns a hexadecimal string representation of the
0N/A * {@code double} argument. All characters mentioned below
0N/A * are ASCII characters.
0N/A *
0N/A * <ul>
0N/A * <li>If the argument is NaN, the result is the string
0N/A * "{@code NaN}".
0N/A * <li>Otherwise, the result is a string that represents the sign
0N/A * and magnitude of the argument. If the sign is negative, the
0N/A * first character of the result is '{@code -}'
0N/A * (<code>'&#92;u002D'</code>); if the sign is positive, no sign
0N/A * character appears in the result. As for the magnitude <i>m</i>:
0N/A *
0N/A * <ul>
0N/A * <li>If <i>m</i> is infinity, it is represented by the string
0N/A * {@code "Infinity"}; thus, positive infinity produces the
0N/A * result {@code "Infinity"} and negative infinity produces
0N/A * the result {@code "-Infinity"}.
0N/A *
0N/A * <li>If <i>m</i> is zero, it is represented by the string
0N/A * {@code "0x0.0p0"}; thus, negative zero produces the result
0N/A * {@code "-0x0.0p0"} and positive zero produces the result
0N/A * {@code "0x0.0p0"}.
0N/A *
0N/A * <li>If <i>m</i> is a {@code double} value with a
0N/A * normalized representation, substrings are used to represent the
0N/A * significand and exponent fields. The significand is
0N/A * represented by the characters {@code "0x1."}
0N/A * followed by a lowercase hexadecimal representation of the rest
0N/A * of the significand as a fraction. Trailing zeros in the
0N/A * hexadecimal representation are removed unless all the digits
0N/A * are zero, in which case a single zero is used. Next, the
0N/A * exponent is represented by {@code "p"} followed
0N/A * by a decimal string of the unbiased exponent as if produced by
0N/A * a call to {@link Integer#toString(int) Integer.toString} on the
0N/A * exponent value.
0N/A *
0N/A * <li>If <i>m</i> is a {@code double} value with a subnormal
0N/A * representation, the significand is represented by the
0N/A * characters {@code "0x0."} followed by a
0N/A * hexadecimal representation of the rest of the significand as a
0N/A * fraction. Trailing zeros in the hexadecimal representation are
0N/A * removed. Next, the exponent is represented by
0N/A * {@code "p-1022"}. Note that there must be at
0N/A * least one nonzero digit in a subnormal significand.
0N/A *
0N/A * </ul>
0N/A *
0N/A * </ul>
0N/A *
0N/A * <table border>
0N/A * <caption><h3>Examples</h3></caption>
0N/A * <tr><th>Floating-point Value</th><th>Hexadecimal String</th>
0N/A * <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td>
0N/A * <tr><td>{@code -1.0}</td> <td>{@code -0x1.0p0}</td>
0N/A * <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>
0N/A * <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>
0N/A * <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>
0N/A * <tr><td>{@code 0.25}</td> <td>{@code 0x1.0p-2}</td>
0N/A * <tr><td>{@code Double.MAX_VALUE}</td>
0N/A * <td>{@code 0x1.fffffffffffffp1023}</td>
0N/A * <tr><td>{@code Minimum Normal Value}</td>
0N/A * <td>{@code 0x1.0p-1022}</td>
0N/A * <tr><td>{@code Maximum Subnormal Value}</td>
0N/A * <td>{@code 0x0.fffffffffffffp-1022}</td>
0N/A * <tr><td>{@code Double.MIN_VALUE}</td>
0N/A * <td>{@code 0x0.0000000000001p-1022}</td>
0N/A * </table>
0N/A * @param d the {@code double} to be converted.
0N/A * @return a hex string representation of the argument.
0N/A * @since 1.5
0N/A * @author Joseph D. Darcy
0N/A */
0N/A public static String toHexString(double d) {
0N/A /*
0N/A * Modeled after the "a" conversion specifier in C99, section
0N/A * 7.19.6.1; however, the output of this method is more
0N/A * tightly specified.
0N/A */
0N/A if (!FpUtils.isFinite(d) )
0N/A // For infinity and NaN, use the decimal output.
0N/A return Double.toString(d);
0N/A else {
0N/A // Initialized to maximum size of output.
0N/A StringBuffer answer = new StringBuffer(24);
0N/A
0N/A if (FpUtils.rawCopySign(1.0, d) == -1.0) // value is negative,
0N/A answer.append("-"); // so append sign info
0N/A
0N/A answer.append("0x");
0N/A
0N/A d = Math.abs(d);
0N/A
0N/A if(d == 0.0) {
0N/A answer.append("0.0p0");
0N/A }
0N/A else {
0N/A boolean subnormal = (d < DoubleConsts.MIN_NORMAL);
0N/A
0N/A // Isolate significand bits and OR in a high-order bit
0N/A // so that the string representation has a known
0N/A // length.
0N/A long signifBits = (Double.doubleToLongBits(d)
0N/A & DoubleConsts.SIGNIF_BIT_MASK) |
0N/A 0x1000000000000000L;
0N/A
0N/A // Subnormal values have a 0 implicit bit; normal
0N/A // values have a 1 implicit bit.
0N/A answer.append(subnormal ? "0." : "1.");
0N/A
0N/A // Isolate the low-order 13 digits of the hex
0N/A // representation. If all the digits are zero,
0N/A // replace with a single 0; otherwise, remove all
0N/A // trailing zeros.
0N/A String signif = Long.toHexString(signifBits).substring(3,16);
0N/A answer.append(signif.equals("0000000000000") ? // 13 zeros
0N/A "0":
0N/A signif.replaceFirst("0{1,12}$", ""));
0N/A
0N/A // If the value is subnormal, use the E_min exponent
0N/A // value for double; otherwise, extract and report d's
0N/A // exponent (the representation of a subnormal uses
0N/A // E_min -1).
0N/A answer.append("p" + (subnormal ?
0N/A DoubleConsts.MIN_EXPONENT:
0N/A FpUtils.getExponent(d) ));
0N/A }
0N/A return answer.toString();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a {@code Double} object holding the
0N/A * {@code double} value represented by the argument string
0N/A * {@code s}.
0N/A *
0N/A * <p>If {@code s} is {@code null}, then a
0N/A * {@code NullPointerException} is thrown.
0N/A *
0N/A * <p>Leading and trailing whitespace characters in {@code s}
0N/A * are ignored. Whitespace is removed as if by the {@link
0N/A * String#trim} method; that is, both ASCII space and control
0N/A * characters are removed. The rest of {@code s} should
0N/A * constitute a <i>FloatValue</i> as described by the lexical
0N/A * syntax rules:
0N/A *
0N/A * <blockquote>
0N/A * <dl>
0N/A * <dt><i>FloatValue:</i>
0N/A * <dd><i>Sign<sub>opt</sub></i> {@code NaN}
0N/A * <dd><i>Sign<sub>opt</sub></i> {@code Infinity}
0N/A * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
0N/A * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
0N/A * <dd><i>SignedInteger</i>
0N/A * </dl>
0N/A *
0N/A * <p>
0N/A *
0N/A * <dl>
0N/A * <dt><i>HexFloatingPointLiteral</i>:
0N/A * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
0N/A * </dl>
0N/A *
0N/A * <p>
0N/A *
0N/A * <dl>
0N/A * <dt><i>HexSignificand:</i>
0N/A * <dd><i>HexNumeral</i>
0N/A * <dd><i>HexNumeral</i> {@code .}
0N/A * <dd>{@code 0x} <i>HexDigits<sub>opt</sub>
0N/A * </i>{@code .}<i> HexDigits</i>
0N/A * <dd>{@code 0X}<i> HexDigits<sub>opt</sub>
0N/A * </i>{@code .} <i>HexDigits</i>
0N/A * </dl>
0N/A *
0N/A * <p>
0N/A *
0N/A * <dl>
0N/A * <dt><i>BinaryExponent:</i>
0N/A * <dd><i>BinaryExponentIndicator SignedInteger</i>
0N/A * </dl>
0N/A *
0N/A * <p>
0N/A *
0N/A * <dl>
0N/A * <dt><i>BinaryExponentIndicator:</i>
0N/A * <dd>{@code p}
0N/A * <dd>{@code P}
0N/A * </dl>
0N/A *
0N/A * </blockquote>
0N/A *
0N/A * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
0N/A * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
0N/A * <i>FloatTypeSuffix</i> are as defined in the lexical structure
4008N/A * sections of
4008N/A * <cite>The Java&trade; Language Specification</cite>,
4008N/A * except that underscores are not accepted between digits.
4008N/A * If {@code s} does not have the form of
0N/A * a <i>FloatValue</i>, then a {@code NumberFormatException}
0N/A * is thrown. Otherwise, {@code s} is regarded as
0N/A * representing an exact decimal value in the usual
0N/A * "computerized scientific notation" or as an exact
0N/A * hexadecimal value; this exact numerical value is then
0N/A * conceptually converted to an "infinitely precise"
0N/A * binary value that is then rounded to type {@code double}
0N/A * by the usual round-to-nearest rule of IEEE 754 floating-point
0N/A * arithmetic, which includes preserving the sign of a zero
807N/A * value.
807N/A *
807N/A * Note that the round-to-nearest rule also implies overflow and
807N/A * underflow behaviour; if the exact value of {@code s} is large
807N/A * enough in magnitude (greater than or equal to ({@link
807N/A * #MAX_VALUE} + {@link Math#ulp(double) ulp(MAX_VALUE)}/2),
807N/A * rounding to {@code double} will result in an infinity and if the
807N/A * exact value of {@code s} is small enough in magnitude (less
807N/A * than or equal to {@link #MIN_VALUE}/2), rounding to float will
807N/A * result in a zero.
807N/A *
807N/A * Finally, after rounding a {@code Double} object representing
807N/A * this {@code double} value is returned.
0N/A *
0N/A * <p> To interpret localized string representations of a
0N/A * floating-point value, use subclasses of {@link
0N/A * java.text.NumberFormat}.
0N/A *
0N/A * <p>Note that trailing format specifiers, specifiers that
0N/A * determine the type of a floating-point literal
0N/A * ({@code 1.0f} is a {@code float} value;
0N/A * {@code 1.0d} is a {@code double} value), do
0N/A * <em>not</em> influence the results of this method. In other
0N/A * words, the numerical value of the input string is converted
0N/A * directly to the target floating-point type. The two-step
0N/A * sequence of conversions, string to {@code float} followed
0N/A * by {@code float} to {@code double}, is <em>not</em>
0N/A * equivalent to converting a string directly to
0N/A * {@code double}. For example, the {@code float}
0N/A * literal {@code 0.1f} is equal to the {@code double}
0N/A * value {@code 0.10000000149011612}; the {@code float}
0N/A * literal {@code 0.1f} represents a different numerical
0N/A * value than the {@code double} literal
0N/A * {@code 0.1}. (The numerical value 0.1 cannot be exactly
0N/A * represented in a binary floating-point number.)
0N/A *
0N/A * <p>To avoid calling this method on an invalid string and having
0N/A * a {@code NumberFormatException} be thrown, the regular
0N/A * expression below can be used to screen the input string:
0N/A *
0N/A * <code>
0N/A * <pre>
0N/A * final String Digits = "(\\p{Digit}+)";
0N/A * final String HexDigits = "(\\p{XDigit}+)";
0N/A * // an exponent is 'e' or 'E' followed by an optionally
0N/A * // signed decimal integer.
0N/A * final String Exp = "[eE][+-]?"+Digits;
0N/A * final String fpRegex =
0N/A * ("[\\x00-\\x20]*"+ // Optional leading "whitespace"
0N/A * "[+-]?(" + // Optional sign character
0N/A * "NaN|" + // "NaN" string
0N/A * "Infinity|" + // "Infinity" string
0N/A *
0N/A * // A decimal floating-point string representing a finite positive
0N/A * // number without a leading sign has at most five basic pieces:
0N/A * // Digits . Digits ExponentPart FloatTypeSuffix
0N/A * //
0N/A * // Since this method allows integer-only strings as input
0N/A * // in addition to strings of floating-point literals, the
0N/A * // two sub-patterns below are simplifications of the grammar
4008N/A * // productions from section 3.10.2 of
4008N/A * // <cite>The Java&trade; Language Specification</cite>.
0N/A *
0N/A * // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
0N/A * "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
0N/A *
0N/A * // . Digits ExponentPart_opt FloatTypeSuffix_opt
0N/A * "(\\.("+Digits+")("+Exp+")?)|"+
0N/A *
0N/A * // Hexadecimal strings
0N/A * "((" +
0N/A * // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
0N/A * "(0[xX]" + HexDigits + "(\\.)?)|" +
0N/A *
0N/A * // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
0N/A * "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
0N/A *
0N/A * ")[pP][+-]?" + Digits + "))" +
0N/A * "[fFdD]?))" +
0N/A * "[\\x00-\\x20]*");// Optional trailing "whitespace"
0N/A *
0N/A * if (Pattern.matches(fpRegex, myString))
0N/A * Double.valueOf(myString); // Will not throw NumberFormatException
0N/A * else {
0N/A * // Perform suitable alternative action
0N/A * }
0N/A * </pre>
0N/A * </code>
0N/A *
0N/A * @param s the string to be parsed.
0N/A * @return a {@code Double} object holding the value
0N/A * represented by the {@code String} argument.
0N/A * @throws NumberFormatException if the string does not contain a
0N/A * parsable number.
0N/A */
0N/A public static Double valueOf(String s) throws NumberFormatException {
0N/A return new Double(FloatingDecimal.readJavaFormatString(s).doubleValue());
0N/A }
0N/A
0N/A /**
0N/A * Returns a {@code Double} instance representing the specified
0N/A * {@code double} value.
0N/A * If a new {@code Double} instance is not required, this method
0N/A * should generally be used in preference to the constructor
0N/A * {@link #Double(double)}, as this method is likely to yield
0N/A * significantly better space and time performance by caching
0N/A * frequently requested values.
0N/A *
0N/A * @param d a double value.
0N/A * @return a {@code Double} instance representing {@code d}.
0N/A * @since 1.5
0N/A */
0N/A public static Double valueOf(double d) {
0N/A return new Double(d);
0N/A }
0N/A
0N/A /**
0N/A * Returns a new {@code double} initialized to the value
0N/A * represented by the specified {@code String}, as performed
0N/A * by the {@code valueOf} method of class
0N/A * {@code Double}.
0N/A *
0N/A * @param s the string to be parsed.
0N/A * @return the {@code double} value represented by the string
0N/A * argument.
1419N/A * @throws NullPointerException if the string is null
0N/A * @throws NumberFormatException if the string does not contain
0N/A * a parsable {@code double}.
0N/A * @see java.lang.Double#valueOf(String)
0N/A * @since 1.2
0N/A */
0N/A public static double parseDouble(String s) throws NumberFormatException {
0N/A return FloatingDecimal.readJavaFormatString(s).doubleValue();
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if the specified number is a
0N/A * Not-a-Number (NaN) value, {@code false} otherwise.
0N/A *
0N/A * @param v the value to be tested.
0N/A * @return {@code true} if the value of the argument is NaN;
0N/A * {@code false} otherwise.
0N/A */
0N/A static public boolean isNaN(double v) {
0N/A return (v != v);
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if the specified number is infinitely
0N/A * large in magnitude, {@code false} otherwise.
0N/A *
0N/A * @param v the value to be tested.
0N/A * @return {@code true} if the value of the argument is positive
0N/A * infinity or negative infinity; {@code false} otherwise.
0N/A */
0N/A static public boolean isInfinite(double v) {
0N/A return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
0N/A }
0N/A
0N/A /**
0N/A * The value of the Double.
0N/A *
0N/A * @serial
0N/A */
0N/A private final double value;
0N/A
0N/A /**
0N/A * Constructs a newly allocated {@code Double} object that
0N/A * represents the primitive {@code double} argument.
0N/A *
0N/A * @param value the value to be represented by the {@code Double}.
0N/A */
0N/A public Double(double value) {
0N/A this.value = value;
0N/A }
0N/A
0N/A /**
0N/A * Constructs a newly allocated {@code Double} object that
0N/A * represents the floating-point value of type {@code double}
0N/A * represented by the string. The string is converted to a
0N/A * {@code double} value as if by the {@code valueOf} method.
0N/A *
0N/A * @param s a string to be converted to a {@code Double}.
0N/A * @throws NumberFormatException if the string does not contain a
0N/A * parsable number.
0N/A * @see java.lang.Double#valueOf(java.lang.String)
0N/A */
0N/A public Double(String s) throws NumberFormatException {
0N/A // REMIND: this is inefficient
0N/A this(valueOf(s).doubleValue());
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if this {@code Double} value is
0N/A * a Not-a-Number (NaN), {@code false} otherwise.
0N/A *
0N/A * @return {@code true} if the value represented by this object is
0N/A * NaN; {@code false} otherwise.
0N/A */
0N/A public boolean isNaN() {
0N/A return isNaN(value);
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if this {@code Double} value is
0N/A * infinitely large in magnitude, {@code false} otherwise.
0N/A *
0N/A * @return {@code true} if the value represented by this object is
0N/A * positive infinity or negative infinity;
0N/A * {@code false} otherwise.
0N/A */
0N/A public boolean isInfinite() {
0N/A return isInfinite(value);
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of this {@code Double} object.
0N/A * The primitive {@code double} value represented by this
0N/A * object is converted to a string exactly as if by the method
0N/A * {@code toString} of one argument.
0N/A *
0N/A * @return a {@code String} representation of this object.
0N/A * @see java.lang.Double#toString(double)
0N/A */
0N/A public String toString() {
1722N/A return toString(value);
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Double} as a {@code byte} (by
0N/A * casting to a {@code byte}).
0N/A *
0N/A * @return the {@code double} value represented by this object
0N/A * converted to type {@code byte}
0N/A * @since JDK1.1
0N/A */
0N/A public byte byteValue() {
0N/A return (byte)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Double} as a
0N/A * {@code short} (by casting to a {@code short}).
0N/A *
0N/A * @return the {@code double} value represented by this object
0N/A * converted to type {@code short}
0N/A * @since JDK1.1
0N/A */
0N/A public short shortValue() {
0N/A return (short)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Double} as an
0N/A * {@code int} (by casting to type {@code int}).
0N/A *
0N/A * @return the {@code double} value represented by this object
0N/A * converted to type {@code int}
0N/A */
0N/A public int intValue() {
0N/A return (int)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Double} as a
0N/A * {@code long} (by casting to type {@code long}).
0N/A *
0N/A * @return the {@code double} value represented by this object
0N/A * converted to type {@code long}
0N/A */
0N/A public long longValue() {
0N/A return (long)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the {@code float} value of this
0N/A * {@code Double} object.
0N/A *
0N/A * @return the {@code double} value represented by this object
0N/A * converted to type {@code float}
0N/A * @since JDK1.0
0N/A */
0N/A public float floatValue() {
0N/A return (float)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the {@code double} value of this
0N/A * {@code Double} object.
0N/A *
0N/A * @return the {@code double} value represented by this object
0N/A */
0N/A public double doubleValue() {
0N/A return (double)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns a hash code for this {@code Double} object. The
0N/A * result is the exclusive OR of the two halves of the
0N/A * {@code long} integer bit representation, exactly as
0N/A * produced by the method {@link #doubleToLongBits(double)}, of
0N/A * the primitive {@code double} value represented by this
0N/A * {@code Double} object. That is, the hash code is the value
0N/A * of the expression:
0N/A *
0N/A * <blockquote>
0N/A * {@code (int)(v^(v>>>32))}
0N/A * </blockquote>
0N/A *
0N/A * where {@code v} is defined by:
0N/A *
0N/A * <blockquote>
0N/A * {@code long v = Double.doubleToLongBits(this.doubleValue());}
0N/A * </blockquote>
0N/A *
0N/A * @return a {@code hash code} value for this object.
0N/A */
0N/A public int hashCode() {
0N/A long bits = doubleToLongBits(value);
0N/A return (int)(bits ^ (bits >>> 32));
0N/A }
0N/A
0N/A /**
0N/A * Compares this object against the specified object. The result
0N/A * is {@code true} if and only if the argument is not
0N/A * {@code null} and is a {@code Double} object that
0N/A * represents a {@code double} that has the same value as the
0N/A * {@code double} represented by this object. For this
0N/A * purpose, two {@code double} values are considered to be
0N/A * the same if and only if the method {@link
0N/A * #doubleToLongBits(double)} returns the identical
0N/A * {@code long} value when applied to each.
0N/A *
0N/A * <p>Note that in most cases, for two instances of class
0N/A * {@code Double}, {@code d1} and {@code d2}, the
0N/A * value of {@code d1.equals(d2)} is {@code true} if and
0N/A * only if
0N/A *
0N/A * <blockquote>
0N/A * {@code d1.doubleValue() == d2.doubleValue()}
0N/A * </blockquote>
0N/A *
0N/A * <p>also has the value {@code true}. However, there are two
0N/A * exceptions:
0N/A * <ul>
0N/A * <li>If {@code d1} and {@code d2} both represent
0N/A * {@code Double.NaN}, then the {@code equals} method
0N/A * returns {@code true}, even though
0N/A * {@code Double.NaN==Double.NaN} has the value
0N/A * {@code false}.
0N/A * <li>If {@code d1} represents {@code +0.0} while
0N/A * {@code d2} represents {@code -0.0}, or vice versa,
0N/A * the {@code equal} test has the value {@code false},
0N/A * even though {@code +0.0==-0.0} has the value {@code true}.
0N/A * </ul>
0N/A * This definition allows hash tables to operate properly.
0N/A * @param obj the object to compare with.
0N/A * @return {@code true} if the objects are the same;
0N/A * {@code false} otherwise.
0N/A * @see java.lang.Double#doubleToLongBits(double)
0N/A */
0N/A public boolean equals(Object obj) {
0N/A return (obj instanceof Double)
0N/A && (doubleToLongBits(((Double)obj).value) ==
0N/A doubleToLongBits(value));
0N/A }
0N/A
0N/A /**
0N/A * Returns a representation of the specified floating-point value
0N/A * according to the IEEE 754 floating-point "double
0N/A * format" bit layout.
0N/A *
0N/A * <p>Bit 63 (the bit that is selected by the mask
0N/A * {@code 0x8000000000000000L}) represents the sign of the
0N/A * floating-point number. Bits
0N/A * 62-52 (the bits that are selected by the mask
0N/A * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
0N/A * (the bits that are selected by the mask
0N/A * {@code 0x000fffffffffffffL}) represent the significand
0N/A * (sometimes called the mantissa) of the floating-point number.
0N/A *
0N/A * <p>If the argument is positive infinity, the result is
0N/A * {@code 0x7ff0000000000000L}.
0N/A *
0N/A * <p>If the argument is negative infinity, the result is
0N/A * {@code 0xfff0000000000000L}.
0N/A *
0N/A * <p>If the argument is NaN, the result is
0N/A * {@code 0x7ff8000000000000L}.
0N/A *
0N/A * <p>In all cases, the result is a {@code long} integer that, when
0N/A * given to the {@link #longBitsToDouble(long)} method, will produce a
0N/A * floating-point value the same as the argument to
0N/A * {@code doubleToLongBits} (except all NaN values are
0N/A * collapsed to a single "canonical" NaN value).
0N/A *
0N/A * @param value a {@code double} precision floating-point number.
0N/A * @return the bits that represent the floating-point number.
0N/A */
0N/A public static long doubleToLongBits(double value) {
0N/A long result = doubleToRawLongBits(value);
0N/A // Check for NaN based on values of bit fields, maximum
0N/A // exponent and nonzero significand.
0N/A if ( ((result & DoubleConsts.EXP_BIT_MASK) ==
0N/A DoubleConsts.EXP_BIT_MASK) &&
0N/A (result & DoubleConsts.SIGNIF_BIT_MASK) != 0L)
0N/A result = 0x7ff8000000000000L;
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Returns a representation of the specified floating-point value
0N/A * according to the IEEE 754 floating-point "double
0N/A * format" bit layout, preserving Not-a-Number (NaN) values.
0N/A *
0N/A * <p>Bit 63 (the bit that is selected by the mask
0N/A * {@code 0x8000000000000000L}) represents the sign of the
0N/A * floating-point number. Bits
0N/A * 62-52 (the bits that are selected by the mask
0N/A * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
0N/A * (the bits that are selected by the mask
0N/A * {@code 0x000fffffffffffffL}) represent the significand
0N/A * (sometimes called the mantissa) of the floating-point number.
0N/A *
0N/A * <p>If the argument is positive infinity, the result is
0N/A * {@code 0x7ff0000000000000L}.
0N/A *
0N/A * <p>If the argument is negative infinity, the result is
0N/A * {@code 0xfff0000000000000L}.
0N/A *
0N/A * <p>If the argument is NaN, the result is the {@code long}
0N/A * integer representing the actual NaN value. Unlike the
0N/A * {@code doubleToLongBits} method,
0N/A * {@code doubleToRawLongBits} does not collapse all the bit
0N/A * patterns encoding a NaN to a single "canonical" NaN
0N/A * value.
0N/A *
0N/A * <p>In all cases, the result is a {@code long} integer that,
0N/A * when given to the {@link #longBitsToDouble(long)} method, will
0N/A * produce a floating-point value the same as the argument to
0N/A * {@code doubleToRawLongBits}.
0N/A *
0N/A * @param value a {@code double} precision floating-point number.
0N/A * @return the bits that represent the floating-point number.
0N/A * @since 1.3
0N/A */
0N/A public static native long doubleToRawLongBits(double value);
0N/A
0N/A /**
0N/A * Returns the {@code double} value corresponding to a given
0N/A * bit representation.
0N/A * The argument is considered to be a representation of a
0N/A * floating-point value according to the IEEE 754 floating-point
0N/A * "double format" bit layout.
0N/A *
0N/A * <p>If the argument is {@code 0x7ff0000000000000L}, the result
0N/A * is positive infinity.
0N/A *
0N/A * <p>If the argument is {@code 0xfff0000000000000L}, the result
0N/A * is negative infinity.
0N/A *
0N/A * <p>If the argument is any value in the range
0N/A * {@code 0x7ff0000000000001L} through
0N/A * {@code 0x7fffffffffffffffL} or in the range
0N/A * {@code 0xfff0000000000001L} through
0N/A * {@code 0xffffffffffffffffL}, the result is a NaN. No IEEE
0N/A * 754 floating-point operation provided by Java can distinguish
0N/A * between two NaN values of the same type with different bit
0N/A * patterns. Distinct values of NaN are only distinguishable by
0N/A * use of the {@code Double.doubleToRawLongBits} method.
0N/A *
0N/A * <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
0N/A * values that can be computed from the argument:
0N/A *
0N/A * <blockquote><pre>
0N/A * int s = ((bits &gt;&gt; 63) == 0) ? 1 : -1;
0N/A * int e = (int)((bits &gt;&gt; 52) & 0x7ffL);
0N/A * long m = (e == 0) ?
0N/A * (bits & 0xfffffffffffffL) &lt;&lt; 1 :
0N/A * (bits & 0xfffffffffffffL) | 0x10000000000000L;
0N/A * </pre></blockquote>
0N/A *
0N/A * Then the floating-point result equals the value of the mathematical
0N/A * expression <i>s</i>&middot;<i>m</i>&middot;2<sup><i>e</i>-1075</sup>.
0N/A *
0N/A * <p>Note that this method may not be able to return a
0N/A * {@code double} NaN with exactly same bit pattern as the
0N/A * {@code long} argument. IEEE 754 distinguishes between two
0N/A * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The
0N/A * differences between the two kinds of NaN are generally not
0N/A * visible in Java. Arithmetic operations on signaling NaNs turn
0N/A * them into quiet NaNs with a different, but often similar, bit
0N/A * pattern. However, on some processors merely copying a
0N/A * signaling NaN also performs that conversion. In particular,
0N/A * copying a signaling NaN to return it to the calling method
0N/A * may perform this conversion. So {@code longBitsToDouble}
0N/A * may not be able to return a {@code double} with a
0N/A * signaling NaN bit pattern. Consequently, for some
0N/A * {@code long} values,
0N/A * {@code doubleToRawLongBits(longBitsToDouble(start))} may
0N/A * <i>not</i> equal {@code start}. Moreover, which
0N/A * particular bit patterns represent signaling NaNs is platform
0N/A * dependent; although all NaN bit patterns, quiet or signaling,
0N/A * must be in the NaN range identified above.
0N/A *
0N/A * @param bits any {@code long} integer.
0N/A * @return the {@code double} floating-point value with the same
0N/A * bit pattern.
0N/A */
0N/A public static native double longBitsToDouble(long bits);
0N/A
0N/A /**
0N/A * Compares two {@code Double} objects numerically. There
0N/A * are two ways in which comparisons performed by this method
0N/A * differ from those performed by the Java language numerical
0N/A * comparison operators ({@code <, <=, ==, >=, >})
0N/A * when applied to primitive {@code double} values:
0N/A * <ul><li>
0N/A * {@code Double.NaN} is considered by this method
0N/A * to be equal to itself and greater than all other
0N/A * {@code double} values (including
0N/A * {@code Double.POSITIVE_INFINITY}).
0N/A * <li>
0N/A * {@code 0.0d} is considered by this method to be greater
0N/A * than {@code -0.0d}.
0N/A * </ul>
0N/A * This ensures that the <i>natural ordering</i> of
0N/A * {@code Double} objects imposed by this method is <i>consistent
0N/A * with equals</i>.
0N/A *
0N/A * @param anotherDouble the {@code Double} to be compared.
0N/A * @return the value {@code 0} if {@code anotherDouble} is
0N/A * numerically equal to this {@code Double}; a value
0N/A * less than {@code 0} if this {@code Double}
0N/A * is numerically less than {@code anotherDouble};
0N/A * and a value greater than {@code 0} if this
0N/A * {@code Double} is numerically greater than
0N/A * {@code anotherDouble}.
0N/A *
0N/A * @since 1.2
0N/A */
0N/A public int compareTo(Double anotherDouble) {
0N/A return Double.compare(value, anotherDouble.value);
0N/A }
0N/A
0N/A /**
0N/A * Compares the two specified {@code double} values. The sign
0N/A * of the integer value returned is the same as that of the
0N/A * integer that would be returned by the call:
0N/A * <pre>
0N/A * new Double(d1).compareTo(new Double(d2))
0N/A * </pre>
0N/A *
0N/A * @param d1 the first {@code double} to compare
0N/A * @param d2 the second {@code double} to compare
0N/A * @return the value {@code 0} if {@code d1} is
0N/A * numerically equal to {@code d2}; a value less than
0N/A * {@code 0} if {@code d1} is numerically less than
0N/A * {@code d2}; and a value greater than {@code 0}
0N/A * if {@code d1} is numerically greater than
0N/A * {@code d2}.
0N/A * @since 1.4
0N/A */
0N/A public static int compare(double d1, double d2) {
0N/A if (d1 < d2)
0N/A return -1; // Neither val is NaN, thisVal is smaller
0N/A if (d1 > d2)
0N/A return 1; // Neither val is NaN, thisVal is larger
0N/A
3202N/A // Cannot use doubleToRawLongBits because of possibility of NaNs.
3202N/A long thisBits = Double.doubleToLongBits(d1);
0N/A long anotherBits = Double.doubleToLongBits(d2);
0N/A
0N/A return (thisBits == anotherBits ? 0 : // Values are equal
0N/A (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
0N/A 1)); // (0.0, -0.0) or (NaN, !NaN)
0N/A }
0N/A
0N/A /** use serialVersionUID from JDK 1.0.2 for interoperability */
0N/A private static final long serialVersionUID = -9172774392245257468L;
0N/A}