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