1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/src/main/java/java/lang/Double.java Sat Sep 29 10:56:23 2012 +0200
1.3 @@ -0,0 +1,988 @@
1.4 +/*
1.5 + * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.lang;
1.30 +
1.31 +import sun.misc.FloatingDecimal;
1.32 +import sun.misc.FpUtils;
1.33 +import sun.misc.DoubleConsts;
1.34 +
1.35 +/**
1.36 + * The {@code Double} class wraps a value of the primitive type
1.37 + * {@code double} in an object. An object of type
1.38 + * {@code Double} contains a single field whose type is
1.39 + * {@code double}.
1.40 + *
1.41 + * <p>In addition, this class provides several methods for converting a
1.42 + * {@code double} to a {@code String} and a
1.43 + * {@code String} to a {@code double}, as well as other
1.44 + * constants and methods useful when dealing with a
1.45 + * {@code double}.
1.46 + *
1.47 + * @author Lee Boynton
1.48 + * @author Arthur van Hoff
1.49 + * @author Joseph D. Darcy
1.50 + * @since JDK1.0
1.51 + */
1.52 +public final class Double extends Number implements Comparable<Double> {
1.53 + /**
1.54 + * A constant holding the positive infinity of type
1.55 + * {@code double}. It is equal to the value returned by
1.56 + * {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
1.57 + */
1.58 + public static final double POSITIVE_INFINITY = 1.0 / 0.0;
1.59 +
1.60 + /**
1.61 + * A constant holding the negative infinity of type
1.62 + * {@code double}. It is equal to the value returned by
1.63 + * {@code Double.longBitsToDouble(0xfff0000000000000L)}.
1.64 + */
1.65 + public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
1.66 +
1.67 + /**
1.68 + * A constant holding a Not-a-Number (NaN) value of type
1.69 + * {@code double}. It is equivalent to the value returned by
1.70 + * {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
1.71 + */
1.72 + public static final double NaN = 0.0d / 0.0;
1.73 +
1.74 + /**
1.75 + * A constant holding the largest positive finite value of type
1.76 + * {@code double},
1.77 + * (2-2<sup>-52</sup>)·2<sup>1023</sup>. It is equal to
1.78 + * the hexadecimal floating-point literal
1.79 + * {@code 0x1.fffffffffffffP+1023} and also equal to
1.80 + * {@code Double.longBitsToDouble(0x7fefffffffffffffL)}.
1.81 + */
1.82 + public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308
1.83 +
1.84 + /**
1.85 + * A constant holding the smallest positive normal value of type
1.86 + * {@code double}, 2<sup>-1022</sup>. It is equal to the
1.87 + * hexadecimal floating-point literal {@code 0x1.0p-1022} and also
1.88 + * equal to {@code Double.longBitsToDouble(0x0010000000000000L)}.
1.89 + *
1.90 + * @since 1.6
1.91 + */
1.92 + public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308
1.93 +
1.94 + /**
1.95 + * A constant holding the smallest positive nonzero value of type
1.96 + * {@code double}, 2<sup>-1074</sup>. It is equal to the
1.97 + * hexadecimal floating-point literal
1.98 + * {@code 0x0.0000000000001P-1022} and also equal to
1.99 + * {@code Double.longBitsToDouble(0x1L)}.
1.100 + */
1.101 + public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324
1.102 +
1.103 + /**
1.104 + * Maximum exponent a finite {@code double} variable may have.
1.105 + * It is equal to the value returned by
1.106 + * {@code Math.getExponent(Double.MAX_VALUE)}.
1.107 + *
1.108 + * @since 1.6
1.109 + */
1.110 + public static final int MAX_EXPONENT = 1023;
1.111 +
1.112 + /**
1.113 + * Minimum exponent a normalized {@code double} variable may
1.114 + * have. It is equal to the value returned by
1.115 + * {@code Math.getExponent(Double.MIN_NORMAL)}.
1.116 + *
1.117 + * @since 1.6
1.118 + */
1.119 + public static final int MIN_EXPONENT = -1022;
1.120 +
1.121 + /**
1.122 + * The number of bits used to represent a {@code double} value.
1.123 + *
1.124 + * @since 1.5
1.125 + */
1.126 + public static final int SIZE = 64;
1.127 +
1.128 + /**
1.129 + * The {@code Class} instance representing the primitive type
1.130 + * {@code double}.
1.131 + *
1.132 + * @since JDK1.1
1.133 + */
1.134 + public static final Class<Double> TYPE = (Class<Double>) Class.getPrimitiveClass("double");
1.135 +
1.136 + /**
1.137 + * Returns a string representation of the {@code double}
1.138 + * argument. All characters mentioned below are ASCII characters.
1.139 + * <ul>
1.140 + * <li>If the argument is NaN, the result is the string
1.141 + * "{@code NaN}".
1.142 + * <li>Otherwise, the result is a string that represents the sign and
1.143 + * magnitude (absolute value) of the argument. If the sign is negative,
1.144 + * the first character of the result is '{@code -}'
1.145 + * (<code>'\u002D'</code>); if the sign is positive, no sign character
1.146 + * appears in the result. As for the magnitude <i>m</i>:
1.147 + * <ul>
1.148 + * <li>If <i>m</i> is infinity, it is represented by the characters
1.149 + * {@code "Infinity"}; thus, positive infinity produces the result
1.150 + * {@code "Infinity"} and negative infinity produces the result
1.151 + * {@code "-Infinity"}.
1.152 + *
1.153 + * <li>If <i>m</i> is zero, it is represented by the characters
1.154 + * {@code "0.0"}; thus, negative zero produces the result
1.155 + * {@code "-0.0"} and positive zero produces the result
1.156 + * {@code "0.0"}.
1.157 + *
1.158 + * <li>If <i>m</i> is greater than or equal to 10<sup>-3</sup> but less
1.159 + * than 10<sup>7</sup>, then it is represented as the integer part of
1.160 + * <i>m</i>, in decimal form with no leading zeroes, followed by
1.161 + * '{@code .}' (<code>'\u002E'</code>), followed by one or
1.162 + * more decimal digits representing the fractional part of <i>m</i>.
1.163 + *
1.164 + * <li>If <i>m</i> is less than 10<sup>-3</sup> or greater than or
1.165 + * equal to 10<sup>7</sup>, then it is represented in so-called
1.166 + * "computerized scientific notation." Let <i>n</i> be the unique
1.167 + * integer such that 10<sup><i>n</i></sup> ≤ <i>m</i> {@literal <}
1.168 + * 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
1.169 + * mathematically exact quotient of <i>m</i> and
1.170 + * 10<sup><i>n</i></sup> so that 1 ≤ <i>a</i> {@literal <} 10. The
1.171 + * magnitude is then represented as the integer part of <i>a</i>,
1.172 + * as a single decimal digit, followed by '{@code .}'
1.173 + * (<code>'\u002E'</code>), followed by decimal digits
1.174 + * representing the fractional part of <i>a</i>, followed by the
1.175 + * letter '{@code E}' (<code>'\u0045'</code>), followed
1.176 + * by a representation of <i>n</i> as a decimal integer, as
1.177 + * produced by the method {@link Integer#toString(int)}.
1.178 + * </ul>
1.179 + * </ul>
1.180 + * How many digits must be printed for the fractional part of
1.181 + * <i>m</i> or <i>a</i>? There must be at least one digit to represent
1.182 + * the fractional part, and beyond that as many, but only as many, more
1.183 + * digits as are needed to uniquely distinguish the argument value from
1.184 + * adjacent values of type {@code double}. That is, suppose that
1.185 + * <i>x</i> is the exact mathematical value represented by the decimal
1.186 + * representation produced by this method for a finite nonzero argument
1.187 + * <i>d</i>. Then <i>d</i> must be the {@code double} value nearest
1.188 + * to <i>x</i>; or if two {@code double} values are equally close
1.189 + * to <i>x</i>, then <i>d</i> must be one of them and the least
1.190 + * significant bit of the significand of <i>d</i> must be {@code 0}.
1.191 + *
1.192 + * <p>To create localized string representations of a floating-point
1.193 + * value, use subclasses of {@link java.text.NumberFormat}.
1.194 + *
1.195 + * @param d the {@code double} to be converted.
1.196 + * @return a string representation of the argument.
1.197 + */
1.198 + public static String toString(double d) {
1.199 + return new FloatingDecimal(d).toJavaFormatString();
1.200 + }
1.201 +
1.202 + /**
1.203 + * Returns a hexadecimal string representation of the
1.204 + * {@code double} argument. All characters mentioned below
1.205 + * are ASCII characters.
1.206 + *
1.207 + * <ul>
1.208 + * <li>If the argument is NaN, the result is the string
1.209 + * "{@code NaN}".
1.210 + * <li>Otherwise, the result is a string that represents the sign
1.211 + * and magnitude of the argument. If the sign is negative, the
1.212 + * first character of the result is '{@code -}'
1.213 + * (<code>'\u002D'</code>); if the sign is positive, no sign
1.214 + * character appears in the result. As for the magnitude <i>m</i>:
1.215 + *
1.216 + * <ul>
1.217 + * <li>If <i>m</i> is infinity, it is represented by the string
1.218 + * {@code "Infinity"}; thus, positive infinity produces the
1.219 + * result {@code "Infinity"} and negative infinity produces
1.220 + * the result {@code "-Infinity"}.
1.221 + *
1.222 + * <li>If <i>m</i> is zero, it is represented by the string
1.223 + * {@code "0x0.0p0"}; thus, negative zero produces the result
1.224 + * {@code "-0x0.0p0"} and positive zero produces the result
1.225 + * {@code "0x0.0p0"}.
1.226 + *
1.227 + * <li>If <i>m</i> is a {@code double} value with a
1.228 + * normalized representation, substrings are used to represent the
1.229 + * significand and exponent fields. The significand is
1.230 + * represented by the characters {@code "0x1."}
1.231 + * followed by a lowercase hexadecimal representation of the rest
1.232 + * of the significand as a fraction. Trailing zeros in the
1.233 + * hexadecimal representation are removed unless all the digits
1.234 + * are zero, in which case a single zero is used. Next, the
1.235 + * exponent is represented by {@code "p"} followed
1.236 + * by a decimal string of the unbiased exponent as if produced by
1.237 + * a call to {@link Integer#toString(int) Integer.toString} on the
1.238 + * exponent value.
1.239 + *
1.240 + * <li>If <i>m</i> is a {@code double} value with a subnormal
1.241 + * representation, the significand is represented by the
1.242 + * characters {@code "0x0."} followed by a
1.243 + * hexadecimal representation of the rest of the significand as a
1.244 + * fraction. Trailing zeros in the hexadecimal representation are
1.245 + * removed. Next, the exponent is represented by
1.246 + * {@code "p-1022"}. Note that there must be at
1.247 + * least one nonzero digit in a subnormal significand.
1.248 + *
1.249 + * </ul>
1.250 + *
1.251 + * </ul>
1.252 + *
1.253 + * <table border>
1.254 + * <caption><h3>Examples</h3></caption>
1.255 + * <tr><th>Floating-point Value</th><th>Hexadecimal String</th>
1.256 + * <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td>
1.257 + * <tr><td>{@code -1.0}</td> <td>{@code -0x1.0p0}</td>
1.258 + * <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>
1.259 + * <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>
1.260 + * <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>
1.261 + * <tr><td>{@code 0.25}</td> <td>{@code 0x1.0p-2}</td>
1.262 + * <tr><td>{@code Double.MAX_VALUE}</td>
1.263 + * <td>{@code 0x1.fffffffffffffp1023}</td>
1.264 + * <tr><td>{@code Minimum Normal Value}</td>
1.265 + * <td>{@code 0x1.0p-1022}</td>
1.266 + * <tr><td>{@code Maximum Subnormal Value}</td>
1.267 + * <td>{@code 0x0.fffffffffffffp-1022}</td>
1.268 + * <tr><td>{@code Double.MIN_VALUE}</td>
1.269 + * <td>{@code 0x0.0000000000001p-1022}</td>
1.270 + * </table>
1.271 + * @param d the {@code double} to be converted.
1.272 + * @return a hex string representation of the argument.
1.273 + * @since 1.5
1.274 + * @author Joseph D. Darcy
1.275 + */
1.276 + public static String toHexString(double d) {
1.277 + /*
1.278 + * Modeled after the "a" conversion specifier in C99, section
1.279 + * 7.19.6.1; however, the output of this method is more
1.280 + * tightly specified.
1.281 + */
1.282 + if (!FpUtils.isFinite(d) )
1.283 + // For infinity and NaN, use the decimal output.
1.284 + return Double.toString(d);
1.285 + else {
1.286 + // Initialized to maximum size of output.
1.287 + StringBuffer answer = new StringBuffer(24);
1.288 +
1.289 + if (FpUtils.rawCopySign(1.0, d) == -1.0) // value is negative,
1.290 + answer.append("-"); // so append sign info
1.291 +
1.292 + answer.append("0x");
1.293 +
1.294 + d = Math.abs(d);
1.295 +
1.296 + if(d == 0.0) {
1.297 + answer.append("0.0p0");
1.298 + }
1.299 + else {
1.300 + boolean subnormal = (d < DoubleConsts.MIN_NORMAL);
1.301 +
1.302 + // Isolate significand bits and OR in a high-order bit
1.303 + // so that the string representation has a known
1.304 + // length.
1.305 + long signifBits = (Double.doubleToLongBits(d)
1.306 + & DoubleConsts.SIGNIF_BIT_MASK) |
1.307 + 0x1000000000000000L;
1.308 +
1.309 + // Subnormal values have a 0 implicit bit; normal
1.310 + // values have a 1 implicit bit.
1.311 + answer.append(subnormal ? "0." : "1.");
1.312 +
1.313 + // Isolate the low-order 13 digits of the hex
1.314 + // representation. If all the digits are zero,
1.315 + // replace with a single 0; otherwise, remove all
1.316 + // trailing zeros.
1.317 + String signif = Long.toHexString(signifBits).substring(3,16);
1.318 + answer.append(signif.equals("0000000000000") ? // 13 zeros
1.319 + "0":
1.320 + signif.replaceFirst("0{1,12}$", ""));
1.321 +
1.322 + // If the value is subnormal, use the E_min exponent
1.323 + // value for double; otherwise, extract and report d's
1.324 + // exponent (the representation of a subnormal uses
1.325 + // E_min -1).
1.326 + answer.append("p" + (subnormal ?
1.327 + DoubleConsts.MIN_EXPONENT:
1.328 + FpUtils.getExponent(d) ));
1.329 + }
1.330 + return answer.toString();
1.331 + }
1.332 + }
1.333 +
1.334 + /**
1.335 + * Returns a {@code Double} object holding the
1.336 + * {@code double} value represented by the argument string
1.337 + * {@code s}.
1.338 + *
1.339 + * <p>If {@code s} is {@code null}, then a
1.340 + * {@code NullPointerException} is thrown.
1.341 + *
1.342 + * <p>Leading and trailing whitespace characters in {@code s}
1.343 + * are ignored. Whitespace is removed as if by the {@link
1.344 + * String#trim} method; that is, both ASCII space and control
1.345 + * characters are removed. The rest of {@code s} should
1.346 + * constitute a <i>FloatValue</i> as described by the lexical
1.347 + * syntax rules:
1.348 + *
1.349 + * <blockquote>
1.350 + * <dl>
1.351 + * <dt><i>FloatValue:</i>
1.352 + * <dd><i>Sign<sub>opt</sub></i> {@code NaN}
1.353 + * <dd><i>Sign<sub>opt</sub></i> {@code Infinity}
1.354 + * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
1.355 + * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
1.356 + * <dd><i>SignedInteger</i>
1.357 + * </dl>
1.358 + *
1.359 + * <p>
1.360 + *
1.361 + * <dl>
1.362 + * <dt><i>HexFloatingPointLiteral</i>:
1.363 + * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
1.364 + * </dl>
1.365 + *
1.366 + * <p>
1.367 + *
1.368 + * <dl>
1.369 + * <dt><i>HexSignificand:</i>
1.370 + * <dd><i>HexNumeral</i>
1.371 + * <dd><i>HexNumeral</i> {@code .}
1.372 + * <dd>{@code 0x} <i>HexDigits<sub>opt</sub>
1.373 + * </i>{@code .}<i> HexDigits</i>
1.374 + * <dd>{@code 0X}<i> HexDigits<sub>opt</sub>
1.375 + * </i>{@code .} <i>HexDigits</i>
1.376 + * </dl>
1.377 + *
1.378 + * <p>
1.379 + *
1.380 + * <dl>
1.381 + * <dt><i>BinaryExponent:</i>
1.382 + * <dd><i>BinaryExponentIndicator SignedInteger</i>
1.383 + * </dl>
1.384 + *
1.385 + * <p>
1.386 + *
1.387 + * <dl>
1.388 + * <dt><i>BinaryExponentIndicator:</i>
1.389 + * <dd>{@code p}
1.390 + * <dd>{@code P}
1.391 + * </dl>
1.392 + *
1.393 + * </blockquote>
1.394 + *
1.395 + * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
1.396 + * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
1.397 + * <i>FloatTypeSuffix</i> are as defined in the lexical structure
1.398 + * sections of
1.399 + * <cite>The Java™ Language Specification</cite>,
1.400 + * except that underscores are not accepted between digits.
1.401 + * If {@code s} does not have the form of
1.402 + * a <i>FloatValue</i>, then a {@code NumberFormatException}
1.403 + * is thrown. Otherwise, {@code s} is regarded as
1.404 + * representing an exact decimal value in the usual
1.405 + * "computerized scientific notation" or as an exact
1.406 + * hexadecimal value; this exact numerical value is then
1.407 + * conceptually converted to an "infinitely precise"
1.408 + * binary value that is then rounded to type {@code double}
1.409 + * by the usual round-to-nearest rule of IEEE 754 floating-point
1.410 + * arithmetic, which includes preserving the sign of a zero
1.411 + * value.
1.412 + *
1.413 + * Note that the round-to-nearest rule also implies overflow and
1.414 + * underflow behaviour; if the exact value of {@code s} is large
1.415 + * enough in magnitude (greater than or equal to ({@link
1.416 + * #MAX_VALUE} + {@link Math#ulp(double) ulp(MAX_VALUE)}/2),
1.417 + * rounding to {@code double} will result in an infinity and if the
1.418 + * exact value of {@code s} is small enough in magnitude (less
1.419 + * than or equal to {@link #MIN_VALUE}/2), rounding to float will
1.420 + * result in a zero.
1.421 + *
1.422 + * Finally, after rounding a {@code Double} object representing
1.423 + * this {@code double} value is returned.
1.424 + *
1.425 + * <p> To interpret localized string representations of a
1.426 + * floating-point value, use subclasses of {@link
1.427 + * java.text.NumberFormat}.
1.428 + *
1.429 + * <p>Note that trailing format specifiers, specifiers that
1.430 + * determine the type of a floating-point literal
1.431 + * ({@code 1.0f} is a {@code float} value;
1.432 + * {@code 1.0d} is a {@code double} value), do
1.433 + * <em>not</em> influence the results of this method. In other
1.434 + * words, the numerical value of the input string is converted
1.435 + * directly to the target floating-point type. The two-step
1.436 + * sequence of conversions, string to {@code float} followed
1.437 + * by {@code float} to {@code double}, is <em>not</em>
1.438 + * equivalent to converting a string directly to
1.439 + * {@code double}. For example, the {@code float}
1.440 + * literal {@code 0.1f} is equal to the {@code double}
1.441 + * value {@code 0.10000000149011612}; the {@code float}
1.442 + * literal {@code 0.1f} represents a different numerical
1.443 + * value than the {@code double} literal
1.444 + * {@code 0.1}. (The numerical value 0.1 cannot be exactly
1.445 + * represented in a binary floating-point number.)
1.446 + *
1.447 + * <p>To avoid calling this method on an invalid string and having
1.448 + * a {@code NumberFormatException} be thrown, the regular
1.449 + * expression below can be used to screen the input string:
1.450 + *
1.451 + * <code>
1.452 + * <pre>
1.453 + * final String Digits = "(\\p{Digit}+)";
1.454 + * final String HexDigits = "(\\p{XDigit}+)";
1.455 + * // an exponent is 'e' or 'E' followed by an optionally
1.456 + * // signed decimal integer.
1.457 + * final String Exp = "[eE][+-]?"+Digits;
1.458 + * final String fpRegex =
1.459 + * ("[\\x00-\\x20]*"+ // Optional leading "whitespace"
1.460 + * "[+-]?(" + // Optional sign character
1.461 + * "NaN|" + // "NaN" string
1.462 + * "Infinity|" + // "Infinity" string
1.463 + *
1.464 + * // A decimal floating-point string representing a finite positive
1.465 + * // number without a leading sign has at most five basic pieces:
1.466 + * // Digits . Digits ExponentPart FloatTypeSuffix
1.467 + * //
1.468 + * // Since this method allows integer-only strings as input
1.469 + * // in addition to strings of floating-point literals, the
1.470 + * // two sub-patterns below are simplifications of the grammar
1.471 + * // productions from section 3.10.2 of
1.472 + * // <cite>The Java™ Language Specification</cite>.
1.473 + *
1.474 + * // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
1.475 + * "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
1.476 + *
1.477 + * // . Digits ExponentPart_opt FloatTypeSuffix_opt
1.478 + * "(\\.("+Digits+")("+Exp+")?)|"+
1.479 + *
1.480 + * // Hexadecimal strings
1.481 + * "((" +
1.482 + * // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
1.483 + * "(0[xX]" + HexDigits + "(\\.)?)|" +
1.484 + *
1.485 + * // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
1.486 + * "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
1.487 + *
1.488 + * ")[pP][+-]?" + Digits + "))" +
1.489 + * "[fFdD]?))" +
1.490 + * "[\\x00-\\x20]*");// Optional trailing "whitespace"
1.491 + *
1.492 + * if (Pattern.matches(fpRegex, myString))
1.493 + * Double.valueOf(myString); // Will not throw NumberFormatException
1.494 + * else {
1.495 + * // Perform suitable alternative action
1.496 + * }
1.497 + * </pre>
1.498 + * </code>
1.499 + *
1.500 + * @param s the string to be parsed.
1.501 + * @return a {@code Double} object holding the value
1.502 + * represented by the {@code String} argument.
1.503 + * @throws NumberFormatException if the string does not contain a
1.504 + * parsable number.
1.505 + */
1.506 + public static Double valueOf(String s) throws NumberFormatException {
1.507 + return new Double(FloatingDecimal.readJavaFormatString(s).doubleValue());
1.508 + }
1.509 +
1.510 + /**
1.511 + * Returns a {@code Double} instance representing the specified
1.512 + * {@code double} value.
1.513 + * If a new {@code Double} instance is not required, this method
1.514 + * should generally be used in preference to the constructor
1.515 + * {@link #Double(double)}, as this method is likely to yield
1.516 + * significantly better space and time performance by caching
1.517 + * frequently requested values.
1.518 + *
1.519 + * @param d a double value.
1.520 + * @return a {@code Double} instance representing {@code d}.
1.521 + * @since 1.5
1.522 + */
1.523 + public static Double valueOf(double d) {
1.524 + return new Double(d);
1.525 + }
1.526 +
1.527 + /**
1.528 + * Returns a new {@code double} initialized to the value
1.529 + * represented by the specified {@code String}, as performed
1.530 + * by the {@code valueOf} method of class
1.531 + * {@code Double}.
1.532 + *
1.533 + * @param s the string to be parsed.
1.534 + * @return the {@code double} value represented by the string
1.535 + * argument.
1.536 + * @throws NullPointerException if the string is null
1.537 + * @throws NumberFormatException if the string does not contain
1.538 + * a parsable {@code double}.
1.539 + * @see java.lang.Double#valueOf(String)
1.540 + * @since 1.2
1.541 + */
1.542 + public static double parseDouble(String s) throws NumberFormatException {
1.543 + return FloatingDecimal.readJavaFormatString(s).doubleValue();
1.544 + }
1.545 +
1.546 + /**
1.547 + * Returns {@code true} if the specified number is a
1.548 + * Not-a-Number (NaN) value, {@code false} otherwise.
1.549 + *
1.550 + * @param v the value to be tested.
1.551 + * @return {@code true} if the value of the argument is NaN;
1.552 + * {@code false} otherwise.
1.553 + */
1.554 + static public boolean isNaN(double v) {
1.555 + return (v != v);
1.556 + }
1.557 +
1.558 + /**
1.559 + * Returns {@code true} if the specified number is infinitely
1.560 + * large in magnitude, {@code false} otherwise.
1.561 + *
1.562 + * @param v the value to be tested.
1.563 + * @return {@code true} if the value of the argument is positive
1.564 + * infinity or negative infinity; {@code false} otherwise.
1.565 + */
1.566 + static public boolean isInfinite(double v) {
1.567 + return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
1.568 + }
1.569 +
1.570 + /**
1.571 + * The value of the Double.
1.572 + *
1.573 + * @serial
1.574 + */
1.575 + private final double value;
1.576 +
1.577 + /**
1.578 + * Constructs a newly allocated {@code Double} object that
1.579 + * represents the primitive {@code double} argument.
1.580 + *
1.581 + * @param value the value to be represented by the {@code Double}.
1.582 + */
1.583 + public Double(double value) {
1.584 + this.value = value;
1.585 + }
1.586 +
1.587 + /**
1.588 + * Constructs a newly allocated {@code Double} object that
1.589 + * represents the floating-point value of type {@code double}
1.590 + * represented by the string. The string is converted to a
1.591 + * {@code double} value as if by the {@code valueOf} method.
1.592 + *
1.593 + * @param s a string to be converted to a {@code Double}.
1.594 + * @throws NumberFormatException if the string does not contain a
1.595 + * parsable number.
1.596 + * @see java.lang.Double#valueOf(java.lang.String)
1.597 + */
1.598 + public Double(String s) throws NumberFormatException {
1.599 + // REMIND: this is inefficient
1.600 + this(valueOf(s).doubleValue());
1.601 + }
1.602 +
1.603 + /**
1.604 + * Returns {@code true} if this {@code Double} value is
1.605 + * a Not-a-Number (NaN), {@code false} otherwise.
1.606 + *
1.607 + * @return {@code true} if the value represented by this object is
1.608 + * NaN; {@code false} otherwise.
1.609 + */
1.610 + public boolean isNaN() {
1.611 + return isNaN(value);
1.612 + }
1.613 +
1.614 + /**
1.615 + * Returns {@code true} if this {@code Double} value is
1.616 + * infinitely large in magnitude, {@code false} otherwise.
1.617 + *
1.618 + * @return {@code true} if the value represented by this object is
1.619 + * positive infinity or negative infinity;
1.620 + * {@code false} otherwise.
1.621 + */
1.622 + public boolean isInfinite() {
1.623 + return isInfinite(value);
1.624 + }
1.625 +
1.626 + /**
1.627 + * Returns a string representation of this {@code Double} object.
1.628 + * The primitive {@code double} value represented by this
1.629 + * object is converted to a string exactly as if by the method
1.630 + * {@code toString} of one argument.
1.631 + *
1.632 + * @return a {@code String} representation of this object.
1.633 + * @see java.lang.Double#toString(double)
1.634 + */
1.635 + public String toString() {
1.636 + return toString(value);
1.637 + }
1.638 +
1.639 + /**
1.640 + * Returns the value of this {@code Double} as a {@code byte} (by
1.641 + * casting to a {@code byte}).
1.642 + *
1.643 + * @return the {@code double} value represented by this object
1.644 + * converted to type {@code byte}
1.645 + * @since JDK1.1
1.646 + */
1.647 + public byte byteValue() {
1.648 + return (byte)value;
1.649 + }
1.650 +
1.651 + /**
1.652 + * Returns the value of this {@code Double} as a
1.653 + * {@code short} (by casting to a {@code short}).
1.654 + *
1.655 + * @return the {@code double} value represented by this object
1.656 + * converted to type {@code short}
1.657 + * @since JDK1.1
1.658 + */
1.659 + public short shortValue() {
1.660 + return (short)value;
1.661 + }
1.662 +
1.663 + /**
1.664 + * Returns the value of this {@code Double} as an
1.665 + * {@code int} (by casting to type {@code int}).
1.666 + *
1.667 + * @return the {@code double} value represented by this object
1.668 + * converted to type {@code int}
1.669 + */
1.670 + public int intValue() {
1.671 + return (int)value;
1.672 + }
1.673 +
1.674 + /**
1.675 + * Returns the value of this {@code Double} as a
1.676 + * {@code long} (by casting to type {@code long}).
1.677 + *
1.678 + * @return the {@code double} value represented by this object
1.679 + * converted to type {@code long}
1.680 + */
1.681 + public long longValue() {
1.682 + return (long)value;
1.683 + }
1.684 +
1.685 + /**
1.686 + * Returns the {@code float} value of this
1.687 + * {@code Double} object.
1.688 + *
1.689 + * @return the {@code double} value represented by this object
1.690 + * converted to type {@code float}
1.691 + * @since JDK1.0
1.692 + */
1.693 + public float floatValue() {
1.694 + return (float)value;
1.695 + }
1.696 +
1.697 + /**
1.698 + * Returns the {@code double} value of this
1.699 + * {@code Double} object.
1.700 + *
1.701 + * @return the {@code double} value represented by this object
1.702 + */
1.703 + public double doubleValue() {
1.704 + return (double)value;
1.705 + }
1.706 +
1.707 + /**
1.708 + * Returns a hash code for this {@code Double} object. The
1.709 + * result is the exclusive OR of the two halves of the
1.710 + * {@code long} integer bit representation, exactly as
1.711 + * produced by the method {@link #doubleToLongBits(double)}, of
1.712 + * the primitive {@code double} value represented by this
1.713 + * {@code Double} object. That is, the hash code is the value
1.714 + * of the expression:
1.715 + *
1.716 + * <blockquote>
1.717 + * {@code (int)(v^(v>>>32))}
1.718 + * </blockquote>
1.719 + *
1.720 + * where {@code v} is defined by:
1.721 + *
1.722 + * <blockquote>
1.723 + * {@code long v = Double.doubleToLongBits(this.doubleValue());}
1.724 + * </blockquote>
1.725 + *
1.726 + * @return a {@code hash code} value for this object.
1.727 + */
1.728 + public int hashCode() {
1.729 + long bits = doubleToLongBits(value);
1.730 + return (int)(bits ^ (bits >>> 32));
1.731 + }
1.732 +
1.733 + /**
1.734 + * Compares this object against the specified object. The result
1.735 + * is {@code true} if and only if the argument is not
1.736 + * {@code null} and is a {@code Double} object that
1.737 + * represents a {@code double} that has the same value as the
1.738 + * {@code double} represented by this object. For this
1.739 + * purpose, two {@code double} values are considered to be
1.740 + * the same if and only if the method {@link
1.741 + * #doubleToLongBits(double)} returns the identical
1.742 + * {@code long} value when applied to each.
1.743 + *
1.744 + * <p>Note that in most cases, for two instances of class
1.745 + * {@code Double}, {@code d1} and {@code d2}, the
1.746 + * value of {@code d1.equals(d2)} is {@code true} if and
1.747 + * only if
1.748 + *
1.749 + * <blockquote>
1.750 + * {@code d1.doubleValue() == d2.doubleValue()}
1.751 + * </blockquote>
1.752 + *
1.753 + * <p>also has the value {@code true}. However, there are two
1.754 + * exceptions:
1.755 + * <ul>
1.756 + * <li>If {@code d1} and {@code d2} both represent
1.757 + * {@code Double.NaN}, then the {@code equals} method
1.758 + * returns {@code true}, even though
1.759 + * {@code Double.NaN==Double.NaN} has the value
1.760 + * {@code false}.
1.761 + * <li>If {@code d1} represents {@code +0.0} while
1.762 + * {@code d2} represents {@code -0.0}, or vice versa,
1.763 + * the {@code equal} test has the value {@code false},
1.764 + * even though {@code +0.0==-0.0} has the value {@code true}.
1.765 + * </ul>
1.766 + * This definition allows hash tables to operate properly.
1.767 + * @param obj the object to compare with.
1.768 + * @return {@code true} if the objects are the same;
1.769 + * {@code false} otherwise.
1.770 + * @see java.lang.Double#doubleToLongBits(double)
1.771 + */
1.772 + public boolean equals(Object obj) {
1.773 + return (obj instanceof Double)
1.774 + && (doubleToLongBits(((Double)obj).value) ==
1.775 + doubleToLongBits(value));
1.776 + }
1.777 +
1.778 + /**
1.779 + * Returns a representation of the specified floating-point value
1.780 + * according to the IEEE 754 floating-point "double
1.781 + * format" bit layout.
1.782 + *
1.783 + * <p>Bit 63 (the bit that is selected by the mask
1.784 + * {@code 0x8000000000000000L}) represents the sign of the
1.785 + * floating-point number. Bits
1.786 + * 62-52 (the bits that are selected by the mask
1.787 + * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
1.788 + * (the bits that are selected by the mask
1.789 + * {@code 0x000fffffffffffffL}) represent the significand
1.790 + * (sometimes called the mantissa) of the floating-point number.
1.791 + *
1.792 + * <p>If the argument is positive infinity, the result is
1.793 + * {@code 0x7ff0000000000000L}.
1.794 + *
1.795 + * <p>If the argument is negative infinity, the result is
1.796 + * {@code 0xfff0000000000000L}.
1.797 + *
1.798 + * <p>If the argument is NaN, the result is
1.799 + * {@code 0x7ff8000000000000L}.
1.800 + *
1.801 + * <p>In all cases, the result is a {@code long} integer that, when
1.802 + * given to the {@link #longBitsToDouble(long)} method, will produce a
1.803 + * floating-point value the same as the argument to
1.804 + * {@code doubleToLongBits} (except all NaN values are
1.805 + * collapsed to a single "canonical" NaN value).
1.806 + *
1.807 + * @param value a {@code double} precision floating-point number.
1.808 + * @return the bits that represent the floating-point number.
1.809 + */
1.810 + public static long doubleToLongBits(double value) {
1.811 + long result = doubleToRawLongBits(value);
1.812 + // Check for NaN based on values of bit fields, maximum
1.813 + // exponent and nonzero significand.
1.814 + if ( ((result & DoubleConsts.EXP_BIT_MASK) ==
1.815 + DoubleConsts.EXP_BIT_MASK) &&
1.816 + (result & DoubleConsts.SIGNIF_BIT_MASK) != 0L)
1.817 + result = 0x7ff8000000000000L;
1.818 + return result;
1.819 + }
1.820 +
1.821 + /**
1.822 + * Returns a representation of the specified floating-point value
1.823 + * according to the IEEE 754 floating-point "double
1.824 + * format" bit layout, preserving Not-a-Number (NaN) values.
1.825 + *
1.826 + * <p>Bit 63 (the bit that is selected by the mask
1.827 + * {@code 0x8000000000000000L}) represents the sign of the
1.828 + * floating-point number. Bits
1.829 + * 62-52 (the bits that are selected by the mask
1.830 + * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
1.831 + * (the bits that are selected by the mask
1.832 + * {@code 0x000fffffffffffffL}) represent the significand
1.833 + * (sometimes called the mantissa) of the floating-point number.
1.834 + *
1.835 + * <p>If the argument is positive infinity, the result is
1.836 + * {@code 0x7ff0000000000000L}.
1.837 + *
1.838 + * <p>If the argument is negative infinity, the result is
1.839 + * {@code 0xfff0000000000000L}.
1.840 + *
1.841 + * <p>If the argument is NaN, the result is the {@code long}
1.842 + * integer representing the actual NaN value. Unlike the
1.843 + * {@code doubleToLongBits} method,
1.844 + * {@code doubleToRawLongBits} does not collapse all the bit
1.845 + * patterns encoding a NaN to a single "canonical" NaN
1.846 + * value.
1.847 + *
1.848 + * <p>In all cases, the result is a {@code long} integer that,
1.849 + * when given to the {@link #longBitsToDouble(long)} method, will
1.850 + * produce a floating-point value the same as the argument to
1.851 + * {@code doubleToRawLongBits}.
1.852 + *
1.853 + * @param value a {@code double} precision floating-point number.
1.854 + * @return the bits that represent the floating-point number.
1.855 + * @since 1.3
1.856 + */
1.857 + public static native long doubleToRawLongBits(double value);
1.858 +
1.859 + /**
1.860 + * Returns the {@code double} value corresponding to a given
1.861 + * bit representation.
1.862 + * The argument is considered to be a representation of a
1.863 + * floating-point value according to the IEEE 754 floating-point
1.864 + * "double format" bit layout.
1.865 + *
1.866 + * <p>If the argument is {@code 0x7ff0000000000000L}, the result
1.867 + * is positive infinity.
1.868 + *
1.869 + * <p>If the argument is {@code 0xfff0000000000000L}, the result
1.870 + * is negative infinity.
1.871 + *
1.872 + * <p>If the argument is any value in the range
1.873 + * {@code 0x7ff0000000000001L} through
1.874 + * {@code 0x7fffffffffffffffL} or in the range
1.875 + * {@code 0xfff0000000000001L} through
1.876 + * {@code 0xffffffffffffffffL}, the result is a NaN. No IEEE
1.877 + * 754 floating-point operation provided by Java can distinguish
1.878 + * between two NaN values of the same type with different bit
1.879 + * patterns. Distinct values of NaN are only distinguishable by
1.880 + * use of the {@code Double.doubleToRawLongBits} method.
1.881 + *
1.882 + * <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
1.883 + * values that can be computed from the argument:
1.884 + *
1.885 + * <blockquote><pre>
1.886 + * int s = ((bits >> 63) == 0) ? 1 : -1;
1.887 + * int e = (int)((bits >> 52) & 0x7ffL);
1.888 + * long m = (e == 0) ?
1.889 + * (bits & 0xfffffffffffffL) << 1 :
1.890 + * (bits & 0xfffffffffffffL) | 0x10000000000000L;
1.891 + * </pre></blockquote>
1.892 + *
1.893 + * Then the floating-point result equals the value of the mathematical
1.894 + * expression <i>s</i>·<i>m</i>·2<sup><i>e</i>-1075</sup>.
1.895 + *
1.896 + * <p>Note that this method may not be able to return a
1.897 + * {@code double} NaN with exactly same bit pattern as the
1.898 + * {@code long} argument. IEEE 754 distinguishes between two
1.899 + * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The
1.900 + * differences between the two kinds of NaN are generally not
1.901 + * visible in Java. Arithmetic operations on signaling NaNs turn
1.902 + * them into quiet NaNs with a different, but often similar, bit
1.903 + * pattern. However, on some processors merely copying a
1.904 + * signaling NaN also performs that conversion. In particular,
1.905 + * copying a signaling NaN to return it to the calling method
1.906 + * may perform this conversion. So {@code longBitsToDouble}
1.907 + * may not be able to return a {@code double} with a
1.908 + * signaling NaN bit pattern. Consequently, for some
1.909 + * {@code long} values,
1.910 + * {@code doubleToRawLongBits(longBitsToDouble(start))} may
1.911 + * <i>not</i> equal {@code start}. Moreover, which
1.912 + * particular bit patterns represent signaling NaNs is platform
1.913 + * dependent; although all NaN bit patterns, quiet or signaling,
1.914 + * must be in the NaN range identified above.
1.915 + *
1.916 + * @param bits any {@code long} integer.
1.917 + * @return the {@code double} floating-point value with the same
1.918 + * bit pattern.
1.919 + */
1.920 + public static native double longBitsToDouble(long bits);
1.921 +
1.922 + /**
1.923 + * Compares two {@code Double} objects numerically. There
1.924 + * are two ways in which comparisons performed by this method
1.925 + * differ from those performed by the Java language numerical
1.926 + * comparison operators ({@code <, <=, ==, >=, >})
1.927 + * when applied to primitive {@code double} values:
1.928 + * <ul><li>
1.929 + * {@code Double.NaN} is considered by this method
1.930 + * to be equal to itself and greater than all other
1.931 + * {@code double} values (including
1.932 + * {@code Double.POSITIVE_INFINITY}).
1.933 + * <li>
1.934 + * {@code 0.0d} is considered by this method to be greater
1.935 + * than {@code -0.0d}.
1.936 + * </ul>
1.937 + * This ensures that the <i>natural ordering</i> of
1.938 + * {@code Double} objects imposed by this method is <i>consistent
1.939 + * with equals</i>.
1.940 + *
1.941 + * @param anotherDouble the {@code Double} to be compared.
1.942 + * @return the value {@code 0} if {@code anotherDouble} is
1.943 + * numerically equal to this {@code Double}; a value
1.944 + * less than {@code 0} if this {@code Double}
1.945 + * is numerically less than {@code anotherDouble};
1.946 + * and a value greater than {@code 0} if this
1.947 + * {@code Double} is numerically greater than
1.948 + * {@code anotherDouble}.
1.949 + *
1.950 + * @since 1.2
1.951 + */
1.952 + public int compareTo(Double anotherDouble) {
1.953 + return Double.compare(value, anotherDouble.value);
1.954 + }
1.955 +
1.956 + /**
1.957 + * Compares the two specified {@code double} values. The sign
1.958 + * of the integer value returned is the same as that of the
1.959 + * integer that would be returned by the call:
1.960 + * <pre>
1.961 + * new Double(d1).compareTo(new Double(d2))
1.962 + * </pre>
1.963 + *
1.964 + * @param d1 the first {@code double} to compare
1.965 + * @param d2 the second {@code double} to compare
1.966 + * @return the value {@code 0} if {@code d1} is
1.967 + * numerically equal to {@code d2}; a value less than
1.968 + * {@code 0} if {@code d1} is numerically less than
1.969 + * {@code d2}; and a value greater than {@code 0}
1.970 + * if {@code d1} is numerically greater than
1.971 + * {@code d2}.
1.972 + * @since 1.4
1.973 + */
1.974 + public static int compare(double d1, double d2) {
1.975 + if (d1 < d2)
1.976 + return -1; // Neither val is NaN, thisVal is smaller
1.977 + if (d1 > d2)
1.978 + return 1; // Neither val is NaN, thisVal is larger
1.979 +
1.980 + // Cannot use doubleToRawLongBits because of possibility of NaNs.
1.981 + long thisBits = Double.doubleToLongBits(d1);
1.982 + long anotherBits = Double.doubleToLongBits(d2);
1.983 +
1.984 + return (thisBits == anotherBits ? 0 : // Values are equal
1.985 + (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1.986 + 1)); // (0.0, -0.0) or (NaN, !NaN)
1.987 + }
1.988 +
1.989 + /** use serialVersionUID from JDK 1.0.2 for interoperability */
1.990 + private static final long serialVersionUID = -9172774392245257468L;
1.991 +}