001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.lang;
019:
020: /**
021: * <p>
022: * Double is the wrapper for the primitive type <code>double</code>.
023: * </p>
024: *
025: * @see java.lang.Number
026: * @since 1.0
027: */
028: public final class Double extends Number implements Comparable<Double> {
029:
030: private static final long serialVersionUID = -9172774392245257468L;
031:
032: /**
033: * The value which the receiver represents.
034: */
035: private final double value;
036:
037: /**
038: * <p>
039: * Constant for the maximum <code>double</code> value, (2 - 2<sup>-52/sup>) *
040: * 2<sup>1023</sup>.
041: * </p>
042: */
043: public static final double MAX_VALUE = 1.79769313486231570e+308;
044:
045: /**
046: * <p>
047: * Constant for the minimum <code>double</code> value, 2<sup>-1074</sup>.
048: * </p>
049: */
050: public static final double MIN_VALUE = 5e-324;
051:
052: /* 4.94065645841246544e-324 gets rounded to 9.88131e-324 */
053:
054: /**
055: * <p>
056: * Constant for the Not-a-Number (NaN) value of the <code>double</code>
057: * type.
058: * </p>
059: */
060: public static final double NaN = 0.0 / 0.0;
061:
062: /**
063: * <p>
064: * Constant for the Positive Infinity value of the <code>double</code>
065: * type.
066: * </p>
067: */
068: public static final double POSITIVE_INFINITY = 1.0 / 0.0;
069:
070: /**
071: * <p>
072: * Constant for the Negative Infinity value of the <code>double</code>
073: * type.
074: * </p>
075: */
076: public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
077:
078: /**
079: * The java.lang.Class that represents this class.
080: *
081: * @since 1.1
082: */
083: @SuppressWarnings("unchecked")
084: public static final Class<Double> TYPE = (Class<Double>) new double[0]
085: .getClass().getComponentType();
086:
087: // Note: This can't be set to "double.class", since *that* is
088: // defined to be "java.lang.Double.TYPE";
089:
090: /**
091: * <p>
092: * Constant for the number of bits to represent a <code>double</code> in
093: * two's compliment form.
094: * </p>
095: *
096: * @since 1.5
097: */
098: public static final int SIZE = 64;
099:
100: /**
101: * Constructs a new instance of the receiver which represents the double
102: * valued argument.
103: *
104: * @param value
105: * the double to store in the new instance.
106: */
107: public Double(double value) {
108: this .value = value;
109: }
110:
111: /**
112: * Constructs a new instance of this class given a string.
113: *
114: * @param string
115: * a string representation of a double quantity.
116: * @exception NumberFormatException
117: * if the argument could not be parsed as a double quantity.
118: */
119: public Double(String string) throws NumberFormatException {
120: this (parseDouble(string));
121: }
122:
123: /**
124: * Compares the receiver with the Double parameter. NaN is equal to NaN, and
125: * is greater than other double values. 0d is greater than -0d.
126: *
127: * @param object
128: * the Double to compare to the receiver
129: *
130: * @return Returns greater than zero when this.doubleValue() is greater than
131: * object.doubleValue(), zero when this.doubleValue() equals
132: * object.doubleValue(), and less than zero when this.doubleValue()
133: * is less than object.doubleValue()
134: *
135: * @throws NullPointerException
136: * if <code>object</code> is <code>null</code>.
137: * @since 1.2
138: */
139: public int compareTo(Double object) {
140: long d1, d2;
141: long NaNbits = Double.doubleToLongBits(Double.NaN);
142: if ((d1 = Double.doubleToLongBits(value)) == NaNbits) {
143: if (Double.doubleToLongBits(object.value) == NaNbits) {
144: return 0;
145: }
146: return 1;
147: }
148: if ((d2 = Double.doubleToLongBits(object.value)) == NaNbits) {
149: return -1;
150: }
151: if (value == object.value) {
152: if (d1 == d2) {
153: return 0;
154: }
155: // check for -0
156: return d1 > d2 ? 1 : -1;
157: }
158: return value > object.value ? 1 : -1;
159: }
160:
161: /**
162: * Answers the byte value which the receiver represents
163: *
164: * @return byte the value of the receiver.
165: */
166: @Override
167: public byte byteValue() {
168: return (byte) value;
169: }
170:
171: /**
172: * Answers the binary representation of the argument, as a long.
173: *
174: * @param value
175: * The double value to convert
176: * @return the bits of the double.
177: */
178: public static native long doubleToLongBits(double value);
179:
180: /**
181: * Answers the binary representation of the argument, as a long.
182: *
183: * @param value
184: * The double value to convert
185: * @return the bits of the double.
186: */
187: public static native long doubleToRawLongBits(double value);
188:
189: /**
190: * Answers the receiver's value as a double.
191: *
192: * @return the receiver's value
193: */
194: @Override
195: public double doubleValue() {
196: return value;
197: }
198:
199: /**
200: * Compares the argument to the receiver, and answers true if they represent
201: * the <em>same</em> object using a class specific comparison. For
202: * Doubles, the check verifies that the receiver's value's bit pattern
203: * matches the bit pattern of the argument, which must also be a Double.
204: *
205: * @param object
206: * the object to compare with this object
207: * @return <code>true</code> if the object is the same as this object
208: * <code>false</code> if it is different from this object
209: * @see #hashCode
210: */
211: @Override
212: public boolean equals(Object object) {
213: return (object == this )
214: || (object instanceof Double)
215: && (doubleToLongBits(this .value) == doubleToLongBits(((Double) object).value));
216: }
217:
218: /**
219: * Answers the float value which the receiver represents
220: *
221: * @return float the value of the receiver.
222: */
223: @Override
224: public float floatValue() {
225: return (float) value;
226: }
227:
228: /**
229: * Answers an integer hash code for the receiver. Any two objects which
230: * answer <code>true</code> when passed to <code>equals</code> must
231: * answer the same value for this method.
232: *
233: * @return the receiver's hash
234: *
235: * @see #equals
236: */
237: @Override
238: public int hashCode() {
239: long v = doubleToLongBits(value);
240: return (int) (v ^ (v >>> 32));
241: }
242:
243: /**
244: * Answers the receiver's value as an integer.
245: *
246: * @return the receiver's value as an integer
247: */
248: @Override
249: public int intValue() {
250: return (int) value;
251: }
252:
253: /**
254: * Answers true if the receiver represents an infinite quantity, and false
255: * otherwise.
256: *
257: * @return <code>true</code> if the argument is positive or negative
258: * infinity <code>false</code> if it is not an infinite value
259: */
260: public boolean isInfinite() {
261: return isInfinite(value);
262: }
263:
264: /**
265: * Answers true if the argument represents an infinite quantity, and false
266: * otherwise.
267: *
268: * @param d
269: * value to check for infinitness.
270: * @return <code>true</code> if the argument is positive or negative
271: * infinity <code>false</code> if it is not an infinite value
272: */
273: public static boolean isInfinite(double d) {
274: return (d == POSITIVE_INFINITY) || (d == NEGATIVE_INFINITY);
275: }
276:
277: /**
278: * Answers true if the receiver does not represent a valid float quantity.
279: *
280: * @return <code>true</code> if the argument is Not A Number
281: * <code>false</code> if it is a (potentially infinite) float
282: * number
283: */
284: public boolean isNaN() {
285: return isNaN(value);
286: }
287:
288: /**
289: * Answers true if the argument does not represent a valid double quantity.
290: *
291: * @param d
292: * value to check for numberness.
293: * @return <code>true</code> if the argument is Not A Number
294: * <code>false</code> if it is a (potentially infinite) double
295: * number
296: */
297: public static boolean isNaN(double d) {
298: return d != d;
299: }
300:
301: /**
302: * Answers a double built from the binary representation given in the
303: * argument.
304: *
305: * @param bits
306: * the bits of the double
307: * @return the double which matches the bits
308: */
309: public static native double longBitsToDouble(long bits);
310:
311: /**
312: * Answers the long value which the receiver represents
313: *
314: * @return long the value of the receiver.
315: */
316: @Override
317: public long longValue() {
318: return (long) value;
319: }
320:
321: /**
322: * Answers the double which matches the passed in string.
323: * NumberFormatException is thrown if the string does not represent a valid
324: * double.
325: *
326: * @param string
327: * the value to convert
328: * @return a double which would print as the argument
329: */
330: public static double parseDouble(String string)
331: throws NumberFormatException {
332: return org.apache.harmony.luni.util.FloatingPointParser
333: .parseDouble(string);
334: }
335:
336: /**
337: * Answers the short value which the receiver represents
338: *
339: * @return short the value of the receiver.
340: */
341: @Override
342: public short shortValue() {
343: return (short) value;
344: }
345:
346: /**
347: * Answers a string containing a concise, human-readable description of the
348: * receiver.
349: *
350: * @return a printable representation for the receiver.
351: */
352: @Override
353: public String toString() {
354: return Double.toString(value);
355: }
356:
357: /**
358: * Answers a string containing a printable representation of the argument.
359: *
360: * @param d
361: * the double to print
362: * @return a printable representation of the argument.
363: */
364: public static String toString(double d) {
365: return org.apache.harmony.luni.util.NumberConverter.convert(d);
366: }
367:
368: /**
369: * Answers the double which matches the passed in string.
370: * NumberFormatException is thrown if the string does not represent a valid
371: * double.
372: *
373: * @param string
374: * the value to convert
375: * @return a double which would print as the argument
376: */
377: public static Double valueOf(String string)
378: throws NumberFormatException {
379: return new Double(parseDouble(string));
380: }
381:
382: /**
383: * Compares the two doubles. NaN is equal to NaN, and is greater than other
384: * double values. 0d is greater than -0d.
385: *
386: * @param double1
387: * the first value to compare
388: * @param double2
389: * the second value to compare
390: *
391: * @return Returns greater than zero when double1 is greater than double2,
392: * zero when double1 equals double2, and less than zero when double1
393: * is less than double2
394: */
395: public static int compare(double double1, double double2) {
396: long d1, d2;
397: long NaNbits = Double.doubleToLongBits(Double.NaN);
398: if ((d1 = Double.doubleToLongBits(double1)) == NaNbits) {
399: if (Double.doubleToLongBits(double2) == NaNbits) {
400: return 0;
401: }
402: return 1;
403: }
404: if ((d2 = Double.doubleToLongBits(double2)) == NaNbits) {
405: return -1;
406: }
407: if (double1 == double2) {
408: if (d1 == d2) {
409: return 0;
410: }
411: // check for -0
412: return d1 > d2 ? 1 : -1;
413: }
414: return double1 > double2 ? 1 : -1;
415: }
416:
417: /**
418: * <p>
419: * Returns a <code>Double</code> instance for the <code>double</code>
420: * value passed. This method is preferred over the constructor, as this
421: * method may maintain a cache of instances.
422: * </p>
423: *
424: * @param d
425: * The double value.
426: * @return A <code>Double</code> instance.
427: * @since 1.5
428: */
429: public static Double valueOf(double d) {
430: return new Double(d);
431: }
432:
433: /**
434: * <p>
435: * Converts a <code>double</code> into a hexadecimal string
436: * representation.
437: * </p>
438: *
439: * @param d
440: * The <code>double</code> to convert.
441: * @return The hexadecimal string representation of <code>f</code>.
442: * @since 1.5
443: */
444: public static String toHexString(double d) {
445: /*
446: * Reference: http://en.wikipedia.org/wiki/IEEE_754
447: */
448: if (d != d) {
449: return "NaN"; //$NON-NLS-1$
450: }
451: if (d == POSITIVE_INFINITY) {
452: return "Infinity"; //$NON-NLS-1$
453: }
454: if (d == NEGATIVE_INFINITY) {
455: return "-Infinity"; //$NON-NLS-1$
456: }
457:
458: long bitValue = doubleToLongBits(d);
459:
460: boolean negative = (bitValue & 0x8000000000000000L) != 0;
461: // mask exponent bits and shift down
462: long exponent = (bitValue & 0x7FF0000000000000L) >>> 52;
463: // mask significand bits and shift up
464: long significand = bitValue & 0x000FFFFFFFFFFFFFL;
465:
466: if (exponent == 0 && significand == 0) {
467: return (negative ? "-0x0.0p0" : "0x0.0p0"); //$NON-NLS-1$ //$NON-NLS-2$
468: }
469:
470: StringBuilder hexString = new StringBuilder(10);
471: if (negative) {
472: hexString.append("-0x"); //$NON-NLS-1$
473: } else {
474: hexString.append("0x"); //$NON-NLS-1$
475: }
476:
477: if (exponent == 0) { // denormal (subnormal) value
478: hexString.append("0."); //$NON-NLS-1$
479: // significand is 52-bits, so there can be 13 hex digits
480: int fractionDigits = 13;
481: // remove trailing hex zeros, so Integer.toHexString() won't print
482: // them
483: while ((significand != 0) && ((significand & 0xF) == 0)) {
484: significand >>>= 4;
485: fractionDigits--;
486: }
487: // this assumes Integer.toHexString() returns lowercase characters
488: String hexSignificand = Long.toHexString(significand);
489:
490: // if there are digits left, then insert some '0' chars first
491: if (significand != 0
492: && fractionDigits > hexSignificand.length()) {
493: int digitDiff = fractionDigits
494: - hexSignificand.length();
495: while (digitDiff-- != 0) {
496: hexString.append('0');
497: }
498: }
499: hexString.append(hexSignificand);
500: hexString.append("p-1022"); //$NON-NLS-1$
501: } else { // normal value
502: hexString.append("1."); //$NON-NLS-1$
503: // significand is 52-bits, so there can be 13 hex digits
504: int fractionDigits = 13;
505: // remove trailing hex zeros, so Integer.toHexString() won't print
506: // them
507: while ((significand != 0) && ((significand & 0xF) == 0)) {
508: significand >>>= 4;
509: fractionDigits--;
510: }
511: // this assumes Integer.toHexString() returns lowercase characters
512: String hexSignificand = Long.toHexString(significand);
513:
514: // if there are digits left, then insert some '0' chars first
515: if (significand != 0
516: && fractionDigits > hexSignificand.length()) {
517: int digitDiff = fractionDigits
518: - hexSignificand.length();
519: while (digitDiff-- != 0) {
520: hexString.append('0');
521: }
522: }
523:
524: hexString.append(hexSignificand);
525: hexString.append('p');
526: // remove exponent's 'bias' and convert to a string
527: hexString.append(Long.toString(exponent - 1023));
528: }
529: return hexString.toString();
530: }
531: }
|