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