1. /*
  2. * @(#)Double.java 1.82 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. /**
  9. * The <code>Double</code> class wraps a value of the primitive type
  10. * <code>double</code> in an object. An object of type
  11. * <code>Double</code> contains a single field whose type is
  12. * <code>double</code>.
  13. * <p>
  14. * In addition, this class provides several methods for converting a
  15. * <code>double</code> to a <code>String</code> and a
  16. * <code>String</code> to a <code>double</code>, as well as other
  17. * constants and methods useful when dealing with a
  18. * <code>double</code>.
  19. *
  20. * @author Lee Boynton
  21. * @author Arthur van Hoff
  22. * @version 1.82, 01/23/03
  23. * @since JDK1.0
  24. */
  25. public final class Double extends Number implements Comparable {
  26. /**
  27. * A constant holding the positive infinity of type
  28. * <code>double</code>. It is equal to the value returned by
  29. * <code>Double.longBitsToDouble(0x7ff0000000000000L)</code>.
  30. */
  31. public static final double POSITIVE_INFINITY = 1.0 / 0.0;
  32. /**
  33. * A constant holding the negative infinity of type
  34. * <code>double</code>. It is equal to the value returned by
  35. * <code>Double.longBitsToDouble(0xfff0000000000000L)</code>.
  36. */
  37. public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
  38. /**
  39. * A constant holding a Not-a-Number (NaN) value of type
  40. * <code>double</code>. It is equivalent to the value returned by
  41. * <code>Double.longBitsToDouble(0x7ff8000000000000L)</code>.
  42. */
  43. public static final double NaN = 0.0d / 0.0;
  44. /**
  45. * A constant holding the largest positive finite value of type
  46. * <code>double</code>, (2-2<sup>-52</sup>)·2<sup>1023</sup>.
  47. * It is equal to the value returned by:
  48. * <code>Double.longBitsToDouble(0x7fefffffffffffffL)</code>.
  49. */
  50. public static final double MAX_VALUE = 1.7976931348623157e+308;
  51. /**
  52. * A constant holding the smallest positive nonzero value of type
  53. * <code>double</code>, 2<sup>-1074</sup>. It is equal to the
  54. * value returned by <code>Double.longBitsToDouble(0x1L)</code>.
  55. */
  56. public static final double MIN_VALUE = 4.9e-324;
  57. /**
  58. * The <code>Class</code> instance representing the primitive type
  59. * <code>double</code>.
  60. *
  61. * @since JDK1.1
  62. */
  63. public static final Class TYPE = Class.getPrimitiveClass("double");
  64. /**
  65. * Returns a string representation of the <code>double</code>
  66. * argument. All characters mentioned below are ASCII characters.
  67. * <ul>
  68. * <li>If the argument is NaN, the result is the string
  69. * "<code>NaN</code>".
  70. * <li>Otherwise, the result is a string that represents the sign and
  71. * magnitude (absolute value) of the argument. If the sign is negative,
  72. * the first character of the result is '<code>-</code>'
  73. * (<code>'\u002D'</code>); if the sign is positive, no sign character
  74. * appears in the result. As for the magnitude <i>m</i>:
  75. * <ul>
  76. * <li>If <i>m</i> is infinity, it is represented by the characters
  77. * <code>"Infinity"</code> thus, positive infinity produces the result
  78. * <code>"Infinity"</code> and negative infinity produces the result
  79. * <code>"-Infinity"</code>.
  80. *
  81. * <li>If <i>m</i> is zero, it is represented by the characters
  82. * <code>"0.0"</code> thus, negative zero produces the result
  83. * <code>"-0.0"</code> and positive zero produces the result
  84. * <code>"0.0"</code>.
  85. *
  86. * <li>If <i>m</i> is greater than or equal to 10<sup>-3</sup> but less
  87. * than 10<sup>7</sup>, then it is represented as the integer part of
  88. * <i>m</i>, in decimal form with no leading zeroes, followed by
  89. * '<code>.</code>' (<code>'\u002E'</code>), followed by one or
  90. * more decimal digits representing the fractional part of <i>m</i>.
  91. *
  92. * <li>If <i>m</i> is less than 10<sup>-3</sup> or greater than or
  93. * equal to 10<sup>7</sup>, then it is represented in so-called
  94. * "computerized scientific notation." Let <i>n</i> be the unique
  95. * integer such that 10<sup><i>n</i></sup> <= <i>m</i> <
  96. * 10<sup><i>n</i>+1</sup> then let <i>a</i> be the
  97. * mathematically exact quotient of <i>m</i> and
  98. * 10<sup><i>n</i></sup> so that 1 <= <i>a</i> < 10. The
  99. * magnitude is then represented as the integer part of <i>a</i>,
  100. * as a single decimal digit, followed by '<code>.</code>'
  101. * (<code>'\u002E'</code>), followed by decimal digits
  102. * representing the fractional part of <i>a</i>, followed by the
  103. * letter '<code>E</code>' (<code>'\u0045'</code>), followed
  104. * by a representation of <i>n</i> as a decimal integer, as
  105. * produced by the method {@link Integer#toString(int)}.
  106. * </ul>
  107. * </ul>
  108. * How many digits must be printed for the fractional part of
  109. * <i>m</i> or <i>a</i>? There must be at least one digit to represent
  110. * the fractional part, and beyond that as many, but only as many, more
  111. * digits as are needed to uniquely distinguish the argument value from
  112. * adjacent values of type <code>double</code>. That is, suppose that
  113. * <i>x</i> is the exact mathematical value represented by the decimal
  114. * representation produced by this method for a finite nonzero argument
  115. * <i>d</i>. Then <i>d</i> must be the <code>double</code> value nearest
  116. * to <i>x</i> or if two <code>double</code> values are equally close
  117. * to <i>x</i>, then <i>d</i> must be one of them and the least
  118. * significant bit of the significand of <i>d</i> must be <code>0</code>.
  119. * <p>
  120. * To create localized string representations of a floating-point
  121. * value, use subclasses of {@link java.text.NumberFormat}.
  122. *
  123. * @param d the <code>double</code> to be converted.
  124. * @return a string representation of the argument.
  125. */
  126. public static String toString(double d) {
  127. return new FloatingDecimal(d).toJavaFormatString();
  128. }
  129. /**
  130. * Returns a <code>Double</code> object holding the
  131. * <code>double</code> value represented by the argument string
  132. * <code>s</code>.
  133. * <p>
  134. * If <code>s</code> is <code>null</code>, then a
  135. * <code>NullPointerException</code> is thrown.
  136. * <p>
  137. * Leading and trailing whitespace characters in <code>s</code>
  138. * are ignored. The rest of <code>s</code> should constitute a
  139. * <i>FloatValue</i> as described by the lexical rule:
  140. * <blockquote><i>
  141. * <dl>
  142. * <dt>FloatValue:
  143. * <dd><i>Sign<sub>opt</sub></i> <code>NaN</code>
  144. * <dd><i>Sign<sub>opt</sub></i> <code>Infinity</code>
  145. * <dd>Sign<sub>opt</sub> FloatingPointLiteral
  146. * </dl>
  147. * </i></blockquote>
  148. * where <i>Sign</i> and <i>FloatingPointLiteral</i> are as
  149. * defined in
  150. * <a href="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#230798">§3.10.2</a>
  151. * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
  152. * Language Specification</a>. If <code>s</code> does not have the
  153. * form of a <i>FloatValue</i>, then a <code>NumberFormatException</code>
  154. * is thrown. Otherwise, <code>s</code> is regarded as
  155. * representing an exact decimal value in the usual "computerized
  156. * scientific notation"; this exact decimal value is then
  157. * conceptually converted to an "infinitely precise" binary value
  158. * that is then rounded to type <code>double</code> by the usual
  159. * round-to-nearest rule of IEEE 754 floating-point arithmetic,
  160. * which includes preserving the sign of a zero value. Finally, a
  161. * <code>Double</code> object representing this
  162. * <code>double</code> value is returned.
  163. * <p>
  164. * To interpret localized string representations of a
  165. * floating-point value, use subclasses of {@link
  166. * java.text.NumberFormat}.
  167. *
  168. * <p>Note that trailing format specifiers, specifiers that
  169. * determine the type of a floating-point literal
  170. * (<code>1.0f</code> is a <code>float</code> value;
  171. * <code>1.0d</code> is a <code>double</code> value), do
  172. * <em>not</em> influence the results of this method. In other
  173. * words, the numerical value of the input string is converted
  174. * directly to the target floating-point type. The two-step
  175. * sequence of conversions, string to <code>float</code> followed
  176. * by <code>float</code> to <code>double</code>, is <em>not</em>
  177. * equivalent to converting a string directly to
  178. * <code>double</code>. For example, the <code>float</code>
  179. * literal <code>0.1f</code> is equal to the <code>double</code>
  180. * value <code>0.10000000149011612</code> the <code>float</code>
  181. * literal <code>0.1f</code> represents a different numerical
  182. * value than the <code>double</code> literal
  183. * <code>0.1</code>. (The numerical value 0.1 cannot be exactly
  184. * represented in a binary floating-point number.)
  185. *
  186. * @param s the string to be parsed.
  187. * @return a <code>Double</code> object holding the value
  188. * represented by the <code>String</code> argument.
  189. * @exception NumberFormatException if the string does not contain a
  190. * parsable number.
  191. */
  192. public static Double valueOf(String s) throws NumberFormatException {
  193. return new Double(FloatingDecimal.readJavaFormatString(s).doubleValue());
  194. }
  195. /**
  196. * Returns a new <code>double</code> initialized to the value
  197. * represented by the specified <code>String</code>, as performed
  198. * by the <code>valueOf</code> method of class
  199. * <code>Double</code>.
  200. *
  201. * @param s the string to be parsed.
  202. * @return the <code>double</code> value represented by the string
  203. * argument.
  204. * @exception NumberFormatException if the string does not contain
  205. * a parsable <code>double</code>.
  206. * @see java.lang.Double#valueOf(String)
  207. * @since 1.2
  208. */
  209. public static double parseDouble(String s) throws NumberFormatException {
  210. return FloatingDecimal.readJavaFormatString(s).doubleValue();
  211. }
  212. /**
  213. * Returns <code>true</code> if the specified number is a
  214. * Not-a-Number (NaN) value, <code>false</code> otherwise.
  215. *
  216. * @param v the value to be tested.
  217. * @return <code>true</code> if the value of the argument is NaN;
  218. * <code>false</code> otherwise.
  219. */
  220. static public boolean isNaN(double v) {
  221. return (v != v);
  222. }
  223. /**
  224. * Returns <code>true</code> if the specified number is infinitely
  225. * large in magnitude, <code>false</code> otherwise.
  226. *
  227. * @param v the value to be tested.
  228. * @return <code>true</code> if the value of the argument is positive
  229. * infinity or negative infinity; <code>false</code> otherwise.
  230. */
  231. static public boolean isInfinite(double v) {
  232. return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
  233. }
  234. /**
  235. * The value of the Double.
  236. *
  237. * @serial
  238. */
  239. private double value;
  240. /**
  241. * Constructs a newly allocated <code>Double</code> object that
  242. * represents the primitive <code>double</code> argument.
  243. *
  244. * @param value the value to be represented by the <code>Double</code>.
  245. */
  246. public Double(double value) {
  247. this.value = value;
  248. }
  249. /**
  250. * Constructs a newly allocated <code>Double</code> object that
  251. * represents the floating-point value of type <code>double</code>
  252. * represented by the string. The string is converted to a
  253. * <code>double</code> value as if by the <code>valueOf</code> method.
  254. *
  255. * @param s a string to be converted to a <code>Double</code>.
  256. * @exception NumberFormatException if the string does not contain a
  257. * parsable number.
  258. * @see java.lang.Double#valueOf(java.lang.String)
  259. */
  260. public Double(String s) throws NumberFormatException {
  261. // REMIND: this is inefficient
  262. this(valueOf(s).doubleValue());
  263. }
  264. /**
  265. * Returns <code>true</code> if this <code>Double</code> value is
  266. * a Not-a-Number (NaN), <code>false</code> otherwise.
  267. *
  268. * @return <code>true</code> if the value represented by this object is
  269. * NaN; <code>false</code> otherwise.
  270. */
  271. public boolean isNaN() {
  272. return isNaN(value);
  273. }
  274. /**
  275. * Returns <code>true</code> if this <code>Double</code> value is
  276. * infinitely large in magnitude, <code>false</code> otherwise.
  277. *
  278. * @return <code>true</code> if the value represented by this object is
  279. * positive infinity or negative infinity;
  280. * <code>false</code> otherwise.
  281. */
  282. public boolean isInfinite() {
  283. return isInfinite(value);
  284. }
  285. /**
  286. * Returns a string representation of this <code>Double</code> object.
  287. * The primitive <code>double</code> value represented by this
  288. * object is converted to a string exactly as if by the method
  289. * <code>toString</code> of one argument.
  290. *
  291. * @return a <code>String</code> representation of this object.
  292. * @see java.lang.Double#toString(double)
  293. */
  294. public String toString() {
  295. return String.valueOf(value);
  296. }
  297. /**
  298. * Returns the value of this <code>Double</code> as a <code>byte</code> (by
  299. * casting to a <code>byte</code>).
  300. *
  301. * @return the <code>double</code> value represented by this object
  302. * converted to type <code>byte</code>
  303. * @since JDK1.1
  304. */
  305. public byte byteValue() {
  306. return (byte)value;
  307. }
  308. /**
  309. * Returns the value of this <code>Double</code> as a
  310. * <code>short</code> (by casting to a <code>short</code>).
  311. *
  312. * @return the <code>double</code> value represented by this object
  313. * converted to type <code>short</code>
  314. * @since JDK1.1
  315. */
  316. public short shortValue() {
  317. return (short)value;
  318. }
  319. /**
  320. * Returns the value of this <code>Double</code> as an
  321. * <code>int</code> (by casting to type <code>int</code>).
  322. *
  323. * @return the <code>double</code> value represented by this object
  324. * converted to type <code>int</code>
  325. */
  326. public int intValue() {
  327. return (int)value;
  328. }
  329. /**
  330. * Returns the value of this <code>Double</code> as a
  331. * <code>long</code> (by casting to type <code>long</code>).
  332. *
  333. * @return the <code>double</code> value represented by this object
  334. * converted to type <code>long</code>
  335. */
  336. public long longValue() {
  337. return (long)value;
  338. }
  339. /**
  340. * Returns the <code>float</code> value of this
  341. * <code>Double</code> object.
  342. *
  343. * @return the <code>double</code> value represented by this object
  344. * converted to type <code>float</code>
  345. * @since JDK1.0
  346. */
  347. public float floatValue() {
  348. return (float)value;
  349. }
  350. /**
  351. * Returns the <code>double</code> value of this
  352. * <code>Double</code> object.
  353. *
  354. * @return the <code>double</code> value represented by this object
  355. */
  356. public double doubleValue() {
  357. return (double)value;
  358. }
  359. /**
  360. * Returns a hash code for this <code>Double</code> object. The
  361. * result is the exclusive OR of the two halves of the
  362. * <code>long</code> integer bit representation, exactly as
  363. * produced by the method {@link #doubleToLongBits(double)}, of
  364. * the primitive <code>double</code> value represented by this
  365. * <code>Double</code> object. That is, the hash code is the value
  366. * of the expression:
  367. * <blockquote><pre>
  368. * (int)(v^(v>>>32))
  369. * </pre></blockquote>
  370. * where <code>v</code> is defined by:
  371. * <blockquote><pre>
  372. * long v = Double.doubleToLongBits(this.doubleValue());
  373. * </pre></blockquote>
  374. *
  375. * @return a <code>hash code</code> value for this object.
  376. */
  377. public int hashCode() {
  378. long bits = doubleToLongBits(value);
  379. return (int)(bits ^ (bits >>> 32));
  380. }
  381. /**
  382. * Compares this object against the specified object. The result
  383. * is <code>true</code> if and only if the argument is not
  384. * <code>null</code> and is a <code>Double</code> object that
  385. * represents a <code>double</code> that has the same value as the
  386. * <code>double</code> represented by this object. For this
  387. * purpose, two <code>double</code> values are considered to be
  388. * the same if and only if the method {@link
  389. * #doubleToLongBits(double)} returns the identical
  390. * <code>long</code> value when applied to each.
  391. * <p>
  392. * Note that in most cases, for two instances of class
  393. * <code>Double</code>, <code>d1</code> and <code>d2</code>, the
  394. * value of <code>d1.equals(d2)</code> is <code>true</code> if and
  395. * only if
  396. * <blockquote><pre>
  397. * d1.doubleValue() == d2.doubleValue()
  398. * </pre></blockquote>
  399. * <p>
  400. * also has the value <code>true</code>. However, there are two
  401. * exceptions:
  402. * <ul>
  403. * <li>If <code>d1</code> and <code>d2</code> both represent
  404. * <code>Double.NaN</code>, then the <code>equals</code> method
  405. * returns <code>true</code>, even though
  406. * <code>Double.NaN==Double.NaN</code> has the value
  407. * <code>false</code>.
  408. * <li>If <code>d1</code> represents <code>+0.0</code> while
  409. * <code>d2</code> represents <code>-0.0</code>, or vice versa,
  410. * the <code>equal</code> test has the value <code>false</code>,
  411. * even though <code>+0.0==-0.0</code> has the value <code>true</code>.
  412. * </ul>
  413. * This definition allows hash tables to operate properly.
  414. * @param obj the object to compare with.
  415. * @return <code>true</code> if the objects are the same;
  416. * <code>false</code> otherwise.
  417. * @see java.lang.Double#doubleToLongBits(double)
  418. */
  419. public boolean equals(Object obj) {
  420. return (obj instanceof Double)
  421. && (doubleToLongBits(((Double)obj).value) ==
  422. doubleToLongBits(value));
  423. }
  424. /**
  425. * Returns a representation of the specified floating-point value
  426. * according to the IEEE 754 floating-point "double
  427. * format" bit layout.
  428. * <p>
  429. * Bit 63 (the bit that is selected by the mask
  430. * <code>0x8000000000000000L</code>) represents the sign of the
  431. * floating-point number. Bits
  432. * 62-52 (the bits that are selected by the mask
  433. * <code>0x7ff0000000000000L</code>) represent the exponent. Bits 51-0
  434. * (the bits that are selected by the mask
  435. * <code>0x000fffffffffffffL</code>) represent the significand
  436. * (sometimes called the mantissa) of the floating-point number.
  437. * <p>
  438. * If the argument is positive infinity, the result is
  439. * <code>0x7ff0000000000000L</code>.
  440. * <p>
  441. * If the argument is negative infinity, the result is
  442. * <code>0xfff0000000000000L</code>.
  443. * <p>
  444. * If the argument is NaN, the result is
  445. * <code>0x7ff8000000000000L</code>.
  446. * <p>
  447. * In all cases, the result is a <code>long</code> integer that, when
  448. * given to the {@link #longBitsToDouble(long)} method, will produce a
  449. * floating-point value the same as the argument to
  450. * <code>doubleToLongBits</code> (except all NaN values are
  451. * collapsed to a single "canonical" NaN value).
  452. *
  453. * @param value a <code>double</code> precision floating-point number.
  454. * @return the bits that represent the floating-point number.
  455. */
  456. public static native long doubleToLongBits(double value);
  457. /**
  458. * Returns a representation of the specified floating-point value
  459. * according to the IEEE 754 floating-point "double
  460. * format" bit layout, preserving Not-a-Number (NaN) values.
  461. * <p>
  462. * Bit 63 (the bit that is selected by the mask
  463. * <code>0x8000000000000000L</code>) represents the sign of the
  464. * floating-point number. Bits
  465. * 62-52 (the bits that are selected by the mask
  466. * <code>0x7ff0000000000000L</code>) represent the exponent. Bits 51-0
  467. * (the bits that are selected by the mask
  468. * <code>0x000fffffffffffffL</code>) represent the significand
  469. * (sometimes called the mantissa) of the floating-point number.
  470. * <p>
  471. * If the argument is positive infinity, the result is
  472. * <code>0x7ff0000000000000L</code>.
  473. * <p>
  474. * If the argument is negative infinity, the result is
  475. * <code>0xfff0000000000000L</code>.
  476. * <p>
  477. * If the argument is NaN, the result is the <code>long</code>
  478. * integer representing the actual NaN value. Unlike the
  479. * <code>doubleToLongBits</code> method,
  480. * <code>doubleToRawLongBits</code> does not collapse all the bit
  481. * patterns encoding a NaN to a single "canonical" NaN
  482. * value.
  483. * <p>
  484. * In all cases, the result is a <code>long</code> integer that,
  485. * when given to the {@link #longBitsToDouble(long)} method, will
  486. * produce a floating-point value the same as the argument to
  487. * <code>doubleToRawLongBits</code>.
  488. *
  489. * @param value a <code>double</code> precision floating-point number.
  490. * @return the bits that represent the floating-point number.
  491. */
  492. public static native long doubleToRawLongBits(double value);
  493. /**
  494. * Returns the <code>double</code> value corresponding to a given
  495. * bit representation.
  496. * The argument is considered to be a representation of a
  497. * floating-point value according to the IEEE 754 floating-point
  498. * "double format" bit layout.
  499. * <p>
  500. * If the argument is <code>0x7ff0000000000000L</code>, the result
  501. * is positive infinity.
  502. * <p>
  503. * If the argument is <code>0xfff0000000000000L</code>, the result
  504. * is negative infinity.
  505. * <p>
  506. * If the argument is any value in the range
  507. * <code>0x7ff0000000000001L</code> through
  508. * <code>0x7fffffffffffffffL</code> or in the range
  509. * <code>0xfff0000000000001L</code> through
  510. * <code>0xffffffffffffffffL</code>, the result is a NaN. No IEEE
  511. * 754 floating-point operation provided by Java can distinguish
  512. * between two NaN values of the same type with different bit
  513. * patterns. Distinct values of NaN are only distinguishable by
  514. * use of the <code>Double.doubleToRawLongBits</code> method.
  515. * <p>
  516. * In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
  517. * values that can be computed from the argument:
  518. * <blockquote><pre>
  519. * int s = ((bits >> 63) == 0) ? 1 : -1;
  520. * int e = (int)((bits >> 52) & 0x7ffL);
  521. * long m = (e == 0) ?
  522. * (bits & 0xfffffffffffffL) << 1 :
  523. * (bits & 0xfffffffffffffL) | 0x10000000000000L;
  524. * </pre></blockquote>
  525. * Then the floating-point result equals the value of the mathematical
  526. * expression <i>s</i>·<i>m</i>·2<sup><i>e</i>-1075</sup>.
  527. *<p>
  528. * Note that this method may not be able to return a
  529. * <code>double</code> NaN with exactly same bit pattern as the
  530. * <code>long</code> argument. IEEE 754 distinguishes between two
  531. * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The
  532. * differences between the two kinds of NaN are generally not
  533. * visible in Java. Arithmetic operations on signaling NaNs turn
  534. * them into quiet NaNs with a different, but often similar, bit
  535. * pattern. However, on some processors merely copying a
  536. * signaling NaN also performs that conversion. In particular,
  537. * copying a signaling NaN to return it to the calling method
  538. * may perform this conversion. So <code>longBitsToDouble</code>
  539. * may not be able to return a <code>double</code> with a
  540. * signaling NaN bit pattern. Consequently, for some
  541. * <code>long</code> values,
  542. * <code>doubleToRawLongBits(longBitsToDouble(start))</code> may
  543. * <i>not</i> equal <code>start</code>. Moreover, which
  544. * particular bit patterns represent signaling NaNs is platform
  545. * dependent; although all NaN bit patterns, quiet or signaling,
  546. * must be in the NaN range identified above.
  547. *
  548. * @param bits any <code>long</code> integer.
  549. * @return the <code>double</code> floating-point value with the same
  550. * bit pattern.
  551. */
  552. public static native double longBitsToDouble(long bits);
  553. /**
  554. * Compares two <code>Double</code> objects numerically. There
  555. * are two ways in which comparisons performed by this method
  556. * differ from those performed by the Java language numerical
  557. * comparison operators (<code><, <=, ==, >= ></code>)
  558. * when applied to primitive <code>double</code> values:
  559. * <ul><li>
  560. * <code>Double.NaN</code> is considered by this method
  561. * to be equal to itself and greater than all other
  562. * <code>double</code> values (including
  563. * <code>Double.POSITIVE_INFINITY</code>).
  564. * <li>
  565. * <code>0.0d</code> is considered by this method to be greater
  566. * than <code>-0.0d</code>.
  567. * </ul>
  568. * This ensures that <code>Double.compareTo(Object)</code> (which
  569. * forwards its behavior to this method) obeys the general
  570. * contract for <code>Comparable.compareTo</code>, and that the
  571. * <i>natural order</i> on <code>Double</code>s is <i>consistent
  572. * with equals</i>.
  573. *
  574. * @param anotherDouble the <code>Double</code> to be compared.
  575. * @return the value <code>0</code> if <code>anotherDouble</code> is
  576. * numerically equal to this <code>Double</code> a value
  577. * less than <code>0</code> if this <code>Double</code>
  578. * is numerically less than <code>anotherDouble</code>
  579. * and a value greater than <code>0</code> if this
  580. * <code>Double</code> is numerically greater than
  581. * <code>anotherDouble</code>.
  582. *
  583. * @since 1.2
  584. * @see Comparable#compareTo(Object)
  585. */
  586. public int compareTo(Double anotherDouble) {
  587. return Double.compare(value, anotherDouble.value);
  588. }
  589. /**
  590. * Compares this <code>Double</code> object to another object. If
  591. * the object is a <code>Double</code>, this function behaves like
  592. * <code>compareTo(Double)</code>. Otherwise, it throws a
  593. * <code>ClassCastException</code> (as <code>Double</code> objects
  594. * are comparable only to other <code>Double</code> objects).
  595. *
  596. * @param o the <code>Object</code> to be compared.
  597. * @return the value <code>0</code> if the argument is a
  598. * <code>Double</code> numerically equal to this
  599. * <code>Double</code> a value less than <code>0</code>
  600. * if the argument is a <code>Double</code> numerically
  601. * greater than this <code>Double</code> and a value
  602. * greater than <code>0</code> if the argument is a
  603. * <code>Double</code> numerically less than this
  604. * <code>Double</code>.
  605. * @exception <code>ClassCastException</code> if the argument is not a
  606. * <code>Double</code>.
  607. * @see java.lang.Comparable
  608. * @since 1.2
  609. */
  610. public int compareTo(Object o) {
  611. return compareTo((Double)o);
  612. }
  613. /**
  614. * Compares the two specified <code>double</code> values. The sign
  615. * of the integer value returned is the same as that of the
  616. * integer that would be returned by the call:
  617. * <pre>
  618. * new Double(d1).compareTo(new Double(d2))
  619. * </pre>
  620. *
  621. * @param d1 the first <code>double</code> to compare
  622. * @param d2 the second <code>double</code> to compare
  623. * @return the value <code>0</code> if <code>d1</code> is
  624. * numerically equal to <code>d2</code> a value less than
  625. * <code>0</code> if <code>d1</code> is numerically less than
  626. * <code>d2</code> and a value greater than <code>0</code>
  627. * if <code>d1</code> is numerically greater than
  628. * <code>d2</code>.
  629. * @since 1.4
  630. */
  631. public static int compare(double d1, double d2) {
  632. if (d1 < d2)
  633. return -1; // Neither val is NaN, thisVal is smaller
  634. if (d1 > d2)
  635. return 1; // Neither val is NaN, thisVal is larger
  636. long thisBits = Double.doubleToLongBits(d1);
  637. long anotherBits = Double.doubleToLongBits(d2);
  638. return (thisBits == anotherBits ? 0 : // Values are equal
  639. (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
  640. 1)); // (0.0, -0.0) or (NaN, !NaN)
  641. }
  642. /** use serialVersionUID from JDK 1.0.2 for interoperability */
  643. private static final long serialVersionUID = -9172774392245257468L;
  644. }