rt/emul/mini/src/main/java/java/lang/Double.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 09 Mar 2013 11:58:50 +0100
changeset 826 fb751bcc23fd
parent 824 97fdbed30f8b
child 828 91d9fa810a43
permissions -rw-r--r--
doubleToLongBits and floatToIntBits implemented via typed arrays. Requires real browser (Rhino does not support typed arrays) and as such creating new module to hold the tests and execute them in 'brwsr' mode.
     1 /*
     2  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 package java.lang;
    27 
    28 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    29 
    30 /**
    31  * The {@code Double} class wraps a value of the primitive type
    32  * {@code double} in an object. An object of type
    33  * {@code Double} contains a single field whose type is
    34  * {@code double}.
    35  *
    36  * <p>In addition, this class provides several methods for converting a
    37  * {@code double} to a {@code String} and a
    38  * {@code String} to a {@code double}, as well as other
    39  * constants and methods useful when dealing with a
    40  * {@code double}.
    41  *
    42  * @author  Lee Boynton
    43  * @author  Arthur van Hoff
    44  * @author  Joseph D. Darcy
    45  * @since JDK1.0
    46  */
    47 public final class Double extends Number implements Comparable<Double> {
    48     /**
    49      * A constant holding the positive infinity of type
    50      * {@code double}. It is equal to the value returned by
    51      * {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
    52      */
    53     public static final double POSITIVE_INFINITY = 1.0 / 0.0;
    54 
    55     /**
    56      * A constant holding the negative infinity of type
    57      * {@code double}. It is equal to the value returned by
    58      * {@code Double.longBitsToDouble(0xfff0000000000000L)}.
    59      */
    60     public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
    61 
    62     /**
    63      * A constant holding a Not-a-Number (NaN) value of type
    64      * {@code double}. It is equivalent to the value returned by
    65      * {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
    66      */
    67     public static final double NaN = 0.0d / 0.0;
    68 
    69     /**
    70      * A constant holding the largest positive finite value of type
    71      * {@code double},
    72      * (2-2<sup>-52</sup>)&middot;2<sup>1023</sup>.  It is equal to
    73      * the hexadecimal floating-point literal
    74      * {@code 0x1.fffffffffffffP+1023} and also equal to
    75      * {@code Double.longBitsToDouble(0x7fefffffffffffffL)}.
    76      */
    77     public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308
    78 
    79     /**
    80      * A constant holding the smallest positive normal value of type
    81      * {@code double}, 2<sup>-1022</sup>.  It is equal to the
    82      * hexadecimal floating-point literal {@code 0x1.0p-1022} and also
    83      * equal to {@code Double.longBitsToDouble(0x0010000000000000L)}.
    84      *
    85      * @since 1.6
    86      */
    87     public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308
    88 
    89     /**
    90      * A constant holding the smallest positive nonzero value of type
    91      * {@code double}, 2<sup>-1074</sup>. It is equal to the
    92      * hexadecimal floating-point literal
    93      * {@code 0x0.0000000000001P-1022} and also equal to
    94      * {@code Double.longBitsToDouble(0x1L)}.
    95      */
    96     public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324
    97 
    98     /**
    99      * Maximum exponent a finite {@code double} variable may have.
   100      * It is equal to the value returned by
   101      * {@code Math.getExponent(Double.MAX_VALUE)}.
   102      *
   103      * @since 1.6
   104      */
   105     public static final int MAX_EXPONENT = 1023;
   106 
   107     /**
   108      * Minimum exponent a normalized {@code double} variable may
   109      * have.  It is equal to the value returned by
   110      * {@code Math.getExponent(Double.MIN_NORMAL)}.
   111      *
   112      * @since 1.6
   113      */
   114     public static final int MIN_EXPONENT = -1022;
   115 
   116     /**
   117      * The number of bits used to represent a {@code double} value.
   118      *
   119      * @since 1.5
   120      */
   121     public static final int SIZE = 64;
   122 
   123     /**
   124      * The {@code Class} instance representing the primitive type
   125      * {@code double}.
   126      *
   127      * @since JDK1.1
   128      */
   129     public static final Class<Double>   TYPE = (Class<Double>) Class.getPrimitiveClass("double");
   130 
   131     /**
   132      * Returns a string representation of the {@code double}
   133      * argument. All characters mentioned below are ASCII characters.
   134      * <ul>
   135      * <li>If the argument is NaN, the result is the string
   136      *     "{@code NaN}".
   137      * <li>Otherwise, the result is a string that represents the sign and
   138      * magnitude (absolute value) of the argument. If the sign is negative,
   139      * the first character of the result is '{@code -}'
   140      * (<code>'&#92;u002D'</code>); if the sign is positive, no sign character
   141      * appears in the result. As for the magnitude <i>m</i>:
   142      * <ul>
   143      * <li>If <i>m</i> is infinity, it is represented by the characters
   144      * {@code "Infinity"}; thus, positive infinity produces the result
   145      * {@code "Infinity"} and negative infinity produces the result
   146      * {@code "-Infinity"}.
   147      *
   148      * <li>If <i>m</i> is zero, it is represented by the characters
   149      * {@code "0.0"}; thus, negative zero produces the result
   150      * {@code "-0.0"} and positive zero produces the result
   151      * {@code "0.0"}.
   152      *
   153      * <li>If <i>m</i> is greater than or equal to 10<sup>-3</sup> but less
   154      * than 10<sup>7</sup>, then it is represented as the integer part of
   155      * <i>m</i>, in decimal form with no leading zeroes, followed by
   156      * '{@code .}' (<code>'&#92;u002E'</code>), followed by one or
   157      * more decimal digits representing the fractional part of <i>m</i>.
   158      *
   159      * <li>If <i>m</i> is less than 10<sup>-3</sup> or greater than or
   160      * equal to 10<sup>7</sup>, then it is represented in so-called
   161      * "computerized scientific notation." Let <i>n</i> be the unique
   162      * integer such that 10<sup><i>n</i></sup> &le; <i>m</i> {@literal <}
   163      * 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
   164      * mathematically exact quotient of <i>m</i> and
   165      * 10<sup><i>n</i></sup> so that 1 &le; <i>a</i> {@literal <} 10. The
   166      * magnitude is then represented as the integer part of <i>a</i>,
   167      * as a single decimal digit, followed by '{@code .}'
   168      * (<code>'&#92;u002E'</code>), followed by decimal digits
   169      * representing the fractional part of <i>a</i>, followed by the
   170      * letter '{@code E}' (<code>'&#92;u0045'</code>), followed
   171      * by a representation of <i>n</i> as a decimal integer, as
   172      * produced by the method {@link Integer#toString(int)}.
   173      * </ul>
   174      * </ul>
   175      * How many digits must be printed for the fractional part of
   176      * <i>m</i> or <i>a</i>? There must be at least one digit to represent
   177      * the fractional part, and beyond that as many, but only as many, more
   178      * digits as are needed to uniquely distinguish the argument value from
   179      * adjacent values of type {@code double}. That is, suppose that
   180      * <i>x</i> is the exact mathematical value represented by the decimal
   181      * representation produced by this method for a finite nonzero argument
   182      * <i>d</i>. Then <i>d</i> must be the {@code double} value nearest
   183      * to <i>x</i>; or if two {@code double} values are equally close
   184      * to <i>x</i>, then <i>d</i> must be one of them and the least
   185      * significant bit of the significand of <i>d</i> must be {@code 0}.
   186      *
   187      * <p>To create localized string representations of a floating-point
   188      * value, use subclasses of {@link java.text.NumberFormat}.
   189      *
   190      * @param   d   the {@code double} to be converted.
   191      * @return a string representation of the argument.
   192      */
   193     @JavaScriptBody(args="d", body="var f = Math.floor(d);\n" +
   194         "if (f === d && isFinite(d)) return d.toString() + '.0';\n" +
   195         "else return d.toString();"
   196     )
   197     public static native String toString(double d);
   198 
   199     /**
   200      * Returns a hexadecimal string representation of the
   201      * {@code double} argument. All characters mentioned below
   202      * are ASCII characters.
   203      *
   204      * <ul>
   205      * <li>If the argument is NaN, the result is the string
   206      *     "{@code NaN}".
   207      * <li>Otherwise, the result is a string that represents the sign
   208      * and magnitude of the argument. If the sign is negative, the
   209      * first character of the result is '{@code -}'
   210      * (<code>'&#92;u002D'</code>); if the sign is positive, no sign
   211      * character appears in the result. As for the magnitude <i>m</i>:
   212      *
   213      * <ul>
   214      * <li>If <i>m</i> is infinity, it is represented by the string
   215      * {@code "Infinity"}; thus, positive infinity produces the
   216      * result {@code "Infinity"} and negative infinity produces
   217      * the result {@code "-Infinity"}.
   218      *
   219      * <li>If <i>m</i> is zero, it is represented by the string
   220      * {@code "0x0.0p0"}; thus, negative zero produces the result
   221      * {@code "-0x0.0p0"} and positive zero produces the result
   222      * {@code "0x0.0p0"}.
   223      *
   224      * <li>If <i>m</i> is a {@code double} value with a
   225      * normalized representation, substrings are used to represent the
   226      * significand and exponent fields.  The significand is
   227      * represented by the characters {@code "0x1."}
   228      * followed by a lowercase hexadecimal representation of the rest
   229      * of the significand as a fraction.  Trailing zeros in the
   230      * hexadecimal representation are removed unless all the digits
   231      * are zero, in which case a single zero is used. Next, the
   232      * exponent is represented by {@code "p"} followed
   233      * by a decimal string of the unbiased exponent as if produced by
   234      * a call to {@link Integer#toString(int) Integer.toString} on the
   235      * exponent value.
   236      *
   237      * <li>If <i>m</i> is a {@code double} value with a subnormal
   238      * representation, the significand is represented by the
   239      * characters {@code "0x0."} followed by a
   240      * hexadecimal representation of the rest of the significand as a
   241      * fraction.  Trailing zeros in the hexadecimal representation are
   242      * removed. Next, the exponent is represented by
   243      * {@code "p-1022"}.  Note that there must be at
   244      * least one nonzero digit in a subnormal significand.
   245      *
   246      * </ul>
   247      *
   248      * </ul>
   249      *
   250      * <table border>
   251      * <caption><h3>Examples</h3></caption>
   252      * <tr><th>Floating-point Value</th><th>Hexadecimal String</th>
   253      * <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td>
   254      * <tr><td>{@code -1.0}</td>        <td>{@code -0x1.0p0}</td>
   255      * <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>
   256      * <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>
   257      * <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>
   258      * <tr><td>{@code 0.25}</td>        <td>{@code 0x1.0p-2}</td>
   259      * <tr><td>{@code Double.MAX_VALUE}</td>
   260      *     <td>{@code 0x1.fffffffffffffp1023}</td>
   261      * <tr><td>{@code Minimum Normal Value}</td>
   262      *     <td>{@code 0x1.0p-1022}</td>
   263      * <tr><td>{@code Maximum Subnormal Value}</td>
   264      *     <td>{@code 0x0.fffffffffffffp-1022}</td>
   265      * <tr><td>{@code Double.MIN_VALUE}</td>
   266      *     <td>{@code 0x0.0000000000001p-1022}</td>
   267      * </table>
   268      * @param   d   the {@code double} to be converted.
   269      * @return a hex string representation of the argument.
   270      * @since 1.5
   271      * @author Joseph D. Darcy
   272      */
   273     public static String toHexString(double d) {
   274         throw new UnsupportedOperationException();
   275 //        /*
   276 //         * Modeled after the "a" conversion specifier in C99, section
   277 //         * 7.19.6.1; however, the output of this method is more
   278 //         * tightly specified.
   279 //         */
   280 //        if (!FpUtils.isFinite(d) )
   281 //            // For infinity and NaN, use the decimal output.
   282 //            return Double.toString(d);
   283 //        else {
   284 //            // Initialized to maximum size of output.
   285 //            StringBuffer answer = new StringBuffer(24);
   286 //
   287 //            if (FpUtils.rawCopySign(1.0, d) == -1.0) // value is negative,
   288 //                answer.append("-");                  // so append sign info
   289 //
   290 //            answer.append("0x");
   291 //
   292 //            d = Math.abs(d);
   293 //
   294 //            if(d == 0.0) {
   295 //                answer.append("0.0p0");
   296 //            }
   297 //            else {
   298 //                boolean subnormal = (d < DoubleConsts.MIN_NORMAL);
   299 //
   300 //                // Isolate significand bits and OR in a high-order bit
   301 //                // so that the string representation has a known
   302 //                // length.
   303 //                long signifBits = (Double.doubleToLongBits(d)
   304 //                                   & DoubleConsts.SIGNIF_BIT_MASK) |
   305 //                    0x1000000000000000L;
   306 //
   307 //                // Subnormal values have a 0 implicit bit; normal
   308 //                // values have a 1 implicit bit.
   309 //                answer.append(subnormal ? "0." : "1.");
   310 //
   311 //                // Isolate the low-order 13 digits of the hex
   312 //                // representation.  If all the digits are zero,
   313 //                // replace with a single 0; otherwise, remove all
   314 //                // trailing zeros.
   315 //                String signif = Long.toHexString(signifBits).substring(3,16);
   316 //                answer.append(signif.equals("0000000000000") ? // 13 zeros
   317 //                              "0":
   318 //                              signif.replaceFirst("0{1,12}$", ""));
   319 //
   320 //                // If the value is subnormal, use the E_min exponent
   321 //                // value for double; otherwise, extract and report d's
   322 //                // exponent (the representation of a subnormal uses
   323 //                // E_min -1).
   324 //                answer.append("p" + (subnormal ?
   325 //                               DoubleConsts.MIN_EXPONENT:
   326 //                               FpUtils.getExponent(d) ));
   327 //            }
   328 //            return answer.toString();
   329 //        }
   330     }
   331 
   332     /**
   333      * Returns a {@code Double} object holding the
   334      * {@code double} value represented by the argument string
   335      * {@code s}.
   336      *
   337      * <p>If {@code s} is {@code null}, then a
   338      * {@code NullPointerException} is thrown.
   339      *
   340      * <p>Leading and trailing whitespace characters in {@code s}
   341      * are ignored.  Whitespace is removed as if by the {@link
   342      * String#trim} method; that is, both ASCII space and control
   343      * characters are removed. The rest of {@code s} should
   344      * constitute a <i>FloatValue</i> as described by the lexical
   345      * syntax rules:
   346      *
   347      * <blockquote>
   348      * <dl>
   349      * <dt><i>FloatValue:</i>
   350      * <dd><i>Sign<sub>opt</sub></i> {@code NaN}
   351      * <dd><i>Sign<sub>opt</sub></i> {@code Infinity}
   352      * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
   353      * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
   354      * <dd><i>SignedInteger</i>
   355      * </dl>
   356      *
   357      * <p>
   358      *
   359      * <dl>
   360      * <dt><i>HexFloatingPointLiteral</i>:
   361      * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
   362      * </dl>
   363      *
   364      * <p>
   365      *
   366      * <dl>
   367      * <dt><i>HexSignificand:</i>
   368      * <dd><i>HexNumeral</i>
   369      * <dd><i>HexNumeral</i> {@code .}
   370      * <dd>{@code 0x} <i>HexDigits<sub>opt</sub>
   371      *     </i>{@code .}<i> HexDigits</i>
   372      * <dd>{@code 0X}<i> HexDigits<sub>opt</sub>
   373      *     </i>{@code .} <i>HexDigits</i>
   374      * </dl>
   375      *
   376      * <p>
   377      *
   378      * <dl>
   379      * <dt><i>BinaryExponent:</i>
   380      * <dd><i>BinaryExponentIndicator SignedInteger</i>
   381      * </dl>
   382      *
   383      * <p>
   384      *
   385      * <dl>
   386      * <dt><i>BinaryExponentIndicator:</i>
   387      * <dd>{@code p}
   388      * <dd>{@code P}
   389      * </dl>
   390      *
   391      * </blockquote>
   392      *
   393      * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
   394      * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
   395      * <i>FloatTypeSuffix</i> are as defined in the lexical structure
   396      * sections of
   397      * <cite>The Java&trade; Language Specification</cite>,
   398      * except that underscores are not accepted between digits.
   399      * If {@code s} does not have the form of
   400      * a <i>FloatValue</i>, then a {@code NumberFormatException}
   401      * is thrown. Otherwise, {@code s} is regarded as
   402      * representing an exact decimal value in the usual
   403      * "computerized scientific notation" or as an exact
   404      * hexadecimal value; this exact numerical value is then
   405      * conceptually converted to an "infinitely precise"
   406      * binary value that is then rounded to type {@code double}
   407      * by the usual round-to-nearest rule of IEEE 754 floating-point
   408      * arithmetic, which includes preserving the sign of a zero
   409      * value.
   410      *
   411      * Note that the round-to-nearest rule also implies overflow and
   412      * underflow behaviour; if the exact value of {@code s} is large
   413      * enough in magnitude (greater than or equal to ({@link
   414      * #MAX_VALUE} + {@link Math#ulp(double) ulp(MAX_VALUE)}/2),
   415      * rounding to {@code double} will result in an infinity and if the
   416      * exact value of {@code s} is small enough in magnitude (less
   417      * than or equal to {@link #MIN_VALUE}/2), rounding to float will
   418      * result in a zero.
   419      *
   420      * Finally, after rounding a {@code Double} object representing
   421      * this {@code double} value is returned.
   422      *
   423      * <p> To interpret localized string representations of a
   424      * floating-point value, use subclasses of {@link
   425      * java.text.NumberFormat}.
   426      *
   427      * <p>Note that trailing format specifiers, specifiers that
   428      * determine the type of a floating-point literal
   429      * ({@code 1.0f} is a {@code float} value;
   430      * {@code 1.0d} is a {@code double} value), do
   431      * <em>not</em> influence the results of this method.  In other
   432      * words, the numerical value of the input string is converted
   433      * directly to the target floating-point type.  The two-step
   434      * sequence of conversions, string to {@code float} followed
   435      * by {@code float} to {@code double}, is <em>not</em>
   436      * equivalent to converting a string directly to
   437      * {@code double}. For example, the {@code float}
   438      * literal {@code 0.1f} is equal to the {@code double}
   439      * value {@code 0.10000000149011612}; the {@code float}
   440      * literal {@code 0.1f} represents a different numerical
   441      * value than the {@code double} literal
   442      * {@code 0.1}. (The numerical value 0.1 cannot be exactly
   443      * represented in a binary floating-point number.)
   444      *
   445      * <p>To avoid calling this method on an invalid string and having
   446      * a {@code NumberFormatException} be thrown, the regular
   447      * expression below can be used to screen the input string:
   448      *
   449      * <code>
   450      * <pre>
   451      *  final String Digits     = "(\\p{Digit}+)";
   452      *  final String HexDigits  = "(\\p{XDigit}+)";
   453      *  // an exponent is 'e' or 'E' followed by an optionally
   454      *  // signed decimal integer.
   455      *  final String Exp        = "[eE][+-]?"+Digits;
   456      *  final String fpRegex    =
   457      *      ("[\\x00-\\x20]*"+  // Optional leading "whitespace"
   458      *       "[+-]?(" + // Optional sign character
   459      *       "NaN|" +           // "NaN" string
   460      *       "Infinity|" +      // "Infinity" string
   461      *
   462      *       // A decimal floating-point string representing a finite positive
   463      *       // number without a leading sign has at most five basic pieces:
   464      *       // Digits . Digits ExponentPart FloatTypeSuffix
   465      *       //
   466      *       // Since this method allows integer-only strings as input
   467      *       // in addition to strings of floating-point literals, the
   468      *       // two sub-patterns below are simplifications of the grammar
   469      *       // productions from section 3.10.2 of
   470      *       // <cite>The Java&trade; Language Specification</cite>.
   471      *
   472      *       // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
   473      *       "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
   474      *
   475      *       // . Digits ExponentPart_opt FloatTypeSuffix_opt
   476      *       "(\\.("+Digits+")("+Exp+")?)|"+
   477      *
   478      *       // Hexadecimal strings
   479      *       "((" +
   480      *        // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
   481      *        "(0[xX]" + HexDigits + "(\\.)?)|" +
   482      *
   483      *        // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
   484      *        "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
   485      *
   486      *        ")[pP][+-]?" + Digits + "))" +
   487      *       "[fFdD]?))" +
   488      *       "[\\x00-\\x20]*");// Optional trailing "whitespace"
   489      *
   490      *  if (Pattern.matches(fpRegex, myString))
   491      *      Double.valueOf(myString); // Will not throw NumberFormatException
   492      *  else {
   493      *      // Perform suitable alternative action
   494      *  }
   495      * </pre>
   496      * </code>
   497      *
   498      * @param      s   the string to be parsed.
   499      * @return     a {@code Double} object holding the value
   500      *             represented by the {@code String} argument.
   501      * @throws     NumberFormatException  if the string does not contain a
   502      *             parsable number.
   503      */
   504     @JavaScriptBody(args="s", body="return parseFloat(s);")
   505     public static Double valueOf(String s) throws NumberFormatException {
   506         throw new UnsupportedOperationException();
   507 //        return new Double(FloatingDecimal.readJavaFormatString(s).doubleValue());
   508     }
   509 
   510     /**
   511      * Returns a {@code Double} instance representing the specified
   512      * {@code double} value.
   513      * If a new {@code Double} instance is not required, this method
   514      * should generally be used in preference to the constructor
   515      * {@link #Double(double)}, as this method is likely to yield
   516      * significantly better space and time performance by caching
   517      * frequently requested values.
   518      *
   519      * @param  d a double value.
   520      * @return a {@code Double} instance representing {@code d}.
   521      * @since  1.5
   522      */
   523     public static Double valueOf(double d) {
   524         return new Double(d);
   525     }
   526 
   527     /**
   528      * Returns a new {@code double} initialized to the value
   529      * represented by the specified {@code String}, as performed
   530      * by the {@code valueOf} method of class
   531      * {@code Double}.
   532      *
   533      * @param  s   the string to be parsed.
   534      * @return the {@code double} value represented by the string
   535      *         argument.
   536      * @throws NullPointerException  if the string is null
   537      * @throws NumberFormatException if the string does not contain
   538      *         a parsable {@code double}.
   539      * @see    java.lang.Double#valueOf(String)
   540      * @since 1.2
   541      */
   542     @JavaScriptBody(args="s", body="return parseFloat(s);")
   543     public static double parseDouble(String s) throws NumberFormatException {
   544         throw new UnsupportedOperationException();
   545 //        return FloatingDecimal.readJavaFormatString(s).doubleValue();
   546     }
   547 
   548     /**
   549      * Returns {@code true} if the specified number is a
   550      * Not-a-Number (NaN) value, {@code false} otherwise.
   551      *
   552      * @param   v   the value to be tested.
   553      * @return  {@code true} if the value of the argument is NaN;
   554      *          {@code false} otherwise.
   555      */
   556     static public boolean isNaN(double v) {
   557         return (v != v);
   558     }
   559 
   560     /**
   561      * Returns {@code true} if the specified number is infinitely
   562      * large in magnitude, {@code false} otherwise.
   563      *
   564      * @param   v   the value to be tested.
   565      * @return  {@code true} if the value of the argument is positive
   566      *          infinity or negative infinity; {@code false} otherwise.
   567      */
   568     static public boolean isInfinite(double v) {
   569         return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
   570     }
   571 
   572     /**
   573      * The value of the Double.
   574      *
   575      * @serial
   576      */
   577     private final double value;
   578 
   579     /**
   580      * Constructs a newly allocated {@code Double} object that
   581      * represents the primitive {@code double} argument.
   582      *
   583      * @param   value   the value to be represented by the {@code Double}.
   584      */
   585     public Double(double value) {
   586         this.value = value;
   587     }
   588 
   589     /**
   590      * Constructs a newly allocated {@code Double} object that
   591      * represents the floating-point value of type {@code double}
   592      * represented by the string. The string is converted to a
   593      * {@code double} value as if by the {@code valueOf} method.
   594      *
   595      * @param  s  a string to be converted to a {@code Double}.
   596      * @throws    NumberFormatException  if the string does not contain a
   597      *            parsable number.
   598      * @see       java.lang.Double#valueOf(java.lang.String)
   599      */
   600     public Double(String s) throws NumberFormatException {
   601         // REMIND: this is inefficient
   602         this(valueOf(s).doubleValue());
   603     }
   604 
   605     /**
   606      * Returns {@code true} if this {@code Double} value is
   607      * a Not-a-Number (NaN), {@code false} otherwise.
   608      *
   609      * @return  {@code true} if the value represented by this object is
   610      *          NaN; {@code false} otherwise.
   611      */
   612     public boolean isNaN() {
   613         return isNaN(value);
   614     }
   615 
   616     /**
   617      * Returns {@code true} if this {@code Double} value is
   618      * infinitely large in magnitude, {@code false} otherwise.
   619      *
   620      * @return  {@code true} if the value represented by this object is
   621      *          positive infinity or negative infinity;
   622      *          {@code false} otherwise.
   623      */
   624     public boolean isInfinite() {
   625         return isInfinite(value);
   626     }
   627 
   628     /**
   629      * Returns a string representation of this {@code Double} object.
   630      * The primitive {@code double} value represented by this
   631      * object is converted to a string exactly as if by the method
   632      * {@code toString} of one argument.
   633      *
   634      * @return  a {@code String} representation of this object.
   635      * @see java.lang.Double#toString(double)
   636      */
   637     public String toString() {
   638         return toString(value);
   639     }
   640 
   641     /**
   642      * Returns the value of this {@code Double} as a {@code byte} (by
   643      * casting to a {@code byte}).
   644      *
   645      * @return  the {@code double} value represented by this object
   646      *          converted to type {@code byte}
   647      * @since JDK1.1
   648      */
   649     public byte byteValue() {
   650         return (byte)value;
   651     }
   652 
   653     /**
   654      * Returns the value of this {@code Double} as a
   655      * {@code short} (by casting to a {@code short}).
   656      *
   657      * @return  the {@code double} value represented by this object
   658      *          converted to type {@code short}
   659      * @since JDK1.1
   660      */
   661     public short shortValue() {
   662         return (short)value;
   663     }
   664 
   665     /**
   666      * Returns the value of this {@code Double} as an
   667      * {@code int} (by casting to type {@code int}).
   668      *
   669      * @return  the {@code double} value represented by this object
   670      *          converted to type {@code int}
   671      */
   672     public int intValue() {
   673         return (int)value;
   674     }
   675 
   676     /**
   677      * Returns the value of this {@code Double} as a
   678      * {@code long} (by casting to type {@code long}).
   679      *
   680      * @return  the {@code double} value represented by this object
   681      *          converted to type {@code long}
   682      */
   683     public long longValue() {
   684         return (long)value;
   685     }
   686 
   687     /**
   688      * Returns the {@code float} value of this
   689      * {@code Double} object.
   690      *
   691      * @return  the {@code double} value represented by this object
   692      *          converted to type {@code float}
   693      * @since JDK1.0
   694      */
   695     public float floatValue() {
   696         return (float)value;
   697     }
   698 
   699     /**
   700      * Returns the {@code double} value of this
   701      * {@code Double} object.
   702      *
   703      * @return the {@code double} value represented by this object
   704      */
   705     public double doubleValue() {
   706         return (double)value;
   707     }
   708 
   709     /**
   710      * Returns a hash code for this {@code Double} object. The
   711      * result is the exclusive OR of the two halves of the
   712      * {@code long} integer bit representation, exactly as
   713      * produced by the method {@link #doubleToLongBits(double)}, of
   714      * the primitive {@code double} value represented by this
   715      * {@code Double} object. That is, the hash code is the value
   716      * of the expression:
   717      *
   718      * <blockquote>
   719      *  {@code (int)(v^(v>>>32))}
   720      * </blockquote>
   721      *
   722      * where {@code v} is defined by:
   723      *
   724      * <blockquote>
   725      *  {@code long v = Double.doubleToLongBits(this.doubleValue());}
   726      * </blockquote>
   727      *
   728      * @return  a {@code hash code} value for this object.
   729      */
   730     public int hashCode() {
   731         long bits = doubleToLongBits(value);
   732         return (int)(bits ^ (bits >>> 32));
   733     }
   734 
   735     /**
   736      * Compares this object against the specified object.  The result
   737      * is {@code true} if and only if the argument is not
   738      * {@code null} and is a {@code Double} object that
   739      * represents a {@code double} that has the same value as the
   740      * {@code double} represented by this object. For this
   741      * purpose, two {@code double} values are considered to be
   742      * the same if and only if the method {@link
   743      * #doubleToLongBits(double)} returns the identical
   744      * {@code long} value when applied to each.
   745      *
   746      * <p>Note that in most cases, for two instances of class
   747      * {@code Double}, {@code d1} and {@code d2}, the
   748      * value of {@code d1.equals(d2)} is {@code true} if and
   749      * only if
   750      *
   751      * <blockquote>
   752      *  {@code d1.doubleValue() == d2.doubleValue()}
   753      * </blockquote>
   754      *
   755      * <p>also has the value {@code true}. However, there are two
   756      * exceptions:
   757      * <ul>
   758      * <li>If {@code d1} and {@code d2} both represent
   759      *     {@code Double.NaN}, then the {@code equals} method
   760      *     returns {@code true}, even though
   761      *     {@code Double.NaN==Double.NaN} has the value
   762      *     {@code false}.
   763      * <li>If {@code d1} represents {@code +0.0} while
   764      *     {@code d2} represents {@code -0.0}, or vice versa,
   765      *     the {@code equal} test has the value {@code false},
   766      *     even though {@code +0.0==-0.0} has the value {@code true}.
   767      * </ul>
   768      * This definition allows hash tables to operate properly.
   769      * @param   obj   the object to compare with.
   770      * @return  {@code true} if the objects are the same;
   771      *          {@code false} otherwise.
   772      * @see java.lang.Double#doubleToLongBits(double)
   773      */
   774     public boolean equals(Object obj) {
   775         return (obj instanceof Double)
   776                && (((Double)obj).value) == value;
   777     }
   778 
   779     /**
   780      * Returns a representation of the specified floating-point value
   781      * according to the IEEE 754 floating-point "double
   782      * format" bit layout.
   783      *
   784      * <p>Bit 63 (the bit that is selected by the mask
   785      * {@code 0x8000000000000000L}) represents the sign of the
   786      * floating-point number. Bits
   787      * 62-52 (the bits that are selected by the mask
   788      * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
   789      * (the bits that are selected by the mask
   790      * {@code 0x000fffffffffffffL}) represent the significand
   791      * (sometimes called the mantissa) of the floating-point number.
   792      *
   793      * <p>If the argument is positive infinity, the result is
   794      * {@code 0x7ff0000000000000L}.
   795      *
   796      * <p>If the argument is negative infinity, the result is
   797      * {@code 0xfff0000000000000L}.
   798      *
   799      * <p>If the argument is NaN, the result is
   800      * {@code 0x7ff8000000000000L}.
   801      *
   802      * <p>In all cases, the result is a {@code long} integer that, when
   803      * given to the {@link #longBitsToDouble(long)} method, will produce a
   804      * floating-point value the same as the argument to
   805      * {@code doubleToLongBits} (except all NaN values are
   806      * collapsed to a single "canonical" NaN value).
   807      *
   808      * @param   value   a {@code double} precision floating-point number.
   809      * @return the bits that represent the floating-point number.
   810      */
   811     public static long doubleToLongBits(double value) {
   812         final long EXP_BIT_MASK = 9218868437227405312L;
   813         final long SIGNIF_BIT_MASK = 4503599627370495L;
   814         
   815         long result = doubleToRawLongBits(value);
   816         // Check for NaN based on values of bit fields, maximum
   817         // exponent and nonzero significand.
   818         if ( ((result & EXP_BIT_MASK) ==
   819               EXP_BIT_MASK) &&
   820              (result & SIGNIF_BIT_MASK) != 0L)
   821             result = 0x7ff8000000000000L;
   822         return result;
   823     }
   824 
   825     /**
   826      * Returns a representation of the specified floating-point value
   827      * according to the IEEE 754 floating-point "double
   828      * format" bit layout, preserving Not-a-Number (NaN) values.
   829      *
   830      * <p>Bit 63 (the bit that is selected by the mask
   831      * {@code 0x8000000000000000L}) represents the sign of the
   832      * floating-point number. Bits
   833      * 62-52 (the bits that are selected by the mask
   834      * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
   835      * (the bits that are selected by the mask
   836      * {@code 0x000fffffffffffffL}) represent the significand
   837      * (sometimes called the mantissa) of the floating-point number.
   838      *
   839      * <p>If the argument is positive infinity, the result is
   840      * {@code 0x7ff0000000000000L}.
   841      *
   842      * <p>If the argument is negative infinity, the result is
   843      * {@code 0xfff0000000000000L}.
   844      *
   845      * <p>If the argument is NaN, the result is the {@code long}
   846      * integer representing the actual NaN value.  Unlike the
   847      * {@code doubleToLongBits} method,
   848      * {@code doubleToRawLongBits} does not collapse all the bit
   849      * patterns encoding a NaN to a single "canonical" NaN
   850      * value.
   851      *
   852      * <p>In all cases, the result is a {@code long} integer that,
   853      * when given to the {@link #longBitsToDouble(long)} method, will
   854      * produce a floating-point value the same as the argument to
   855      * {@code doubleToRawLongBits}.
   856      *
   857      * @param   value   a {@code double} precision floating-point number.
   858      * @return the bits that represent the floating-point number.
   859      * @since 1.3
   860      */
   861     public static long doubleToRawLongBits(double value) {
   862         int[] arr = { 0, 0 };
   863         doubleToRawLongBits(value, arr);
   864         long l = arr[1];
   865         return (l << 32) | arr[0];
   866     }
   867     
   868     @JavaScriptBody(args = { "value", "arr" }, body = ""
   869         + "var a = new ArrayBuffer(8);"
   870         + "new Float64Array(a)[0] = value;"
   871         + "var out = new Int32Array(a);"
   872         + "arr[0] = out[0];"
   873         + "arr[1] = out[1];"
   874     )
   875     private static native void doubleToRawLongBits(double value, int[] arr);
   876 
   877     /**
   878      * Returns the {@code double} value corresponding to a given
   879      * bit representation.
   880      * The argument is considered to be a representation of a
   881      * floating-point value according to the IEEE 754 floating-point
   882      * "double format" bit layout.
   883      *
   884      * <p>If the argument is {@code 0x7ff0000000000000L}, the result
   885      * is positive infinity.
   886      *
   887      * <p>If the argument is {@code 0xfff0000000000000L}, the result
   888      * is negative infinity.
   889      *
   890      * <p>If the argument is any value in the range
   891      * {@code 0x7ff0000000000001L} through
   892      * {@code 0x7fffffffffffffffL} or in the range
   893      * {@code 0xfff0000000000001L} through
   894      * {@code 0xffffffffffffffffL}, the result is a NaN.  No IEEE
   895      * 754 floating-point operation provided by Java can distinguish
   896      * between two NaN values of the same type with different bit
   897      * patterns.  Distinct values of NaN are only distinguishable by
   898      * use of the {@code Double.doubleToRawLongBits} method.
   899      *
   900      * <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
   901      * values that can be computed from the argument:
   902      *
   903      * <blockquote><pre>
   904      * int s = ((bits &gt;&gt; 63) == 0) ? 1 : -1;
   905      * int e = (int)((bits &gt;&gt; 52) & 0x7ffL);
   906      * long m = (e == 0) ?
   907      *                 (bits & 0xfffffffffffffL) &lt;&lt; 1 :
   908      *                 (bits & 0xfffffffffffffL) | 0x10000000000000L;
   909      * </pre></blockquote>
   910      *
   911      * Then the floating-point result equals the value of the mathematical
   912      * expression <i>s</i>&middot;<i>m</i>&middot;2<sup><i>e</i>-1075</sup>.
   913      *
   914      * <p>Note that this method may not be able to return a
   915      * {@code double} NaN with exactly same bit pattern as the
   916      * {@code long} argument.  IEEE 754 distinguishes between two
   917      * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>.  The
   918      * differences between the two kinds of NaN are generally not
   919      * visible in Java.  Arithmetic operations on signaling NaNs turn
   920      * them into quiet NaNs with a different, but often similar, bit
   921      * pattern.  However, on some processors merely copying a
   922      * signaling NaN also performs that conversion.  In particular,
   923      * copying a signaling NaN to return it to the calling method
   924      * may perform this conversion.  So {@code longBitsToDouble}
   925      * may not be able to return a {@code double} with a
   926      * signaling NaN bit pattern.  Consequently, for some
   927      * {@code long} values,
   928      * {@code doubleToRawLongBits(longBitsToDouble(start))} may
   929      * <i>not</i> equal {@code start}.  Moreover, which
   930      * particular bit patterns represent signaling NaNs is platform
   931      * dependent; although all NaN bit patterns, quiet or signaling,
   932      * must be in the NaN range identified above.
   933      *
   934      * @param   bits   any {@code long} integer.
   935      * @return  the {@code double} floating-point value with the same
   936      *          bit pattern.
   937      */
   938     @JavaScriptBody(args={ "bits" },
   939         body=
   940           "var hi = bits.high32();\n"
   941         + "var s = (hi & 0x80000000) === 0 ? 1 : -1;\n"
   942         + "var e = (hi >> 20) & 0x7ff;\n"
   943         + "if (e === 0x7ff) {\n"
   944         + "  if ((bits == 0) && ((hi & 0xfffff) === 0)) {\n"
   945         + "    return (s > 0) ? Number.POSITIVE_INFINITY"
   946                           + " : Number.NEGATIVE_INFINITY;\n"
   947         + "  }\n"
   948         + "  return Number.NaN;\n"
   949         + "}\n"
   950         + "var m = (hi & 0xfffff).next32(bits);\n"
   951         + "if (e === 0) {\n"
   952         + "  m = m.shl64(1);\n"
   953         + "} else {\n"
   954         + "  m.hi = m.high32() | 0x100000;\n"
   955         + "}\n"
   956         + "return s * m.toFP() * Math.pow(2.0, e - 1075);\n"
   957     )
   958     public static native double longBitsToDouble(long bits);
   959 
   960     /**
   961      * Compares two {@code Double} objects numerically.  There
   962      * are two ways in which comparisons performed by this method
   963      * differ from those performed by the Java language numerical
   964      * comparison operators ({@code <, <=, ==, >=, >})
   965      * when applied to primitive {@code double} values:
   966      * <ul><li>
   967      *          {@code Double.NaN} is considered by this method
   968      *          to be equal to itself and greater than all other
   969      *          {@code double} values (including
   970      *          {@code Double.POSITIVE_INFINITY}).
   971      * <li>
   972      *          {@code 0.0d} is considered by this method to be greater
   973      *          than {@code -0.0d}.
   974      * </ul>
   975      * This ensures that the <i>natural ordering</i> of
   976      * {@code Double} objects imposed by this method is <i>consistent
   977      * with equals</i>.
   978      *
   979      * @param   anotherDouble   the {@code Double} to be compared.
   980      * @return  the value {@code 0} if {@code anotherDouble} is
   981      *          numerically equal to this {@code Double}; a value
   982      *          less than {@code 0} if this {@code Double}
   983      *          is numerically less than {@code anotherDouble};
   984      *          and a value greater than {@code 0} if this
   985      *          {@code Double} is numerically greater than
   986      *          {@code anotherDouble}.
   987      *
   988      * @since   1.2
   989      */
   990     public int compareTo(Double anotherDouble) {
   991         return Double.compare(value, anotherDouble.value);
   992     }
   993 
   994     /**
   995      * Compares the two specified {@code double} values. The sign
   996      * of the integer value returned is the same as that of the
   997      * integer that would be returned by the call:
   998      * <pre>
   999      *    new Double(d1).compareTo(new Double(d2))
  1000      * </pre>
  1001      *
  1002      * @param   d1        the first {@code double} to compare
  1003      * @param   d2        the second {@code double} to compare
  1004      * @return  the value {@code 0} if {@code d1} is
  1005      *          numerically equal to {@code d2}; a value less than
  1006      *          {@code 0} if {@code d1} is numerically less than
  1007      *          {@code d2}; and a value greater than {@code 0}
  1008      *          if {@code d1} is numerically greater than
  1009      *          {@code d2}.
  1010      * @since 1.4
  1011      */
  1012     public static int compare(double d1, double d2) {
  1013         if (d1 < d2)
  1014             return -1;           // Neither val is NaN, thisVal is smaller
  1015         if (d1 > d2)
  1016             return 1;            // Neither val is NaN, thisVal is larger
  1017 
  1018         // Cannot use doubleToRawLongBits because of possibility of NaNs.
  1019         long thisBits    = Double.doubleToLongBits(d1);
  1020         long anotherBits = Double.doubleToLongBits(d2);
  1021 
  1022         return (thisBits == anotherBits ?  0 : // Values are equal
  1023                 (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
  1024                  1));                          // (0.0, -0.0) or (NaN, !NaN)
  1025     }
  1026 
  1027     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  1028     private static final long serialVersionUID = -9172774392245257468L;
  1029 }