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