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