• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.
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
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
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  *
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.
24  */
25 
26 package java.lang;
27 
28 // BEGIN Android-removed: dynamic constants not supported on Android.
29 /*
30 import java.lang.invoke.MethodHandles;
31 import java.lang.constant.Constable;
32 import java.lang.constant.ConstantDesc;
33 import java.util.Optional;
34 */
35 // END Android-removed: dynamic constants not supported on Android.
36 
37 import jdk.internal.math.FloatingDecimal;
38 import jdk.internal.math.DoubleConsts;
39 import jdk.internal.vm.annotation.IntrinsicCandidate;
40 
41 /**
42  * The {@code Double} class wraps a value of the primitive type
43  * {@code double} in an object. An object of type
44  * {@code Double} contains a single field whose type is
45  * {@code double}.
46  *
47  * <p>In addition, this class provides several methods for converting a
48  * {@code double} to a {@code String} and a
49  * {@code String} to a {@code double}, as well as other
50  * constants and methods useful when dealing with a
51  * {@code double}.
52  *
53  * <!-- Android-removed: paragraph on ValueBased
54  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
55  * class; programmers should treat instances that are
56  * {@linkplain #equals(Object) equal} as interchangeable and should not
57  * use instances for synchronization, or unpredictable behavior may
58  * occur. For example, in a future release, synchronization may fail.
59  * -->
60  *
61  * <h2><a id=equivalenceRelation>Floating-point Equality, Equivalence,
62  * and Comparison</a></h2>
63  *
64  * IEEE 754 floating-point values include finite nonzero values,
65  * signed zeros ({@code +0.0} and {@code -0.0}), signed infinities
66  * {@linkplain Double#POSITIVE_INFINITY positive infinity} and
67  * {@linkplain Double#NEGATIVE_INFINITY negative infinity}), and
68  * {@linkplain Double#NaN NaN} (not-a-number).
69  *
70  * <p>An <em>equivalence relation</em> on a set of values is a boolean
71  * relation on pairs of values that is reflexive, symmetric, and
72  * transitive. For more discussion of equivalence relations and object
73  * equality, see the {@link Object#equals Object.equals}
74  * specification. An equivalence relation partitions the values it
75  * operates over into sets called <i>equivalence classes</i>.  All the
76  * members of the equivalence class are equal to each other under the
77  * relation. An equivalence class may contain only a single member. At
78  * least for some purposes, all the members of an equivalence class
79  * are substitutable for each other.  In particular, in a numeric
80  * expression equivalent values can be <em>substituted</em> for one
81  * another without changing the result of the expression, meaning
82  * changing the equivalence class of the result of the expression.
83  *
84  * <p>Notably, the built-in {@code ==} operation on floating-point
85  * values is <em>not</em> an equivalence relation. Despite not
86  * defining an equivalence relation, the semantics of the IEEE 754
87  * {@code ==} operator were deliberately designed to meet other needs
88  * of numerical computation. There are two exceptions where the
89  * properties of an equivalence relation are not satisfied by {@code
90  * ==} on floating-point values:
91  *
92  * <ul>
93  *
94  * <li>If {@code v1} and {@code v2} are both NaN, then {@code v1
95  * == v2} has the value {@code false}. Therefore, for two NaN
96  * arguments the <em>reflexive</em> property of an equivalence
97  * relation is <em>not</em> satisfied by the {@code ==} operator.
98  *
99  * <li>If {@code v1} represents {@code +0.0} while {@code v2}
100  * represents {@code -0.0}, or vice versa, then {@code v1 == v2} has
101  * the value {@code true} even though {@code +0.0} and {@code -0.0}
102  * are distinguishable under various floating-point operations. For
103  * example, {@code 1.0/+0.0} evaluates to positive infinity while
104  * {@code 1.0/-0.0} evaluates to <em>negative</em> infinity and
105  * positive infinity and negative infinity are neither equal to each
106  * other nor equivalent to each other. Thus, while a signed zero input
107  * most commonly determines the sign of a zero result, because of
108  * dividing by zero, {@code +0.0} and {@code -0.0} may not be
109  * substituted for each other in general. The sign of a zero input
110  * also has a non-substitutable effect on the result of some math
111  * library methods.
112  *
113  * </ul>
114  *
115  * <p>For ordered comparisons using the built-in comparison operators
116  * ({@code <}, {@code <=}, etc.), NaN values have another anomalous
117  * situation: a NaN is neither less than, nor greater than, nor equal
118  * to any value, including itself. This means the <i>trichotomy of
119  * comparison</i> does <em>not</em> hold.
120  *
121  * <p>To provide the appropriate semantics for {@code equals} and
122  * {@code compareTo} methods, those methods cannot simply be wrappers
123  * around {@code ==} or ordered comparison operations. Instead, {@link
124  * Double#equals equals} defines NaN arguments to be equal to each
125  * other and defines {@code +0.0} to <em>not</em> be equal to {@code
126  * -0.0}, restoring reflexivity. For comparisons, {@link
127  * Double#compareTo compareTo} defines a total order where {@code
128  * -0.0} is less than {@code +0.0} and where a NaN is equal to itself
129  * and considered greater than positive infinity.
130  *
131  * <p>The operational semantics of {@code equals} and {@code
132  * compareTo} are expressed in terms of {@linkplain #doubleToLongBits
133  * bit-wise converting} the floating-point values to integral values.
134  *
135  * <p>The <em>natural ordering</em> implemented by {@link #compareTo
136  * compareTo} is {@linkplain Comparable consistent with equals}. That
137  * is, two objects are reported as equal by {@code equals} if and only
138  * if {@code compareTo} on those objects returns zero.
139  *
140  * <p>The adjusted behaviors defined for {@code equals} and {@code
141  * compareTo} allow instances of wrapper classes to work properly with
142  * conventional data structures. For example, defining NaN
143  * values to be {@code equals} to one another allows NaN to be used as
144  * an element of a {@link java.util.HashSet HashSet} or as the key of
145  * a {@link java.util.HashMap HashMap}. Similarly, defining {@code
146  * compareTo} as a total ordering, including {@code +0.0}, {@code
147  * -0.0}, and NaN, allows instances of wrapper classes to be used as
148  * elements of a {@link java.util.SortedSet SortedSet} or as keys of a
149  * {@link java.util.SortedMap SortedMap}.
150  *
151  * @jls 4.2.3 Floating-Point Types, Formats, and Values
152  * @jls 4.2.4. Floating-Point Operations
153  * @jls 15.21.1 Numerical Equality Operators == and !=
154  * @jls 15.20.1 Numerical Comparison Operators {@code <}, {@code <=}, {@code >}, and {@code >=}
155  *
156  * @author  Lee Boynton
157  * @author  Arthur van Hoff
158  * @author  Joseph D. Darcy
159  * @since 1.0
160  */
161 @jdk.internal.ValueBased
162 public final class Double extends Number
163         implements Comparable<Double>
164 // Android-removed: no Constable support.
165 // , Constable, ConstantDesc
166 {
167     /**
168      * A constant holding the positive infinity of type
169      * {@code double}. It is equal to the value returned by
170      * {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
171      */
172     public static final double POSITIVE_INFINITY = 1.0 / 0.0;
173 
174     /**
175      * A constant holding the negative infinity of type
176      * {@code double}. It is equal to the value returned by
177      * {@code Double.longBitsToDouble(0xfff0000000000000L)}.
178      */
179     public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
180 
181     /**
182      * A constant holding a Not-a-Number (NaN) value of type
183      * {@code double}. It is equivalent to the value returned by
184      * {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
185      */
186     public static final double NaN = 0.0d / 0.0;
187 
188     /**
189      * A constant holding the largest positive finite value of type
190      * {@code double},
191      * (2-2<sup>-52</sup>)&middot;2<sup>1023</sup>.  It is equal to
192      * the hexadecimal floating-point literal
193      * {@code 0x1.fffffffffffffP+1023} and also equal to
194      * {@code Double.longBitsToDouble(0x7fefffffffffffffL)}.
195      */
196     public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308
197 
198     /**
199      * A constant holding the smallest positive normal value of type
200      * {@code double}, 2<sup>-1022</sup>.  It is equal to the
201      * hexadecimal floating-point literal {@code 0x1.0p-1022} and also
202      * equal to {@code Double.longBitsToDouble(0x0010000000000000L)}.
203      *
204      * @since 1.6
205      */
206     public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308
207 
208     /**
209      * A constant holding the smallest positive nonzero value of type
210      * {@code double}, 2<sup>-1074</sup>. It is equal to the
211      * hexadecimal floating-point literal
212      * {@code 0x0.0000000000001P-1022} and also equal to
213      * {@code Double.longBitsToDouble(0x1L)}.
214      */
215     public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324
216 
217     /**
218      * Maximum exponent a finite {@code double} variable may have.
219      * It is equal to the value returned by
220      * {@code Math.getExponent(Double.MAX_VALUE)}.
221      *
222      * @since 1.6
223      */
224     public static final int MAX_EXPONENT = 1023;
225 
226     /**
227      * Minimum exponent a normalized {@code double} variable may
228      * have.  It is equal to the value returned by
229      * {@code Math.getExponent(Double.MIN_NORMAL)}.
230      *
231      * @since 1.6
232      */
233     public static final int MIN_EXPONENT = -1022;
234 
235     /**
236      * The number of bits used to represent a {@code double} value.
237      *
238      * @since 1.5
239      */
240     public static final int SIZE = 64;
241 
242     /**
243      * The number of bytes used to represent a {@code double} value.
244      *
245      * @since 1.8
246      */
247     public static final int BYTES = SIZE / Byte.SIZE;
248 
249     /**
250      * The {@code Class} instance representing the primitive type
251      * {@code double}.
252      *
253      * @since 1.1
254      */
255     @SuppressWarnings("unchecked")
256     public static final Class<Double>   TYPE = (Class<Double>) Class.getPrimitiveClass("double");
257 
258     /**
259      * Returns a string representation of the {@code double}
260      * argument. All characters mentioned below are ASCII characters.
261      * <ul>
262      * <li>If the argument is NaN, the result is the string
263      *     "{@code NaN}".
264      * <li>Otherwise, the result is a string that represents the sign and
265      * magnitude (absolute value) of the argument. If the sign is negative,
266      * the first character of the result is '{@code -}'
267      * ({@code '\u005Cu002D'}); if the sign is positive, no sign character
268      * appears in the result. As for the magnitude <i>m</i>:
269      * <ul>
270      * <li>If <i>m</i> is infinity, it is represented by the characters
271      * {@code "Infinity"}; thus, positive infinity produces the result
272      * {@code "Infinity"} and negative infinity produces the result
273      * {@code "-Infinity"}.
274      *
275      * <li>If <i>m</i> is zero, it is represented by the characters
276      * {@code "0.0"}; thus, negative zero produces the result
277      * {@code "-0.0"} and positive zero produces the result
278      * {@code "0.0"}.
279      *
280      * <li>If <i>m</i> is greater than or equal to 10<sup>-3</sup> but less
281      * than 10<sup>7</sup>, then it is represented as the integer part of
282      * <i>m</i>, in decimal form with no leading zeroes, followed by
283      * '{@code .}' ({@code '\u005Cu002E'}), followed by one or
284      * more decimal digits representing the fractional part of <i>m</i>.
285      *
286      * <li>If <i>m</i> is less than 10<sup>-3</sup> or greater than or
287      * equal to 10<sup>7</sup>, then it is represented in so-called
288      * "computerized scientific notation." Let <i>n</i> be the unique
289      * integer such that 10<sup><i>n</i></sup> &le; <i>m</i> {@literal <}
290      * 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
291      * mathematically exact quotient of <i>m</i> and
292      * 10<sup><i>n</i></sup> so that 1 &le; <i>a</i> {@literal <} 10. The
293      * magnitude is then represented as the integer part of <i>a</i>,
294      * as a single decimal digit, followed by '{@code .}'
295      * ({@code '\u005Cu002E'}), followed by decimal digits
296      * representing the fractional part of <i>a</i>, followed by the
297      * letter '{@code E}' ({@code '\u005Cu0045'}), followed
298      * by a representation of <i>n</i> as a decimal integer, as
299      * produced by the method {@link Integer#toString(int)}.
300      * </ul>
301      * </ul>
302      * How many digits must be printed for the fractional part of
303      * <i>m</i> or <i>a</i>? There must be at least one digit to represent
304      * the fractional part, and beyond that as many, but only as many, more
305      * digits as are needed to uniquely distinguish the argument value from
306      * adjacent values of type {@code double}. That is, suppose that
307      * <i>x</i> is the exact mathematical value represented by the decimal
308      * representation produced by this method for a finite nonzero argument
309      * <i>d</i>. Then <i>d</i> must be the {@code double} value nearest
310      * to <i>x</i>; or if two {@code double} values are equally close
311      * to <i>x</i>, then <i>d</i> must be one of them and the least
312      * significant bit of the significand of <i>d</i> must be {@code 0}.
313      *
314      * <p>To create localized string representations of a floating-point
315      * value, use subclasses of {@link java.text.NumberFormat}.
316      *
317      * @param   d   the {@code double} to be converted.
318      * @return a string representation of the argument.
319      */
toString(double d)320     public static String toString(double d) {
321         return FloatingDecimal.toJavaFormatString(d);
322     }
323 
324     /**
325      * Returns a hexadecimal string representation of the
326      * {@code double} argument. All characters mentioned below
327      * are ASCII characters.
328      *
329      * <ul>
330      * <li>If the argument is NaN, the result is the string
331      *     "{@code NaN}".
332      * <li>Otherwise, the result is a string that represents the sign
333      * and magnitude of the argument. If the sign is negative, the
334      * first character of the result is '{@code -}'
335      * ({@code '\u005Cu002D'}); if the sign is positive, no sign
336      * character appears in the result. As for the magnitude <i>m</i>:
337      *
338      * <ul>
339      * <li>If <i>m</i> is infinity, it is represented by the string
340      * {@code "Infinity"}; thus, positive infinity produces the
341      * result {@code "Infinity"} and negative infinity produces
342      * the result {@code "-Infinity"}.
343      *
344      * <li>If <i>m</i> is zero, it is represented by the string
345      * {@code "0x0.0p0"}; thus, negative zero produces the result
346      * {@code "-0x0.0p0"} and positive zero produces the result
347      * {@code "0x0.0p0"}.
348      *
349      * <li>If <i>m</i> is a {@code double} value with a
350      * normalized representation, substrings are used to represent the
351      * significand and exponent fields.  The significand is
352      * represented by the characters {@code "0x1."}
353      * followed by a lowercase hexadecimal representation of the rest
354      * of the significand as a fraction.  Trailing zeros in the
355      * hexadecimal representation are removed unless all the digits
356      * are zero, in which case a single zero is used. Next, the
357      * exponent is represented by {@code "p"} followed
358      * by a decimal string of the unbiased exponent as if produced by
359      * a call to {@link Integer#toString(int) Integer.toString} on the
360      * exponent value.
361      *
362      * <li>If <i>m</i> is a {@code double} value with a subnormal
363      * representation, the significand is represented by the
364      * characters {@code "0x0."} followed by a
365      * hexadecimal representation of the rest of the significand as a
366      * fraction.  Trailing zeros in the hexadecimal representation are
367      * removed. Next, the exponent is represented by
368      * {@code "p-1022"}.  Note that there must be at
369      * least one nonzero digit in a subnormal significand.
370      *
371      * </ul>
372      *
373      * </ul>
374      *
375      * <table class="striped">
376      * <caption>Examples</caption>
377      * <thead>
378      * <tr><th scope="col">Floating-point Value</th><th scope="col">Hexadecimal String</th>
379      * </thead>
380      * <tbody style="text-align:right">
381      * <tr><th scope="row">{@code 1.0}</th> <td>{@code 0x1.0p0}</td>
382      * <tr><th scope="row">{@code -1.0}</th>        <td>{@code -0x1.0p0}</td>
383      * <tr><th scope="row">{@code 2.0}</th> <td>{@code 0x1.0p1}</td>
384      * <tr><th scope="row">{@code 3.0}</th> <td>{@code 0x1.8p1}</td>
385      * <tr><th scope="row">{@code 0.5}</th> <td>{@code 0x1.0p-1}</td>
386      * <tr><th scope="row">{@code 0.25}</th>        <td>{@code 0x1.0p-2}</td>
387      * <tr><th scope="row">{@code Double.MAX_VALUE}</th>
388      *     <td>{@code 0x1.fffffffffffffp1023}</td>
389      * <tr><th scope="row">{@code Minimum Normal Value}</th>
390      *     <td>{@code 0x1.0p-1022}</td>
391      * <tr><th scope="row">{@code Maximum Subnormal Value}</th>
392      *     <td>{@code 0x0.fffffffffffffp-1022}</td>
393      * <tr><th scope="row">{@code Double.MIN_VALUE}</th>
394      *     <td>{@code 0x0.0000000000001p-1022}</td>
395      * </tbody>
396      * </table>
397      * @param   d   the {@code double} to be converted.
398      * @return a hex string representation of the argument.
399      * @since 1.5
400      * @author Joseph D. Darcy
401      */
toHexString(double d)402     public static String toHexString(double d) {
403         /*
404          * Modeled after the "a" conversion specifier in C99, section
405          * 7.19.6.1; however, the output of this method is more
406          * tightly specified.
407          */
408         if (!isFinite(d) )
409             // For infinity and NaN, use the decimal output.
410             return Double.toString(d);
411         else {
412             // Initialized to maximum size of output.
413             StringBuilder answer = new StringBuilder(24);
414 
415             if (Math.copySign(1.0, d) == -1.0)    // value is negative,
416                 answer.append("-");                  // so append sign info
417 
418             answer.append("0x");
419 
420             d = Math.abs(d);
421 
422             if(d == 0.0) {
423                 answer.append("0.0p0");
424             } else {
425                 boolean subnormal = (d < Double.MIN_NORMAL);
426 
427                 // Isolate significand bits and OR in a high-order bit
428                 // so that the string representation has a known
429                 // length.
430                 long signifBits = (Double.doubleToLongBits(d)
431                                    & DoubleConsts.SIGNIF_BIT_MASK) |
432                     0x1000000000000000L;
433 
434                 // Subnormal values have a 0 implicit bit; normal
435                 // values have a 1 implicit bit.
436                 answer.append(subnormal ? "0." : "1.");
437 
438                 // Isolate the low-order 13 digits of the hex
439                 // representation.  If all the digits are zero,
440                 // replace with a single 0; otherwise, remove all
441                 // trailing zeros.
442                 String signif = Long.toHexString(signifBits).substring(3,16);
443                 answer.append(signif.equals("0000000000000") ? // 13 zeros
444                               "0":
445                               signif.replaceFirst("0{1,12}$", ""));
446 
447                 answer.append('p');
448                 // If the value is subnormal, use the E_min exponent
449                 // value for double; otherwise, extract and report d's
450                 // exponent (the representation of a subnormal uses
451                 // E_min -1).
452                 answer.append(subnormal ?
453                               Double.MIN_EXPONENT:
454                               Math.getExponent(d));
455             }
456             return answer.toString();
457         }
458     }
459 
460     /**
461      * Returns a {@code Double} object holding the
462      * {@code double} value represented by the argument string
463      * {@code s}.
464      *
465      * <p>If {@code s} is {@code null}, then a
466      * {@code NullPointerException} is thrown.
467      *
468      * <p>Leading and trailing whitespace characters in {@code s}
469      * are ignored.  Whitespace is removed as if by the {@link
470      * String#trim} method; that is, both ASCII space and control
471      * characters are removed. The rest of {@code s} should
472      * constitute a <i>FloatValue</i> as described by the lexical
473      * syntax rules:
474      *
475      * <blockquote>
476      * <dl>
477      * <dt><i>FloatValue:</i>
478      * <dd><i>Sign<sub>opt</sub></i> {@code NaN}
479      * <dd><i>Sign<sub>opt</sub></i> {@code Infinity}
480      * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
481      * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
482      * <dd><i>SignedInteger</i>
483      * </dl>
484      *
485      * <dl>
486      * <dt><i>HexFloatingPointLiteral</i>:
487      * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
488      * </dl>
489      *
490      * <dl>
491      * <dt><i>HexSignificand:</i>
492      * <dd><i>HexNumeral</i>
493      * <dd><i>HexNumeral</i> {@code .}
494      * <dd>{@code 0x} <i>HexDigits<sub>opt</sub>
495      *     </i>{@code .}<i> HexDigits</i>
496      * <dd>{@code 0X}<i> HexDigits<sub>opt</sub>
497      *     </i>{@code .} <i>HexDigits</i>
498      * </dl>
499      *
500      * <dl>
501      * <dt><i>BinaryExponent:</i>
502      * <dd><i>BinaryExponentIndicator SignedInteger</i>
503      * </dl>
504      *
505      * <dl>
506      * <dt><i>BinaryExponentIndicator:</i>
507      * <dd>{@code p}
508      * <dd>{@code P}
509      * </dl>
510      *
511      * </blockquote>
512      *
513      * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
514      * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
515      * <i>FloatTypeSuffix</i> are as defined in the lexical structure
516      * sections of
517      * <cite>The Java Language Specification</cite>,
518      * except that underscores are not accepted between digits.
519      * If {@code s} does not have the form of
520      * a <i>FloatValue</i>, then a {@code NumberFormatException}
521      * is thrown. Otherwise, {@code s} is regarded as
522      * representing an exact decimal value in the usual
523      * "computerized scientific notation" or as an exact
524      * hexadecimal value; this exact numerical value is then
525      * conceptually converted to an "infinitely precise"
526      * binary value that is then rounded to type {@code double}
527      * by the usual round-to-nearest rule of IEEE 754 floating-point
528      * arithmetic, which includes preserving the sign of a zero
529      * value.
530      *
531      * Note that the round-to-nearest rule also implies overflow and
532      * underflow behaviour; if the exact value of {@code s} is large
533      * enough in magnitude (greater than or equal to ({@link
534      * #MAX_VALUE} + {@link Math#ulp(double) ulp(MAX_VALUE)}/2),
535      * rounding to {@code double} will result in an infinity and if the
536      * exact value of {@code s} is small enough in magnitude (less
537      * than or equal to {@link #MIN_VALUE}/2), rounding to float will
538      * result in a zero.
539      *
540      * Finally, after rounding a {@code Double} object representing
541      * this {@code double} value is returned.
542      *
543      * <p> To interpret localized string representations of a
544      * floating-point value, use subclasses of {@link
545      * java.text.NumberFormat}.
546      *
547      * <p>Note that trailing format specifiers, specifiers that
548      * determine the type of a floating-point literal
549      * ({@code 1.0f} is a {@code float} value;
550      * {@code 1.0d} is a {@code double} value), do
551      * <em>not</em> influence the results of this method.  In other
552      * words, the numerical value of the input string is converted
553      * directly to the target floating-point type.  The two-step
554      * sequence of conversions, string to {@code float} followed
555      * by {@code float} to {@code double}, is <em>not</em>
556      * equivalent to converting a string directly to
557      * {@code double}. For example, the {@code float}
558      * literal {@code 0.1f} is equal to the {@code double}
559      * value {@code 0.10000000149011612}; the {@code float}
560      * literal {@code 0.1f} represents a different numerical
561      * value than the {@code double} literal
562      * {@code 0.1}. (The numerical value 0.1 cannot be exactly
563      * represented in a binary floating-point number.)
564      *
565      * <p>To avoid calling this method on an invalid string and having
566      * a {@code NumberFormatException} be thrown, the regular
567      * expression below can be used to screen the input string:
568      *
569      * <pre>{@code
570      *  final String Digits     = "(\\p{Digit}+)";
571      *  final String HexDigits  = "(\\p{XDigit}+)";
572      *  // an exponent is 'e' or 'E' followed by an optionally
573      *  // signed decimal integer.
574      *  final String Exp        = "[eE][+-]?"+Digits;
575      *  final String fpRegex    =
576      *      ("[\\x00-\\x20]*"+  // Optional leading "whitespace"
577      *       "[+-]?(" + // Optional sign character
578      *       "NaN|" +           // "NaN" string
579      *       "Infinity|" +      // "Infinity" string
580      *
581      *       // A decimal floating-point string representing a finite positive
582      *       // number without a leading sign has at most five basic pieces:
583      *       // Digits . Digits ExponentPart FloatTypeSuffix
584      *       //
585      *       // Since this method allows integer-only strings as input
586      *       // in addition to strings of floating-point literals, the
587      *       // two sub-patterns below are simplifications of the grammar
588      *       // productions from section 3.10.2 of
589      *       // The Java Language Specification.
590      *
591      *       // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
592      *       "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
593      *
594      *       // . Digits ExponentPart_opt FloatTypeSuffix_opt
595      *       "(\\.("+Digits+")("+Exp+")?)|"+
596      *
597      *       // Hexadecimal strings
598      *       "((" +
599      *        // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
600      *        "(0[xX]" + HexDigits + "(\\.)?)|" +
601      *
602      *        // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
603      *        "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
604      *
605      *        ")[pP][+-]?" + Digits + "))" +
606      *       "[fFdD]?))" +
607      *       "[\\x00-\\x20]*");// Optional trailing "whitespace"
608      *
609      *  if (Pattern.matches(fpRegex, myString))
610      *      Double.valueOf(myString); // Will not throw NumberFormatException
611      *  else {
612      *      // Perform suitable alternative action
613      *  }
614      * }</pre>
615      *
616      * @param      s   the string to be parsed.
617      * @return     a {@code Double} object holding the value
618      *             represented by the {@code String} argument.
619      * @throws     NumberFormatException  if the string does not contain a
620      *             parsable number.
621      */
valueOf(String s)622     public static Double valueOf(String s) throws NumberFormatException {
623         return new Double(parseDouble(s));
624     }
625 
626     /**
627      * Returns a {@code Double} instance representing the specified
628      * {@code double} value.
629      * If a new {@code Double} instance is not required, this method
630      * should generally be used in preference to the constructor
631      * {@link #Double(double)}, as this method is likely to yield
632      * significantly better space and time performance by caching
633      * frequently requested values.
634      *
635      * @param  d a double value.
636      * @return a {@code Double} instance representing {@code d}.
637      * @since  1.5
638      */
639     @IntrinsicCandidate
valueOf(double d)640     public static Double valueOf(double d) {
641         return new Double(d);
642     }
643 
644     /**
645      * Returns a new {@code double} initialized to the value
646      * represented by the specified {@code String}, as performed
647      * by the {@code valueOf} method of class
648      * {@code Double}.
649      *
650      * @param  s   the string to be parsed.
651      * @return the {@code double} value represented by the string
652      *         argument.
653      * @throws NullPointerException  if the string is null
654      * @throws NumberFormatException if the string does not contain
655      *         a parsable {@code double}.
656      * @see    java.lang.Double#valueOf(String)
657      * @since 1.2
658      */
parseDouble(String s)659     public static double parseDouble(String s) throws NumberFormatException {
660         return FloatingDecimal.parseDouble(s);
661     }
662 
663     /**
664      * Returns {@code true} if the specified number is a
665      * Not-a-Number (NaN) value, {@code false} otherwise.
666      *
667      * @param   v   the value to be tested.
668      * @return  {@code true} if the value of the argument is NaN;
669      *          {@code false} otherwise.
670      */
isNaN(double v)671     public static boolean isNaN(double v) {
672         return (v != v);
673     }
674 
675     /**
676      * Returns {@code true} if the specified number is infinitely
677      * large in magnitude, {@code false} otherwise.
678      *
679      * @param   v   the value to be tested.
680      * @return  {@code true} if the value of the argument is positive
681      *          infinity or negative infinity; {@code false} otherwise.
682      */
isInfinite(double v)683     public static boolean isInfinite(double v) {
684         return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
685     }
686 
687     /**
688      * Returns {@code true} if the argument is a finite floating-point
689      * value; returns {@code false} otherwise (for NaN and infinity
690      * arguments).
691      *
692      * @param d the {@code double} value to be tested
693      * @return {@code true} if the argument is a finite
694      * floating-point value, {@code false} otherwise.
695      * @since 1.8
696      */
isFinite(double d)697     public static boolean isFinite(double d) {
698         return Math.abs(d) <= Double.MAX_VALUE;
699     }
700 
701     /**
702      * The value of the Double.
703      *
704      * @serial
705      */
706     private final double value;
707 
708     /**
709      * Constructs a newly allocated {@code Double} object that
710      * represents the primitive {@code double} argument.
711      *
712      * @param   value   the value to be represented by the {@code Double}.
713      *
714      * @deprecated
715      * It is rarely appropriate to use this constructor. The static factory
716      * {@link #valueOf(double)} is generally a better choice, as it is
717      * likely to yield significantly better space and time performance.
718      */
719     // Android-changed: not yet forRemoval on Android.
720     @Deprecated(since="9"/*, forRemoval = true*/)
Double(double value)721     public Double(double value) {
722         this.value = value;
723     }
724 
725     /**
726      * Constructs a newly allocated {@code Double} object that
727      * represents the floating-point value of type {@code double}
728      * represented by the string. The string is converted to a
729      * {@code double} value as if by the {@code valueOf} method.
730      *
731      * @param  s  a string to be converted to a {@code Double}.
732      * @throws    NumberFormatException if the string does not contain a
733      *            parsable number.
734      *
735      * @deprecated
736      * It is rarely appropriate to use this constructor.
737      * Use {@link #parseDouble(String)} to convert a string to a
738      * {@code double} primitive, or use {@link #valueOf(String)}
739      * to convert a string to a {@code Double} object.
740      */
741     // Android-changed: not yet forRemoval on Android.
742     @Deprecated(since="9"/*, forRemoval = true */)
Double(String s)743     public Double(String s) throws NumberFormatException {
744         value = parseDouble(s);
745     }
746 
747     /**
748      * Returns {@code true} if this {@code Double} value is
749      * a Not-a-Number (NaN), {@code false} otherwise.
750      *
751      * @return  {@code true} if the value represented by this object is
752      *          NaN; {@code false} otherwise.
753      */
isNaN()754     public boolean isNaN() {
755         return isNaN(value);
756     }
757 
758     /**
759      * Returns {@code true} if this {@code Double} value is
760      * infinitely large in magnitude, {@code false} otherwise.
761      *
762      * @return  {@code true} if the value represented by this object is
763      *          positive infinity or negative infinity;
764      *          {@code false} otherwise.
765      */
isInfinite()766     public boolean isInfinite() {
767         return isInfinite(value);
768     }
769 
770     /**
771      * Returns a string representation of this {@code Double} object.
772      * The primitive {@code double} value represented by this
773      * object is converted to a string exactly as if by the method
774      * {@code toString} of one argument.
775      *
776      * @return  a {@code String} representation of this object.
777      * @see java.lang.Double#toString(double)
778      */
toString()779     public String toString() {
780         return toString(value);
781     }
782 
783     /**
784      * Returns the value of this {@code Double} as a {@code byte}
785      * after a narrowing primitive conversion.
786      *
787      * @return  the {@code double} value represented by this object
788      *          converted to type {@code byte}
789      * @jls 5.1.3 Narrowing Primitive Conversion
790      * @since 1.1
791      */
byteValue()792     public byte byteValue() {
793         return (byte)value;
794     }
795 
796     /**
797      * Returns the value of this {@code Double} as a {@code short}
798      * after a narrowing primitive conversion.
799      *
800      * @return  the {@code double} value represented by this object
801      *          converted to type {@code short}
802      * @jls 5.1.3 Narrowing Primitive Conversion
803      * @since 1.1
804      */
shortValue()805     public short shortValue() {
806         return (short)value;
807     }
808 
809     /**
810      * Returns the value of this {@code Double} as an {@code int}
811      * after a narrowing primitive conversion.
812      * @jls 5.1.3 Narrowing Primitive Conversion
813      *
814      * @return  the {@code double} value represented by this object
815      *          converted to type {@code int}
816      */
intValue()817     public int intValue() {
818         return (int)value;
819     }
820 
821     /**
822      * Returns the value of this {@code Double} as a {@code long}
823      * after a narrowing primitive conversion.
824      *
825      * @return  the {@code double} value represented by this object
826      *          converted to type {@code long}
827      * @jls 5.1.3 Narrowing Primitive Conversion
828      */
longValue()829     public long longValue() {
830         return (long)value;
831     }
832 
833     /**
834      * Returns the value of this {@code Double} as a {@code float}
835      * after a narrowing primitive conversion.
836      *
837      * @return  the {@code double} value represented by this object
838      *          converted to type {@code float}
839      * @jls 5.1.3 Narrowing Primitive Conversion
840      * @since 1.0
841      */
floatValue()842     public float floatValue() {
843         return (float)value;
844     }
845 
846     /**
847      * Returns the {@code double} value of this {@code Double} object.
848      *
849      * @return the {@code double} value represented by this object
850      */
851     @IntrinsicCandidate
doubleValue()852     public double doubleValue() {
853         return value;
854     }
855 
856     /**
857      * Returns a hash code for this {@code Double} object. The
858      * result is the exclusive OR of the two halves of the
859      * {@code long} integer bit representation, exactly as
860      * produced by the method {@link #doubleToLongBits(double)}, of
861      * the primitive {@code double} value represented by this
862      * {@code Double} object. That is, the hash code is the value
863      * of the expression:
864      *
865      * <blockquote>
866      *  {@code (int)(v^(v>>>32))}
867      * </blockquote>
868      *
869      * where {@code v} is defined by:
870      *
871      * <blockquote>
872      *  {@code long v = Double.doubleToLongBits(this.doubleValue());}
873      * </blockquote>
874      *
875      * @return  a {@code hash code} value for this object.
876      */
877     @Override
hashCode()878     public int hashCode() {
879         return Double.hashCode(value);
880     }
881 
882     /**
883      * Returns a hash code for a {@code double} value; compatible with
884      * {@code Double.hashCode()}.
885      *
886      * @param value the value to hash
887      * @return a hash code value for a {@code double} value.
888      * @since 1.8
889      */
hashCode(double value)890     public static int hashCode(double value) {
891         long bits = doubleToLongBits(value);
892         return (int)(bits ^ (bits >>> 32));
893     }
894 
895     /**
896      * Compares this object against the specified object.  The result
897      * is {@code true} if and only if the argument is not
898      * {@code null} and is a {@code Double} object that
899      * represents a {@code double} that has the same value as the
900      * {@code double} represented by this object. For this
901      * purpose, two {@code double} values are considered to be
902      * the same if and only if the method {@link
903      * #doubleToLongBits(double)} returns the identical
904      * {@code long} value when applied to each.
905      *
906      * @apiNote
907      * This method is defined in terms of {@link
908      * #doubleToLongBits(double)} rather than the {@code ==} operator
909      * on {@code double} values since the {@code ==} operator does
910      * <em>not</em> define an equivalence relation and to satisfy the
911      * {@linkplain Object#equals equals contract} an equivalence
912      * relation must be implemented; see <a
913      * href="#equivalenceRelation">this discussion</a> for details of
914      * floating-point equality and equivalence.
915      *
916      * @see java.lang.Double#doubleToLongBits(double)
917      * @jls 15.21.1 Numerical Equality Operators == and !=
918      */
equals(Object obj)919     public boolean equals(Object obj) {
920         return (obj instanceof Double)
921                && (doubleToLongBits(((Double)obj).value) ==
922                       doubleToLongBits(value));
923     }
924 
925     /**
926      * Returns a representation of the specified floating-point value
927      * according to the IEEE 754 floating-point "double
928      * format" bit layout.
929      *
930      * <p>Bit 63 (the bit that is selected by the mask
931      * {@code 0x8000000000000000L}) represents the sign of the
932      * floating-point number. Bits
933      * 62-52 (the bits that are selected by the mask
934      * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
935      * (the bits that are selected by the mask
936      * {@code 0x000fffffffffffffL}) represent the significand
937      * (sometimes called the mantissa) of the floating-point number.
938      *
939      * <p>If the argument is positive infinity, the result is
940      * {@code 0x7ff0000000000000L}.
941      *
942      * <p>If the argument is negative infinity, the result is
943      * {@code 0xfff0000000000000L}.
944      *
945      * <p>If the argument is NaN, the result is
946      * {@code 0x7ff8000000000000L}.
947      *
948      * <p>In all cases, the result is a {@code long} integer that, when
949      * given to the {@link #longBitsToDouble(long)} method, will produce a
950      * floating-point value the same as the argument to
951      * {@code doubleToLongBits} (except all NaN values are
952      * collapsed to a single "canonical" NaN value).
953      *
954      * @param   value   a {@code double} precision floating-point number.
955      * @return the bits that represent the floating-point number.
956      */
957     @IntrinsicCandidate
doubleToLongBits(double value)958     public static long doubleToLongBits(double value) {
959         if (!isNaN(value)) {
960             return doubleToRawLongBits(value);
961         }
962         return 0x7ff8000000000000L;
963     }
964 
965     /**
966      * Returns a representation of the specified floating-point value
967      * according to the IEEE 754 floating-point "double
968      * format" bit layout, preserving Not-a-Number (NaN) values.
969      *
970      * <p>Bit 63 (the bit that is selected by the mask
971      * {@code 0x8000000000000000L}) represents the sign of the
972      * floating-point number. Bits
973      * 62-52 (the bits that are selected by the mask
974      * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
975      * (the bits that are selected by the mask
976      * {@code 0x000fffffffffffffL}) represent the significand
977      * (sometimes called the mantissa) of the floating-point number.
978      *
979      * <p>If the argument is positive infinity, the result is
980      * {@code 0x7ff0000000000000L}.
981      *
982      * <p>If the argument is negative infinity, the result is
983      * {@code 0xfff0000000000000L}.
984      *
985      * <p>If the argument is NaN, the result is the {@code long}
986      * integer representing the actual NaN value.  Unlike the
987      * {@code doubleToLongBits} method,
988      * {@code doubleToRawLongBits} does not collapse all the bit
989      * patterns encoding a NaN to a single "canonical" NaN
990      * value.
991      *
992      * <p>In all cases, the result is a {@code long} integer that,
993      * when given to the {@link #longBitsToDouble(long)} method, will
994      * produce a floating-point value the same as the argument to
995      * {@code doubleToRawLongBits}.
996      *
997      * @param   value   a {@code double} precision floating-point number.
998      * @return the bits that represent the floating-point number.
999      * @since 1.3
1000      */
1001     @IntrinsicCandidate
doubleToRawLongBits(double value)1002     public static native long doubleToRawLongBits(double value);
1003 
1004     /**
1005      * Returns the {@code double} value corresponding to a given
1006      * bit representation.
1007      * The argument is considered to be a representation of a
1008      * floating-point value according to the IEEE 754 floating-point
1009      * "double format" bit layout.
1010      *
1011      * <p>If the argument is {@code 0x7ff0000000000000L}, the result
1012      * is positive infinity.
1013      *
1014      * <p>If the argument is {@code 0xfff0000000000000L}, the result
1015      * is negative infinity.
1016      *
1017      * <p>If the argument is any value in the range
1018      * {@code 0x7ff0000000000001L} through
1019      * {@code 0x7fffffffffffffffL} or in the range
1020      * {@code 0xfff0000000000001L} through
1021      * {@code 0xffffffffffffffffL}, the result is a NaN.  No IEEE
1022      * 754 floating-point operation provided by Java can distinguish
1023      * between two NaN values of the same type with different bit
1024      * patterns.  Distinct values of NaN are only distinguishable by
1025      * use of the {@code Double.doubleToRawLongBits} method.
1026      *
1027      * <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
1028      * values that can be computed from the argument:
1029      *
1030      * <blockquote><pre>{@code
1031      * int s = ((bits >> 63) == 0) ? 1 : -1;
1032      * int e = (int)((bits >> 52) & 0x7ffL);
1033      * long m = (e == 0) ?
1034      *                 (bits & 0xfffffffffffffL) << 1 :
1035      *                 (bits & 0xfffffffffffffL) | 0x10000000000000L;
1036      * }</pre></blockquote>
1037      *
1038      * Then the floating-point result equals the value of the mathematical
1039      * expression <i>s</i>&middot;<i>m</i>&middot;2<sup><i>e</i>-1075</sup>.
1040      *
1041      * <p>Note that this method may not be able to return a
1042      * {@code double} NaN with exactly same bit pattern as the
1043      * {@code long} argument.  IEEE 754 distinguishes between two
1044      * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>.  The
1045      * differences between the two kinds of NaN are generally not
1046      * visible in Java.  Arithmetic operations on signaling NaNs turn
1047      * them into quiet NaNs with a different, but often similar, bit
1048      * pattern.  However, on some processors merely copying a
1049      * signaling NaN also performs that conversion.  In particular,
1050      * copying a signaling NaN to return it to the calling method
1051      * may perform this conversion.  So {@code longBitsToDouble}
1052      * may not be able to return a {@code double} with a
1053      * signaling NaN bit pattern.  Consequently, for some
1054      * {@code long} values,
1055      * {@code doubleToRawLongBits(longBitsToDouble(start))} may
1056      * <i>not</i> equal {@code start}.  Moreover, which
1057      * particular bit patterns represent signaling NaNs is platform
1058      * dependent; although all NaN bit patterns, quiet or signaling,
1059      * must be in the NaN range identified above.
1060      *
1061      * @param   bits   any {@code long} integer.
1062      * @return  the {@code double} floating-point value with the same
1063      *          bit pattern.
1064      */
1065     @IntrinsicCandidate
longBitsToDouble(long bits)1066     public static native double longBitsToDouble(long bits);
1067 
1068     /**
1069      * Compares two {@code Double} objects numerically.
1070      *
1071      * This method imposes a total order on {@code Double} objects
1072      * with two differences compared to the incomplete order defined by
1073      * the Java language numerical comparison operators ({@code <, <=,
1074      * ==, >=, >}) on {@code double} values.
1075      *
1076      * <ul><li> A NaN is <em>unordered</em> with respect to other
1077      *          values and unequal to itself under the comparison
1078      *          operators.  This method chooses to define {@code
1079      *          Double.NaN} to be equal to itself and greater than all
1080      *          other {@code double} values (including {@code
1081      *          Double.POSITIVE_INFINITY}).
1082      *
1083      *      <li> Positive zero and negative zero compare equal
1084      *      numerically, but are distinct and distinguishable values.
1085      *      This method chooses to define positive zero ({@code +0.0d}),
1086      *      to be greater than negative zero ({@code -0.0d}).
1087      * </ul>
1088 
1089      * This ensures that the <i>natural ordering</i> of {@code Double}
1090      * objects imposed by this method is <i>consistent with
1091      * equals</i>; see <a href="#equivalenceRelation">this
1092      * discussion</a> for details of floating-point comparison and
1093      * ordering.
1094      *
1095      * @param   anotherDouble   the {@code Double} to be compared.
1096      * @return  the value {@code 0} if {@code anotherDouble} is
1097      *          numerically equal to this {@code Double}; a value
1098      *          less than {@code 0} if this {@code Double}
1099      *          is numerically less than {@code anotherDouble};
1100      *          and a value greater than {@code 0} if this
1101      *          {@code Double} is numerically greater than
1102      *          {@code anotherDouble}.
1103      *
1104      * @jls 15.20.1 Numerical Comparison Operators {@code <}, {@code <=}, {@code >}, and {@code >=}
1105      * @since   1.2
1106      */
compareTo(Double anotherDouble)1107     public int compareTo(Double anotherDouble) {
1108         return Double.compare(value, anotherDouble.value);
1109     }
1110 
1111     /**
1112      * Compares the two specified {@code double} values. The sign
1113      * of the integer value returned is the same as that of the
1114      * integer that would be returned by the call:
1115      * <pre>
1116      *    new Double(d1).compareTo(new Double(d2))
1117      * </pre>
1118      *
1119      * @param   d1        the first {@code double} to compare
1120      * @param   d2        the second {@code double} to compare
1121      * @return  the value {@code 0} if {@code d1} is
1122      *          numerically equal to {@code d2}; a value less than
1123      *          {@code 0} if {@code d1} is numerically less than
1124      *          {@code d2}; and a value greater than {@code 0}
1125      *          if {@code d1} is numerically greater than
1126      *          {@code d2}.
1127      * @since 1.4
1128      */
compare(double d1, double d2)1129     public static int compare(double d1, double d2) {
1130         if (d1 < d2)
1131             return -1;           // Neither val is NaN, thisVal is smaller
1132         if (d1 > d2)
1133             return 1;            // Neither val is NaN, thisVal is larger
1134 
1135         // Cannot use doubleToRawLongBits because of possibility of NaNs.
1136         long thisBits    = Double.doubleToLongBits(d1);
1137         long anotherBits = Double.doubleToLongBits(d2);
1138 
1139         return (thisBits == anotherBits ?  0 : // Values are equal
1140                 (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1141                  1));                          // (0.0, -0.0) or (NaN, !NaN)
1142     }
1143 
1144     /**
1145      * Adds two {@code double} values together as per the + operator.
1146      *
1147      * @param a the first operand
1148      * @param b the second operand
1149      * @return the sum of {@code a} and {@code b}
1150      * @jls 4.2.4 Floating-Point Operations
1151      * @see java.util.function.BinaryOperator
1152      * @since 1.8
1153      */
sum(double a, double b)1154     public static double sum(double a, double b) {
1155         return a + b;
1156     }
1157 
1158     /**
1159      * Returns the greater of two {@code double} values
1160      * as if by calling {@link Math#max(double, double) Math.max}.
1161      *
1162      * @param a the first operand
1163      * @param b the second operand
1164      * @return the greater of {@code a} and {@code b}
1165      * @see java.util.function.BinaryOperator
1166      * @since 1.8
1167      */
max(double a, double b)1168     public static double max(double a, double b) {
1169         return Math.max(a, b);
1170     }
1171 
1172     /**
1173      * Returns the smaller of two {@code double} values
1174      * as if by calling {@link Math#min(double, double) Math.min}.
1175      *
1176      * @param a the first operand
1177      * @param b the second operand
1178      * @return the smaller of {@code a} and {@code b}.
1179      * @see java.util.function.BinaryOperator
1180      * @since 1.8
1181      */
min(double a, double b)1182     public static double min(double a, double b) {
1183         return Math.min(a, b);
1184     }
1185 
1186     // BEGIN Android-removed: dynamic constants not supported on Android.
1187     /**
1188      * Returns an {@link Optional} containing the nominal descriptor for this
1189      * instance, which is the instance itself.
1190      *
1191      * @return an {@link Optional} describing the {@linkplain Double} instance
1192      * @since 12
1193      *
1194     @Override
1195     public Optional<Double> describeConstable() {
1196         return Optional.of(this);
1197     }
1198 
1199     /**
1200      * Resolves this instance as a {@link ConstantDesc}, the result of which is
1201      * the instance itself.
1202      *
1203      * @param lookup ignored
1204      * @return the {@linkplain Double} instance
1205      * @since 12
1206      *
1207     @Override
1208     public Double resolveConstantDesc(MethodHandles.Lookup lookup) {
1209         return this;
1210     }
1211     // END Android-removed: dynamic constants not supported on Android.
1212 
1213     /** use serialVersionUID from JDK 1.0.2 for interoperability */
1214     @java.io.Serial
1215     private static final long serialVersionUID = -9172774392245257468L;
1216 }
1217