src/java.base/share/classes/java/lang/Double.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 46148 jdk/src/java.base/share/classes/java/lang/Double.java@6d8e27cd2f1e
child 52914 4fa75d8ad418
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 37521
diff changeset
     2
 * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3964
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3964
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3964
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3964
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3964
diff changeset
    23
 * questions.
2
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
34781
479b1724ab80 8145990: Move sun.misc math support classes to jdk.internal.math
chegar
parents: 31671
diff changeset
    28
import jdk.internal.math.FloatingDecimal;
479b1724ab80 8145990: Move sun.misc math support classes to jdk.internal.math
chegar
parents: 31671
diff changeset
    29
import jdk.internal.math.DoubleConsts;
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
    30
import jdk.internal.HotSpotIntrinsicCandidate;
2
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
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 22634
diff changeset
    47
 * @since 1.0
2
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
    /**
14507
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
   126
     * The number of bytes used to represent a {@code double} value.
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
   127
     *
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
   128
     * @since 1.8
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
   129
     */
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
   130
    public static final int BYTES = SIZE / Byte.SIZE;
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
   131
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
   132
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * The {@code Class} instance representing the primitive type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * {@code double}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 22634
diff changeset
   136
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
11275
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11016
diff changeset
   138
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public static final Class<Double>   TYPE = (Class<Double>) Class.getPrimitiveClass("double");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Returns a string representation of the {@code double}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * argument. All characters mentioned below are ASCII characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <li>If the argument is NaN, the result is the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *     "{@code NaN}".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <li>Otherwise, the result is a string that represents the sign and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * magnitude (absolute value) of the argument. If the sign is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * the first character of the result is '{@code -}'
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11275
diff changeset
   150
     * ({@code '\u005Cu002D'}); if the sign is positive, no sign character
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * appears in the result. As for the magnitude <i>m</i>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <li>If <i>m</i> is infinity, it is represented by the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * {@code "Infinity"}; thus, positive infinity produces the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * {@code "Infinity"} and negative infinity produces the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * {@code "-Infinity"}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <li>If <i>m</i> is zero, it is represented by the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * {@code "0.0"}; thus, negative zero produces the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * {@code "-0.0"} and positive zero produces the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * {@code "0.0"}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <li>If <i>m</i> is greater than or equal to 10<sup>-3</sup> but less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * than 10<sup>7</sup>, then it is represented as the integer part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <i>m</i>, in decimal form with no leading zeroes, followed by
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11275
diff changeset
   166
     * '{@code .}' ({@code '\u005Cu002E'}), followed by one or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * more decimal digits representing the fractional part of <i>m</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <li>If <i>m</i> is less than 10<sup>-3</sup> or greater than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * equal to 10<sup>7</sup>, then it is represented in so-called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * "computerized scientific notation." Let <i>n</i> be the unique
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * integer such that 10<sup><i>n</i></sup> &le; <i>m</i> {@literal <}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * mathematically exact quotient of <i>m</i> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * 10<sup><i>n</i></sup> so that 1 &le; <i>a</i> {@literal <} 10. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * magnitude is then represented as the integer part of <i>a</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * as a single decimal digit, followed by '{@code .}'
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11275
diff changeset
   178
     * ({@code '\u005Cu002E'}), followed by decimal digits
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * representing the fractional part of <i>a</i>, followed by the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11275
diff changeset
   180
     * letter '{@code E}' ({@code '\u005Cu0045'}), followed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * by a representation of <i>n</i> as a decimal integer, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * produced by the method {@link Integer#toString(int)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * How many digits must be printed for the fractional part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <i>m</i> or <i>a</i>? There must be at least one digit to represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * the fractional part, and beyond that as many, but only as many, more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * digits as are needed to uniquely distinguish the argument value from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * adjacent values of type {@code double}. That is, suppose that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <i>x</i> is the exact mathematical value represented by the decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * representation produced by this method for a finite nonzero argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * <i>d</i>. Then <i>d</i> must be the {@code double} value nearest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * to <i>x</i>; or if two {@code double} values are equally close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * to <i>x</i>, then <i>d</i> must be one of them and the least
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * significant bit of the significand of <i>d</i> must be {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <p>To create localized string representations of a floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * value, use subclasses of {@link java.text.NumberFormat}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param   d   the {@code double} to be converted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @return a string representation of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public static String toString(double d) {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15525
diff changeset
   204
        return FloatingDecimal.toJavaFormatString(d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Returns a hexadecimal string representation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * {@code double} argument. All characters mentioned below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * are ASCII characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * <li>If the argument is NaN, the result is the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *     "{@code NaN}".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <li>Otherwise, the result is a string that represents the sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * and magnitude of the argument. If the sign is negative, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * first character of the result is '{@code -}'
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11275
diff changeset
   218
     * ({@code '\u005Cu002D'}); if the sign is positive, no sign
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * character appears in the result. As for the magnitude <i>m</i>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * <li>If <i>m</i> is infinity, it is represented by the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * {@code "Infinity"}; thus, positive infinity produces the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * result {@code "Infinity"} and negative infinity produces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * the result {@code "-Infinity"}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <li>If <i>m</i> is zero, it is represented by the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * {@code "0x0.0p0"}; thus, negative zero produces the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * {@code "-0x0.0p0"} and positive zero produces the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * {@code "0x0.0p0"}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <li>If <i>m</i> is a {@code double} value with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * normalized representation, substrings are used to represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * significand and exponent fields.  The significand is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * represented by the characters {@code "0x1."}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * followed by a lowercase hexadecimal representation of the rest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * of the significand as a fraction.  Trailing zeros in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * hexadecimal representation are removed unless all the digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * are zero, in which case a single zero is used. Next, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * exponent is represented by {@code "p"} followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * by a decimal string of the unbiased exponent as if produced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * a call to {@link Integer#toString(int) Integer.toString} on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * exponent value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * <li>If <i>m</i> is a {@code double} value with a subnormal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * representation, the significand is represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * characters {@code "0x0."} followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * hexadecimal representation of the rest of the significand as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * fraction.  Trailing zeros in the hexadecimal representation are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * removed. Next, the exponent is represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * {@code "p-1022"}.  Note that there must be at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * least one nonzero digit in a subnormal significand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45124
diff changeset
   258
     * <table class="striped">
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18546
diff changeset
   259
     * <caption>Examples</caption>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 37521
diff changeset
   260
     * <thead>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45124
diff changeset
   261
     * <tr><th scope="col">Floating-point Value</th><th scope="col">Hexadecimal String</th>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 37521
diff changeset
   262
     * </thead>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45124
diff changeset
   263
     * <tbody style="text-align:right">
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45124
diff changeset
   264
     * <tr><th scope="row">{@code 1.0}</th> <td>{@code 0x1.0p0}</td>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45124
diff changeset
   265
     * <tr><th scope="row">{@code -1.0}</th>        <td>{@code -0x1.0p0}</td>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45124
diff changeset
   266
     * <tr><th scope="row">{@code 2.0}</th> <td>{@code 0x1.0p1}</td>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45124
diff changeset
   267
     * <tr><th scope="row">{@code 3.0}</th> <td>{@code 0x1.8p1}</td>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45124
diff changeset
   268
     * <tr><th scope="row">{@code 0.5}</th> <td>{@code 0x1.0p-1}</td>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45124
diff changeset
   269
     * <tr><th scope="row">{@code 0.25}</th>        <td>{@code 0x1.0p-2}</td>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45124
diff changeset
   270
     * <tr><th scope="row">{@code Double.MAX_VALUE}</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *     <td>{@code 0x1.fffffffffffffp1023}</td>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45124
diff changeset
   272
     * <tr><th scope="row">{@code Minimum Normal Value}</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *     <td>{@code 0x1.0p-1022}</td>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45124
diff changeset
   274
     * <tr><th scope="row">{@code Maximum Subnormal Value}</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *     <td>{@code 0x0.fffffffffffffp-1022}</td>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45124
diff changeset
   276
     * <tr><th scope="row">{@code Double.MIN_VALUE}</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *     <td>{@code 0x0.0000000000001p-1022}</td>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 37521
diff changeset
   278
     * </tbody>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param   d   the {@code double} to be converted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @return a hex string representation of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public static String toHexString(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         * Modeled after the "a" conversion specifier in C99, section
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         * 7.19.6.1; however, the output of this method is more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         * tightly specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         */
10608
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
   291
        if (!isFinite(d) )
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            // For infinity and NaN, use the decimal output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return Double.toString(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            // Initialized to maximum size of output.
15525
0308cc37489b 6964528: Double.toHexString(double d) String manipulation with + in an append of StringBuilder
darcy
parents: 15311
diff changeset
   296
            StringBuilder answer = new StringBuilder(24);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10067
diff changeset
   298
            if (Math.copySign(1.0, d) == -1.0)    // value is negative,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                answer.append("-");                  // so append sign info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            answer.append("0x");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            d = Math.abs(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if(d == 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                answer.append("0.0p0");
15525
0308cc37489b 6964528: Double.toHexString(double d) String manipulation with + in an append of StringBuilder
darcy
parents: 15311
diff changeset
   307
            } else {
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
   308
                boolean subnormal = (d < Double.MIN_NORMAL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                // Isolate significand bits and OR in a high-order bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                // so that the string representation has a known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                // length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                long signifBits = (Double.doubleToLongBits(d)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                   & DoubleConsts.SIGNIF_BIT_MASK) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    0x1000000000000000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                // Subnormal values have a 0 implicit bit; normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                // values have a 1 implicit bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                answer.append(subnormal ? "0." : "1.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                // Isolate the low-order 13 digits of the hex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                // representation.  If all the digits are zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                // replace with a single 0; otherwise, remove all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                // trailing zeros.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                String signif = Long.toHexString(signifBits).substring(3,16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                answer.append(signif.equals("0000000000000") ? // 13 zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                              "0":
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                              signif.replaceFirst("0{1,12}$", ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
15525
0308cc37489b 6964528: Double.toHexString(double d) String manipulation with + in an append of StringBuilder
darcy
parents: 15311
diff changeset
   330
                answer.append('p');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                // If the value is subnormal, use the E_min exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                // value for double; otherwise, extract and report d's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                // exponent (the representation of a subnormal uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                // E_min -1).
15525
0308cc37489b 6964528: Double.toHexString(double d) String manipulation with + in an append of StringBuilder
darcy
parents: 15311
diff changeset
   335
                answer.append(subnormal ?
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
   336
                              Double.MIN_EXPONENT:
15525
0308cc37489b 6964528: Double.toHexString(double d) String manipulation with + in an append of StringBuilder
darcy
parents: 15311
diff changeset
   337
                              Math.getExponent(d));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return answer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Returns a {@code Double} object holding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * {@code double} value represented by the argument string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * {@code s}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * <p>If {@code s} is {@code null}, then a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * {@code NullPointerException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * <p>Leading and trailing whitespace characters in {@code s}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * are ignored.  Whitespace is removed as if by the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * String#trim} method; that is, both ASCII space and control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * characters are removed. The rest of {@code s} should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * constitute a <i>FloatValue</i> as described by the lexical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * syntax rules:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * <dt><i>FloatValue:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * <dd><i>Sign<sub>opt</sub></i> {@code NaN}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <dd><i>Sign<sub>opt</sub></i> {@code Infinity}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <dd><i>SignedInteger</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * <dt><i>HexFloatingPointLiteral</i>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * <dt><i>HexSignificand:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * <dd><i>HexNumeral</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <dd><i>HexNumeral</i> {@code .}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <dd>{@code 0x} <i>HexDigits<sub>opt</sub>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *     </i>{@code .}<i> HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <dd>{@code 0X}<i> HexDigits<sub>opt</sub>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *     </i>{@code .} <i>HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * <dt><i>BinaryExponent:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * <dd><i>BinaryExponentIndicator SignedInteger</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * <dt><i>BinaryExponentIndicator:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * <dd>{@code p}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <dd>{@code P}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * <i>FloatTypeSuffix</i> are as defined in the lexical structure
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7517
diff changeset
   399
     * sections of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7517
diff changeset
   400
     * <cite>The Java&trade; Language Specification</cite>,
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7517
diff changeset
   401
     * except that underscores are not accepted between digits.
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7517
diff changeset
   402
     * If {@code s} does not have the form of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * a <i>FloatValue</i>, then a {@code NumberFormatException}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * is thrown. Otherwise, {@code s} is regarded as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * representing an exact decimal value in the usual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * "computerized scientific notation" or as an exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * hexadecimal value; this exact numerical value is then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * conceptually converted to an "infinitely precise"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * binary value that is then rounded to type {@code double}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * by the usual round-to-nearest rule of IEEE 754 floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * arithmetic, which includes preserving the sign of a zero
1824
7a685390c6ab 6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents: 2
diff changeset
   412
     * value.
7a685390c6ab 6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents: 2
diff changeset
   413
     *
7a685390c6ab 6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents: 2
diff changeset
   414
     * Note that the round-to-nearest rule also implies overflow and
7a685390c6ab 6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents: 2
diff changeset
   415
     * underflow behaviour; if the exact value of {@code s} is large
7a685390c6ab 6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents: 2
diff changeset
   416
     * enough in magnitude (greater than or equal to ({@link
7a685390c6ab 6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents: 2
diff changeset
   417
     * #MAX_VALUE} + {@link Math#ulp(double) ulp(MAX_VALUE)}/2),
7a685390c6ab 6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents: 2
diff changeset
   418
     * rounding to {@code double} will result in an infinity and if the
7a685390c6ab 6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents: 2
diff changeset
   419
     * exact value of {@code s} is small enough in magnitude (less
7a685390c6ab 6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents: 2
diff changeset
   420
     * than or equal to {@link #MIN_VALUE}/2), rounding to float will
7a685390c6ab 6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents: 2
diff changeset
   421
     * result in a zero.
7a685390c6ab 6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents: 2
diff changeset
   422
     *
7a685390c6ab 6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents: 2
diff changeset
   423
     * Finally, after rounding a {@code Double} object representing
7a685390c6ab 6604864: Double.valueOf(String) does not specify behaviour for overflow and underflow
darcy
parents: 2
diff changeset
   424
     * this {@code double} value is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * <p> To interpret localized string representations of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * floating-point value, use subclasses of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * java.text.NumberFormat}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * <p>Note that trailing format specifiers, specifiers that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * determine the type of a floating-point literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * ({@code 1.0f} is a {@code float} value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * {@code 1.0d} is a {@code double} value), do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * <em>not</em> influence the results of this method.  In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * words, the numerical value of the input string is converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * directly to the target floating-point type.  The two-step
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * sequence of conversions, string to {@code float} followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * by {@code float} to {@code double}, is <em>not</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * equivalent to converting a string directly to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * {@code double}. For example, the {@code float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * literal {@code 0.1f} is equal to the {@code double}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * value {@code 0.10000000149011612}; the {@code float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * literal {@code 0.1f} represents a different numerical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * value than the {@code double} literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * {@code 0.1}. (The numerical value 0.1 cannot be exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * represented in a binary floating-point number.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * <p>To avoid calling this method on an invalid string and having
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * a {@code NumberFormatException} be thrown, the regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * expression below can be used to screen the input string:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18156
diff changeset
   452
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *  final String Digits     = "(\\p{Digit}+)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *  final String HexDigits  = "(\\p{XDigit}+)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *  // an exponent is 'e' or 'E' followed by an optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *  // signed decimal integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *  final String Exp        = "[eE][+-]?"+Digits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *  final String fpRegex    =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *      ("[\\x00-\\x20]*"+  // Optional leading "whitespace"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *       "[+-]?(" + // Optional sign character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *       "NaN|" +           // "NaN" string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *       "Infinity|" +      // "Infinity" string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *       // A decimal floating-point string representing a finite positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *       // number without a leading sign has at most five basic pieces:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *       // Digits . Digits ExponentPart FloatTypeSuffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *       //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *       // Since this method allows integer-only strings as input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *       // in addition to strings of floating-point literals, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *       // two sub-patterns below are simplifications of the grammar
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7517
diff changeset
   471
     *       // productions from section 3.10.2 of
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18156
diff changeset
   472
     *       // The Java Language Specification.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *       // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *       "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *       // . Digits ExponentPart_opt FloatTypeSuffix_opt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *       "(\\.("+Digits+")("+Exp+")?)|"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *       // Hexadecimal strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *       "((" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *        // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *        "(0[xX]" + HexDigits + "(\\.)?)|" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *        // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *        "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *        ")[pP][+-]?" + Digits + "))" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *       "[fFdD]?))" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *       "[\\x00-\\x20]*");// Optional trailing "whitespace"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *  if (Pattern.matches(fpRegex, myString))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *      Double.valueOf(myString); // Will not throw NumberFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *      // Perform suitable alternative action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *  }
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18156
diff changeset
   497
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @param      s   the string to be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @return     a {@code Double} object holding the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *             represented by the {@code String} argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @throws     NumberFormatException  if the string does not contain a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *             parsable number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    public static Double valueOf(String s) throws NumberFormatException {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15525
diff changeset
   506
        return new Double(parseDouble(s));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Returns a {@code Double} instance representing the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * If a new {@code Double} instance is not required, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * should generally be used in preference to the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * {@link #Double(double)}, as this method is likely to yield
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * significantly better space and time performance by caching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * frequently requested values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @param  d a double value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @return a {@code Double} instance representing {@code d}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   522
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    public static Double valueOf(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return new Double(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Returns a new {@code double} initialized to the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * represented by the specified {@code String}, as performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * by the {@code valueOf} method of class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * {@code Double}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @param  s   the string to be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @return the {@code double} value represented by the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *         argument.
3312
d8cd9665ece8 6463998: Undocumented NullPointerExeption from Float.parseFloat and Double.parseDouble
darcy
parents: 1824
diff changeset
   536
     * @throws NullPointerException  if the string is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @throws NumberFormatException if the string does not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *         a parsable {@code double}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @see    java.lang.Double#valueOf(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    public static double parseDouble(String s) throws NumberFormatException {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15525
diff changeset
   543
        return FloatingDecimal.parseDouble(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * Returns {@code true} if the specified number is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * Not-a-Number (NaN) value, {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @param   v   the value to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @return  {@code true} if the value of the argument is NaN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *          {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     */
10608
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
   554
    public static boolean isNaN(double v) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        return (v != v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Returns {@code true} if the specified number is infinitely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * large in magnitude, {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @param   v   the value to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @return  {@code true} if the value of the argument is positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *          infinity or negative infinity; {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
10608
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
   566
    public static boolean isInfinite(double v) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /**
10608
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
   571
     * Returns {@code true} if the argument is a finite floating-point
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
   572
     * value; returns {@code false} otherwise (for NaN and infinity
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
   573
     * arguments).
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
   574
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
   575
     * @param d the {@code double} value to be tested
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
   576
     * @return {@code true} if the argument is a finite
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
   577
     * floating-point value, {@code false} otherwise.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
   578
     * @since 1.8
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
   579
     */
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
   580
    public static boolean isFinite(double d) {
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
   581
        return Math.abs(d) <= Double.MAX_VALUE;
10608
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
   582
    }
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
   583
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
   584
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * The value of the Double.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    private final double value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Constructs a newly allocated {@code Double} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * represents the primitive {@code double} argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @param   value   the value to be represented by the {@code Double}.
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35979
diff changeset
   596
     *
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35979
diff changeset
   597
     * @deprecated
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35979
diff changeset
   598
     * It is rarely appropriate to use this constructor. The static factory
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35979
diff changeset
   599
     * {@link #valueOf(double)} is generally a better choice, as it is
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35979
diff changeset
   600
     * likely to yield significantly better space and time performance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35979
diff changeset
   602
    @Deprecated(since="9")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    public Double(double value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * Constructs a newly allocated {@code Double} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * represents the floating-point value of type {@code double}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * represented by the string. The string is converted to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * {@code double} value as if by the {@code valueOf} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @param  s  a string to be converted to a {@code Double}.
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35979
diff changeset
   614
     * @throws    NumberFormatException if the string does not contain a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *            parsable number.
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35979
diff changeset
   616
     *
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35979
diff changeset
   617
     * @deprecated
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35979
diff changeset
   618
     * It is rarely appropriate to use this constructor.
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35979
diff changeset
   619
     * Use {@link #parseDouble(String)} to convert a string to a
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35979
diff changeset
   620
     * {@code double} primitive, or use {@link #valueOf(String)}
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35979
diff changeset
   621
     * to convert a string to a {@code Double} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35979
diff changeset
   623
    @Deprecated(since="9")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    public Double(String s) throws NumberFormatException {
11016
e2665f4ac6d2 7110111: Minor Java SE javadoc & Constructor clean up
lancea
parents: 10608
diff changeset
   625
        value = parseDouble(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * Returns {@code true} if this {@code Double} value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * a Not-a-Number (NaN), {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @return  {@code true} if the value represented by this object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *          NaN; {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    public boolean isNaN() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        return isNaN(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * Returns {@code true} if this {@code Double} value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * infinitely large in magnitude, {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @return  {@code true} if the value represented by this object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *          positive infinity or negative infinity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *          {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    public boolean isInfinite() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        return isInfinite(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * Returns a string representation of this {@code Double} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * The primitive {@code double} value represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * object is converted to a string exactly as if by the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * {@code toString} of one argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @return  a {@code String} representation of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @see java.lang.Double#toString(double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    public String toString() {
3964
cf913644be58 6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents: 3312
diff changeset
   661
        return toString(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    /**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   665
     * Returns the value of this {@code Double} as a {@code byte}
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   666
     * after a narrowing primitive conversion.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @return  the {@code double} value represented by this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *          converted to type {@code byte}
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   670
     * @jls 5.1.3 Narrowing Primitive Conversions
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 22634
diff changeset
   671
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    public byte byteValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        return (byte)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    /**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   678
     * Returns the value of this {@code Double} as a {@code short}
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   679
     * after a narrowing primitive conversion.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @return  the {@code double} value represented by this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *          converted to type {@code short}
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   683
     * @jls 5.1.3 Narrowing Primitive Conversions
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 22634
diff changeset
   684
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    public short shortValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        return (short)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    /**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   691
     * Returns the value of this {@code Double} as an {@code int}
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   692
     * after a narrowing primitive conversion.
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   693
     * @jls 5.1.3 Narrowing Primitive Conversions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @return  the {@code double} value represented by this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *          converted to type {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    public int intValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        return (int)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    /**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   703
     * Returns the value of this {@code Double} as a {@code long}
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   704
     * after a narrowing primitive conversion.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * @return  the {@code double} value represented by this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     *          converted to type {@code long}
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   708
     * @jls 5.1.3 Narrowing Primitive Conversions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    public long longValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        return (long)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    /**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   715
     * Returns the value of this {@code Double} as a {@code float}
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   716
     * after a narrowing primitive conversion.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @return  the {@code double} value represented by this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *          converted to type {@code float}
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   720
     * @jls 5.1.3 Narrowing Primitive Conversions
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 22634
diff changeset
   721
     * @since 1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    public float floatValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        return (float)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    /**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   728
     * Returns the {@code double} value of this {@code Double} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @return the {@code double} value represented by this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   732
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    public double doubleValue() {
11275
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11016
diff changeset
   734
        return value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * Returns a hash code for this {@code Double} object. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * result is the exclusive OR of the two halves of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * {@code long} integer bit representation, exactly as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * produced by the method {@link #doubleToLongBits(double)}, of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * the primitive {@code double} value represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * {@code Double} object. That is, the hash code is the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * of the expression:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     *  {@code (int)(v^(v>>>32))}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * where {@code v} is defined by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *  {@code long v = Double.doubleToLongBits(this.doubleValue());}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * @return  a {@code hash code} value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     */
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   758
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    public int hashCode() {
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   760
        return Double.hashCode(value);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   761
    }
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   762
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   763
    /**
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   764
     * Returns a hash code for a {@code double} value; compatible with
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   765
     * {@code Double.hashCode()}.
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   766
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18156
diff changeset
   767
     * @param value the value to hash
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18156
diff changeset
   768
     * @return a hash code value for a {@code double} value.
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   769
     * @since 1.8
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   770
     */
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   771
    public static int hashCode(double value) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        long bits = doubleToLongBits(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        return (int)(bits ^ (bits >>> 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * Compares this object against the specified object.  The result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * is {@code true} if and only if the argument is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * {@code null} and is a {@code Double} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * represents a {@code double} that has the same value as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * {@code double} represented by this object. For this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * purpose, two {@code double} values are considered to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * the same if and only if the method {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * #doubleToLongBits(double)} returns the identical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * {@code long} value when applied to each.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * <p>Note that in most cases, for two instances of class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * {@code Double}, {@code d1} and {@code d2}, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * value of {@code d1.equals(d2)} is {@code true} if and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     *  {@code d1.doubleValue() == d2.doubleValue()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * <p>also has the value {@code true}. However, there are two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * exceptions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * <li>If {@code d1} and {@code d2} both represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *     {@code Double.NaN}, then the {@code equals} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *     returns {@code true}, even though
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     *     {@code Double.NaN==Double.NaN} has the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *     {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * <li>If {@code d1} represents {@code +0.0} while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *     {@code d2} represents {@code -0.0}, or vice versa,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     *     the {@code equal} test has the value {@code false},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     *     even though {@code +0.0==-0.0} has the value {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * This definition allows hash tables to operate properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @param   obj   the object to compare with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @return  {@code true} if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *          {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @see java.lang.Double#doubleToLongBits(double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        return (obj instanceof Double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
               && (doubleToLongBits(((Double)obj).value) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                      doubleToLongBits(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * Returns a representation of the specified floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * according to the IEEE 754 floating-point "double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * format" bit layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * <p>Bit 63 (the bit that is selected by the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * {@code 0x8000000000000000L}) represents the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * floating-point number. Bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * 62-52 (the bits that are selected by the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * (the bits that are selected by the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * {@code 0x000fffffffffffffL}) represent the significand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * (sometimes called the mantissa) of the floating-point number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * <p>If the argument is positive infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * {@code 0x7ff0000000000000L}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * <p>If the argument is negative infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * {@code 0xfff0000000000000L}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * <p>If the argument is NaN, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * {@code 0x7ff8000000000000L}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * <p>In all cases, the result is a {@code long} integer that, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * given to the {@link #longBitsToDouble(long)} method, will produce a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * floating-point value the same as the argument to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * {@code doubleToLongBits} (except all NaN values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * collapsed to a single "canonical" NaN value).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * @param   value   a {@code double} precision floating-point number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @return the bits that represent the floating-point number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   853
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    public static long doubleToLongBits(double value) {
22290
6d4551cf4b3e 6667086: Double.doubleToLongBits(final double value) contains inefficient test for NaN
bpb
parents: 21334
diff changeset
   855
        if (!isNaN(value)) {
6d4551cf4b3e 6667086: Double.doubleToLongBits(final double value) contains inefficient test for NaN
bpb
parents: 21334
diff changeset
   856
            return doubleToRawLongBits(value);
6d4551cf4b3e 6667086: Double.doubleToLongBits(final double value) contains inefficient test for NaN
bpb
parents: 21334
diff changeset
   857
        }
6d4551cf4b3e 6667086: Double.doubleToLongBits(final double value) contains inefficient test for NaN
bpb
parents: 21334
diff changeset
   858
        return 0x7ff8000000000000L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * Returns a representation of the specified floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * according to the IEEE 754 floating-point "double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * format" bit layout, preserving Not-a-Number (NaN) values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * <p>Bit 63 (the bit that is selected by the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * {@code 0x8000000000000000L}) represents the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * floating-point number. Bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * 62-52 (the bits that are selected by the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * (the bits that are selected by the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * {@code 0x000fffffffffffffL}) represent the significand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * (sometimes called the mantissa) of the floating-point number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * <p>If the argument is positive infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * {@code 0x7ff0000000000000L}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * <p>If the argument is negative infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * {@code 0xfff0000000000000L}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * <p>If the argument is NaN, the result is the {@code long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * integer representing the actual NaN value.  Unlike the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * {@code doubleToLongBits} method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * {@code doubleToRawLongBits} does not collapse all the bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * patterns encoding a NaN to a single "canonical" NaN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * <p>In all cases, the result is a {@code long} integer that,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * when given to the {@link #longBitsToDouble(long)} method, will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * produce a floating-point value the same as the argument to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * {@code doubleToRawLongBits}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @param   value   a {@code double} precision floating-point number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @return the bits that represent the floating-point number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   897
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    public static native long doubleToRawLongBits(double value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * Returns the {@code double} value corresponding to a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * bit representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * The argument is considered to be a representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * floating-point value according to the IEEE 754 floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * "double format" bit layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * <p>If the argument is {@code 0x7ff0000000000000L}, the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * <p>If the argument is {@code 0xfff0000000000000L}, the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * is negative infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * <p>If the argument is any value in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * {@code 0x7ff0000000000001L} through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * {@code 0x7fffffffffffffffL} or in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * {@code 0xfff0000000000001L} through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * {@code 0xffffffffffffffffL}, the result is a NaN.  No IEEE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * 754 floating-point operation provided by Java can distinguish
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * between two NaN values of the same type with different bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * patterns.  Distinct values of NaN are only distinguishable by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * use of the {@code Double.doubleToRawLongBits} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * <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
   924
     * values that can be computed from the argument:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 18143
diff changeset
   926
     * <blockquote><pre>{@code
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 18143
diff changeset
   927
     * int s = ((bits >> 63) == 0) ? 1 : -1;
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 18143
diff changeset
   928
     * int e = (int)((bits >> 52) & 0x7ffL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * long m = (e == 0) ?
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 18143
diff changeset
   930
     *                 (bits & 0xfffffffffffffL) << 1 :
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     *                 (bits & 0xfffffffffffffL) | 0x10000000000000L;
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 18143
diff changeset
   932
     * }</pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * Then the floating-point result equals the value of the mathematical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * expression <i>s</i>&middot;<i>m</i>&middot;2<sup><i>e</i>-1075</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * <p>Note that this method may not be able to return a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * {@code double} NaN with exactly same bit pattern as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * {@code long} argument.  IEEE 754 distinguishes between two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * differences between the two kinds of NaN are generally not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * visible in Java.  Arithmetic operations on signaling NaNs turn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * them into quiet NaNs with a different, but often similar, bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * pattern.  However, on some processors merely copying a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * signaling NaN also performs that conversion.  In particular,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * copying a signaling NaN to return it to the calling method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * may perform this conversion.  So {@code longBitsToDouble}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * may not be able to return a {@code double} with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * signaling NaN bit pattern.  Consequently, for some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * {@code long} values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * {@code doubleToRawLongBits(longBitsToDouble(start))} may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * <i>not</i> equal {@code start}.  Moreover, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * particular bit patterns represent signaling NaNs is platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * dependent; although all NaN bit patterns, quiet or signaling,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * must be in the NaN range identified above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @param   bits   any {@code long} integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * @return  the {@code double} floating-point value with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     *          bit pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   961
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    public static native double longBitsToDouble(long bits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * Compares two {@code Double} objects numerically.  There
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * are two ways in which comparisons performed by this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * differ from those performed by the Java language numerical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * comparison operators ({@code <, <=, ==, >=, >})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * when applied to primitive {@code double} values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * <ul><li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     *          {@code Double.NaN} is considered by this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     *          to be equal to itself and greater than all other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     *          {@code double} values (including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     *          {@code Double.POSITIVE_INFINITY}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     *          {@code 0.0d} is considered by this method to be greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *          than {@code -0.0d}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * This ensures that the <i>natural ordering</i> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * {@code Double} objects imposed by this method is <i>consistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * with equals</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * @param   anotherDouble   the {@code Double} to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * @return  the value {@code 0} if {@code anotherDouble} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *          numerically equal to this {@code Double}; a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     *          less than {@code 0} if this {@code Double}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     *          is numerically less than {@code anotherDouble};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *          and a value greater than {@code 0} if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     *          {@code Double} is numerically greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     *          {@code anotherDouble}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    public int compareTo(Double anotherDouble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        return Double.compare(value, anotherDouble.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * Compares the two specified {@code double} values. The sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * of the integer value returned is the same as that of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * integer that would be returned by the call:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     *    new Double(d1).compareTo(new Double(d2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @param   d1        the first {@code double} to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * @param   d2        the second {@code double} to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * @return  the value {@code 0} if {@code d1} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     *          numerically equal to {@code d2}; a value less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     *          {@code 0} if {@code d1} is numerically less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     *          {@code d2}; and a value greater than {@code 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     *          if {@code d1} is numerically greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     *          {@code d2}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    public static int compare(double d1, double d2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        if (d1 < d2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            return -1;           // Neither val is NaN, thisVal is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        if (d1 > d2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            return 1;            // Neither val is NaN, thisVal is larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
7517
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
  1022
        // Cannot use doubleToRawLongBits because of possibility of NaNs.
7303bc0e78d6 7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0
darcy
parents: 5506
diff changeset
  1023
        long thisBits    = Double.doubleToLongBits(d1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        long anotherBits = Double.doubleToLongBits(d2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        return (thisBits == anotherBits ?  0 : // Values are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                 1));                          // (0.0, -0.0) or (NaN, !NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
15311
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1031
    /**
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1032
     * Adds two {@code double} values together as per the + operator.
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1033
     *
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1034
     * @param a the first operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1035
     * @param b the second operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1036
     * @return the sum of {@code a} and {@code b}
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1037
     * @jls 4.2.4 Floating-Point Operations
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1038
     * @see java.util.function.BinaryOperator
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1039
     * @since 1.8
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1040
     */
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1041
    public static double sum(double a, double b) {
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1042
        return a + b;
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1043
    }
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1044
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1045
    /**
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1046
     * Returns the greater of two {@code double} values
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1047
     * as if by calling {@link Math#max(double, double) Math.max}.
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1048
     *
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1049
     * @param a the first operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1050
     * @param b the second operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1051
     * @return the greater of {@code a} and {@code b}
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1052
     * @see java.util.function.BinaryOperator
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1053
     * @since 1.8
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1054
     */
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1055
    public static double max(double a, double b) {
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1056
        return Math.max(a, b);
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1057
    }
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1058
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1059
    /**
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1060
     * Returns the smaller of two {@code double} values
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1061
     * as if by calling {@link Math#min(double, double) Math.min}.
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1062
     *
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1063
     * @param a the first operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1064
     * @param b the second operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1065
     * @return the smaller of {@code a} and {@code b}.
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1066
     * @see java.util.function.BinaryOperator
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1067
     * @since 1.8
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1068
     */
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1069
    public static double min(double a, double b) {
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1070
        return Math.min(a, b);
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1071
    }
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14507
diff changeset
  1072
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    private static final long serialVersionUID = -9172774392245257468L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
}