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