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