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