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