Source Code Cross Referenced for Double.java in  » 6.0-JDK-Core » lang » java » lang » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » lang » java.lang 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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