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