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