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