1. /*
  2. * @(#)Double.java 1.94 04/05/11
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. import sun.misc.FloatingDecimal;
  9. import sun.misc.FpUtils;
  10. import sun.misc.DoubleConsts;
  11. /**
  12. * The <code>Double</code> 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. * @author Joseph D. Darcy
  26. * @version 1.94, 05/11/04
  27. * @since JDK1.0
  28. */
  29. public final class Double extends Number implements Comparable<Double> {
  30. /**
  31. * A constant holding the positive infinity of type
  32. * <code>double</code>. It is equal to the value returned by
  33. * <code>Double.longBitsToDouble(0x7ff0000000000000L)</code>.
  34. */
  35. public static final double POSITIVE_INFINITY = 1.0 / 0.0;
  36. /**
  37. * A constant holding the negative infinity of type
  38. * <code>double</code>. It is equal to the value returned by
  39. * <code>Double.longBitsToDouble(0xfff0000000000000L)</code>.
  40. */
  41. public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
  42. /**
  43. * A constant holding a Not-a-Number (NaN) value of type
  44. * <code>double</code>. It is equivalent to the value returned by
  45. * <code>Double.longBitsToDouble(0x7ff8000000000000L)</code>.
  46. */
  47. public static final double NaN = 0.0d / 0.0;
  48. /**
  49. * A constant holding the largest positive finite value of type
  50. * <code>double</code>,
  51. * (2-2<sup>-52</sup>)·2<sup>1023</sup>. It is equal to
  52. * the hexadecimal floating-point literal
  53. * <code>0x1.fffffffffffffP+1023</code> and also equal to
  54. * <code>Double.longBitsToDouble(0x7fefffffffffffffL)</code>.
  55. */
  56. public static final double MAX_VALUE = 1.7976931348623157e+308; // 0x1.fffffffffffffP+1023
  57. /**
  58. * A constant holding the smallest positive nonzero value of type
  59. * <code>double</code>, 2<sup>-1074</sup>. It is equal to the
  60. * hexadecimal floating-point literal
  61. * <code>0x0.0000000000001P-1022</code> and also equal to
  62. * <code>Double.longBitsToDouble(0x1L)</code>.
  63. */
  64. public static final double MIN_VALUE = 4.9e-324; // 0x0.0000000000001P-1022
  65. /**
  66. * The number of bits used to represent a <tt>double</tt> value.
  67. *
  68. * @since 1.5
  69. */
  70. public static final int SIZE = 64;
  71. /**
  72. * The <code>Class</code> instance representing the primitive type
  73. * <code>double</code>.
  74. *
  75. * @since JDK1.1
  76. */
  77. public static final Class<Double> TYPE = (Class<Double>) Class.getPrimitiveClass("double");
  78. /**
  79. * Returns a string representation of the <code>double</code>
  80. * argument. All characters mentioned below are ASCII characters.
  81. * <ul>
  82. * <li>If the argument is NaN, the result is the string
  83. * "<code>NaN</code>".
  84. * <li>Otherwise, the result is a string that represents the sign and
  85. * magnitude (absolute value) of the argument. If the sign is negative,
  86. * the first character of the result is '<code>-</code>'
  87. * (<code>'\u002D'</code>); if the sign is positive, no sign character
  88. * appears in the result. As for the magnitude <i>m</i>:
  89. * <ul>
  90. * <li>If <i>m</i> is infinity, it is represented by the characters
  91. * <code>"Infinity"</code> thus, positive infinity produces the result
  92. * <code>"Infinity"</code> and negative infinity produces the result
  93. * <code>"-Infinity"</code>.
  94. *
  95. * <li>If <i>m</i> is zero, it is represented by the characters
  96. * <code>"0.0"</code> thus, negative zero produces the result
  97. * <code>"-0.0"</code> and positive zero produces the result
  98. * <code>"0.0"</code>.
  99. *
  100. * <li>If <i>m</i> is greater than or equal to 10<sup>-3</sup> but less
  101. * than 10<sup>7</sup>, then it is represented as the integer part of
  102. * <i>m</i>, in decimal form with no leading zeroes, followed by
  103. * '<code>.</code>' (<code>'\u002E'</code>), followed by one or
  104. * more decimal digits representing the fractional part of <i>m</i>.
  105. *
  106. * <li>If <i>m</i> is less than 10<sup>-3</sup> or greater than or
  107. * equal to 10<sup>7</sup>, then it is represented in so-called
  108. * "computerized scientific notation." Let <i>n</i> be the unique
  109. * integer such that 10<sup><i>n</i></sup> <= <i>m</i> <
  110. * 10<sup><i>n</i>+1</sup> then let <i>a</i> be the
  111. * mathematically exact quotient of <i>m</i> and
  112. * 10<sup><i>n</i></sup> so that 1 <= <i>a</i> < 10. The
  113. * magnitude is then represented as the integer part of <i>a</i>,
  114. * as a single decimal digit, followed by '<code>.</code>'
  115. * (<code>'\u002E'</code>), followed by decimal digits
  116. * representing the fractional part of <i>a</i>, followed by the
  117. * letter '<code>E</code>' (<code>'\u0045'</code>), followed
  118. * by a representation of <i>n</i> as a decimal integer, as
  119. * produced by the method {@link Integer#toString(int)}.
  120. * </ul>
  121. * </ul>
  122. * How many digits must be printed for the fractional part of
  123. * <i>m</i> or <i>a</i>? There must be at least one digit to represent
  124. * the fractional part, and beyond that as many, but only as many, more
  125. * digits as are needed to uniquely distinguish the argument value from
  126. * adjacent values of type <code>double</code>. That is, suppose that
  127. * <i>x</i> is the exact mathematical value represented by the decimal
  128. * representation produced by this method for a finite nonzero argument
  129. * <i>d</i>. Then <i>d</i> must be the <code>double</code> value nearest
  130. * to <i>x</i> or if two <code>double</code> values are equally close
  131. * to <i>x</i>, then <i>d</i> must be one of them and the least
  132. * significant bit of the significand of <i>d</i> must be <code>0</code>.
  133. * <p>
  134. * To create localized string representations of a floating-point
  135. * value, use subclasses of {@link java.text.NumberFormat}.
  136. *
  137. * @param d the <code>double</code> to be converted.
  138. * @return a string representation of the argument.
  139. */
  140. public static String toString(double d) {
  141. return new FloatingDecimal(d).toJavaFormatString();
  142. }
  143. /**
  144. * Returns a hexadecimal string representation of the
  145. * <code>double</code> argument. All characters mentioned below
  146. * are ASCII characters.
  147. *
  148. * <ul>
  149. * <li>If the argument is NaN, the result is the string
  150. * "<code>NaN</code>".
  151. * <li>Otherwise, the result is a string that represents the sign
  152. * and magnitude of the argument. If the sign is negative, the
  153. * first character of the result is '<code>-</code>'
  154. * (<code>'\u002D'</code>); if the sign is positive, no sign
  155. * character appears in the result. As for the magnitude <i>m</i>:
  156. *
  157. * <ul>
  158. * <li>If <i>m</i> is infinity, it is represented by the string
  159. * <code>"Infinity"</code> thus, positive infinity produces the
  160. * result <code>"Infinity"</code> and negative infinity produces
  161. * the result <code>"-Infinity"</code>.
  162. *
  163. * <li>If <i>m</i> is zero, it is represented by the string
  164. * <code>"0x0.0p0"</code> thus, negative zero produces the result
  165. * <code>"-0x0.0p0"</code> and positive zero produces the result
  166. * <code>"0x0.0p0"</code>.
  167. *
  168. * <li>If <i>m</i> is a <code>double</code> value with a
  169. * normalized representation, substrings are used to represent the
  170. * significand and exponent fields. The significand is
  171. * represented by the characters <code>"0x1."</code>
  172. * followed by a lowercase hexadecimal representation of the rest
  173. * of the significand as a fraction. Trailing zeros in the
  174. * hexadecimal representation are removed unless all the digits
  175. * are zero, in which case a single zero is used. Next, the
  176. * exponent is represented by <code>"p"</code> followed
  177. * by a decimal string of the unbiased exponent as if produced by
  178. * a call to {@link Integer#toString(int) Integer.toString} on the
  179. * exponent value.
  180. *
  181. * <li>If <i>m</i> is a <code>double</code> value with a subnormal
  182. * representation, the significand is represented by the
  183. * characters <code>"0x0."</code> followed by a
  184. * hexadecimal representation of the rest of the significand as a
  185. * fraction. Trailing zeros in the hexadecimal representation are
  186. * removed. Next, the exponent is represented by
  187. * <code>"p-1022"</code>. Note that there must be at
  188. * least one nonzero digit in a subnormal significand.
  189. *
  190. * </ul>
  191. *
  192. * </ul>
  193. *
  194. * <table border>
  195. * <caption><h3>Examples</h3></caption>
  196. * <tr><th>Floating-point Value</th><th>Hexadecimal String</th>
  197. * <tr><td><code>1.0</code></td> <td><code>0x1.0p0</code></td>
  198. * <tr><td><code>-1.0</code></td> <td><code>-0x1.0p0</code></td>
  199. * <tr><td><code>2.0</code></td> <td><code>0x1.0p1</code></td>
  200. * <tr><td><code>3.0</code></td> <td><code>0x1.8p1</code></td>
  201. * <tr><td><code>0.5</code></td> <td><code>0x1.0p-1</code></td>
  202. * <tr><td><code>0.25</code></td> <td><code>0x1.0p-2</code></td>
  203. * <tr><td><code>Double.MAX_VALUE</code></td>
  204. * <td><code>0x1.fffffffffffffp1023</code></td>
  205. * <tr><td><code>Minimum Normal Value</code></td>
  206. * <td><code>0x1.0p-1022</code></td>
  207. * <tr><td><code>Maximum Subnormal Value</code></td>
  208. * <td><code>0x0.fffffffffffffp-1022</code></td>
  209. * <tr><td><code>Double.MIN_VALUE</code></td>
  210. * <td><code>0x0.0000000000001p-1022</code></td>
  211. * </table>
  212. * @param d the <code>double</code> to be converted.
  213. * @return a hex string representation of the argument.
  214. * @since 1.5
  215. * @author Joseph D. Darcy
  216. */
  217. public static String toHexString(double d) {
  218. /*
  219. * Modeled after the "a" conversion specifier in C99, section
  220. * 7.19.6.1; however, the output of this method is more
  221. * tightly specified.
  222. */
  223. if (!FpUtils.isFinite(d) )
  224. // For infinity and NaN, use the decimal output.
  225. return Double.toString(d);
  226. else {
  227. // Initialized to maximum size of output.
  228. StringBuffer answer = new StringBuffer(24);
  229. if (FpUtils.rawCopySign(1.0, d) == -1.0) // value is negative,
  230. answer.append("-"); // so append sign info
  231. answer.append("0x");
  232. d = Math.abs(d);
  233. if(d == 0.0) {
  234. answer.append("0.0p0");
  235. }
  236. else {
  237. boolean subnormal = (d < DoubleConsts.MIN_NORMAL);
  238. // Isolate significand bits and OR in a high-order bit
  239. // so that the string representation has a known
  240. // length.
  241. long signifBits = (Double.doubleToLongBits(d)
  242. & DoubleConsts.SIGNIF_BIT_MASK) |
  243. 0x1000000000000000L;
  244. // Subnormal values have a 0 implicit bit; normal
  245. // values have a a 1 implicit bit.
  246. answer.append(subnormal ? "0." : "1.");
  247. // Isolate the low-order 13 digits of the hex
  248. // representation. If all the digits are zero,
  249. // replace with a single 0; otherwise, remove all
  250. // trailing zeros.
  251. String signif = Long.toHexString(signifBits).substring(3,16);
  252. answer.append(signif.equals("0000000000000") ? // 13 zeros
  253. "0":
  254. signif.replaceFirst("0{1,12}$", ""));
  255. // If the value is subnormal, use the E_min exponent
  256. // value for double; otherwise, extract and report d's
  257. // exponent (the representation of a subnormal uses
  258. // E_min -1).
  259. answer.append("p" + (subnormal ?
  260. DoubleConsts.MIN_EXPONENT:
  261. FpUtils.getExponent(d) ));
  262. }
  263. return answer.toString();
  264. }
  265. }
  266. /**
  267. * Returns a <code>Double</code> object holding the
  268. * <code>double</code> value represented by the argument string
  269. * <code>s</code>.
  270. *
  271. * <p>If <code>s</code> is <code>null</code>, then a
  272. * <code>NullPointerException</code> is thrown.
  273. *
  274. * <p>Leading and trailing whitespace characters in <code>s</code>
  275. * are ignored. Whitespace is removed as if by the {@link
  276. * String#trim} method; that is, both ASCII space and control
  277. * characters are removed. The rest of <code>s</code> should
  278. * constitute a <i>FloatValue</i> as described by the lexical
  279. * syntax rules:
  280. *
  281. * <blockquote>
  282. * <dl>
  283. * <dt><i>FloatValue:</i>
  284. * <dd><i>Sign<sub>opt</sub></i> <code>NaN</code>
  285. * <dd><i>Sign<sub>opt</sub></i> <code>Infinity</code>
  286. * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
  287. * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
  288. * <dd><i>SignedInteger</i>
  289. * </dl>
  290. *
  291. * <p>
  292. *
  293. * <dl>
  294. * <dt><i>HexFloatingPointLiteral</i>:
  295. * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
  296. * </dl>
  297. *
  298. * <p>
  299. *
  300. * <dl>
  301. * <dt><i>HexSignificand:</i>
  302. * <dd><i>HexNumeral</i>
  303. * <dd><i>HexNumeral</i> <code>.</code>
  304. * <dd><code>0x</code> <i>HexDigits<sub>opt</sub>
  305. * </i><code>.</code><i> HexDigits</i>
  306. * <dd><code>0X</code><i> HexDigits<sub>opt</sub>
  307. * </i><code>.</code> <i>HexDigits</i>
  308. * </dl>
  309. *
  310. * <p>
  311. *
  312. * <dl>
  313. * <dt><i>BinaryExponent:</i>
  314. * <dd><i>BinaryExponentIndicator SignedInteger</i>
  315. * </dl>
  316. *
  317. * <p>
  318. *
  319. * <dl>
  320. * <dt><i>BinaryExponentIndicator:</i>
  321. * <dd><code>p</code>
  322. * <dd><code>P</code>
  323. * </dl>
  324. *
  325. * </blockquote>
  326. *
  327. * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
  328. * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
  329. * <i>FloatTypeSuffix</i> are as defined in the lexical structure
  330. * sections of the of the <a
  331. * href="http://java.sun.com/docs/books/jls/html/">Java Language
  332. * Specification</a>. If <code>s</code> does not have the form of
  333. * a <i>FloatValue</i>, then a <code>NumberFormatException</code>
  334. * is thrown. Otherwise, <code>s</code> is regarded as
  335. * representing an exact decimal value in the usual
  336. * "computerized scientific notation" or as an exact
  337. * hexadecimal value; this exact numerical value is then
  338. * conceptually converted to an "infinitely precise"
  339. * binary value that is then rounded to type <code>double</code>
  340. * by the usual round-to-nearest rule of IEEE 754 floating-point
  341. * arithmetic, which includes preserving the sign of a zero
  342. * value. Finally, a <code>Double</code> object representing this
  343. * <code>double</code> value is returned.
  344. *
  345. * <p> To interpret localized string representations of a
  346. * floating-point value, use subclasses of {@link
  347. * java.text.NumberFormat}.
  348. *
  349. * <p>Note that trailing format specifiers, specifiers that
  350. * determine the type of a floating-point literal
  351. * (<code>1.0f</code> is a <code>float</code> value;
  352. * <code>1.0d</code> is a <code>double</code> value), do
  353. * <em>not</em> influence the results of this method. In other
  354. * words, the numerical value of the input string is converted
  355. * directly to the target floating-point type. The two-step
  356. * sequence of conversions, string to <code>float</code> followed
  357. * by <code>float</code> to <code>double</code>, is <em>not</em>
  358. * equivalent to converting a string directly to
  359. * <code>double</code>. For example, the <code>float</code>
  360. * literal <code>0.1f</code> is equal to the <code>double</code>
  361. * value <code>0.10000000149011612</code> the <code>float</code>
  362. * literal <code>0.1f</code> represents a different numerical
  363. * value than the <code>double</code> literal
  364. * <code>0.1</code>. (The numerical value 0.1 cannot be exactly
  365. * represented in a binary floating-point number.)
  366. *
  367. * <p>To avoid calling this method on a invalid string and having
  368. * a <code>NumberFormatException</code> be thrown, the regular
  369. * expression below can be used to screen the input string:
  370. *
  371. * <code>
  372. * <pre>
  373. * final String Digits = "(\\p{Digit}+)";
  374. * final String HexDigits = "(\\p{XDigit}+)";
  375. * // an exponent is 'e' or 'E' followed by an optionally
  376. * // signed decimal integer.
  377. * final String Exp = "[eE][+-]?"+Digits;
  378. * final String fpRegex =
  379. * ("[\\x00-\\x20]*"+ // Optional leading "whitespace"
  380. * "[+-]?(" + // Optional sign character
  381. * "NaN|" + // "NaN" string
  382. * "Infinity|" + // "Infinity" string
  383. *
  384. * // A decimal floating-point string representing a finite positive
  385. * // number without a leading sign has at most five basic pieces:
  386. * // Digits . Digits ExponentPart FloatTypeSuffix
  387. * //
  388. * // Since this method allows integer-only strings as input
  389. * // in addition to strings of floating-point literals, the
  390. * // two sub-patterns below are simplifications of the grammar
  391. * // productions from the Java Language Specification, 2nd
  392. * // edition, section 3.10.2.
  393. *
  394. * // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
  395. * "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
  396. *
  397. * // . Digits ExponentPart_opt FloatTypeSuffix_opt
  398. * "(\\.("+Digits+")("+Exp+")?)|"+
  399. *
  400. * // Hexadecimal strings
  401. * "((" +
  402. * // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
  403. * "(0[xX]" + HexDigits + "(\\.)?)|" +
  404. *
  405. * // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
  406. * "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
  407. *
  408. * ")[pP][+-]?" + Digits + "))" +
  409. * "[fFdD]?))" +
  410. * "[\\x00-\\x20]*");// Optional trailing "whitespace"
  411. *
  412. * if (Pattern.matches(fpRegex, myString))
  413. * Double.valueOf(myString); // Will not throw NumberFormatException
  414. * else {
  415. * // Perform suitable alternative action
  416. * }
  417. * </pre>
  418. * </code>
  419. *
  420. * @param s the string to be parsed.
  421. * @return a <code>Double</code> object holding the value
  422. * represented by the <code>String</code> argument.
  423. * @exception NumberFormatException if the string does not contain a
  424. * parsable number.
  425. */
  426. public static Double valueOf(String s) throws NumberFormatException {
  427. return new Double(FloatingDecimal.readJavaFormatString(s).doubleValue());
  428. }
  429. /**
  430. * Returns a <tt>Double</tt> instance representing the specified
  431. * <tt>double</tt> value.
  432. * If a new <tt>Double</tt> instance is not required, this method
  433. * should generally be used in preference to the constructor
  434. * {@link #Double(double)}, as this method is likely to yield
  435. * significantly better space and time performance by caching
  436. * frequently requested values.
  437. *
  438. * @param d a double value.
  439. * @return a <tt>Double</tt> instance representing <tt>d</tt>.
  440. * @since 1.5
  441. */
  442. public static Double valueOf(double d) {
  443. return new Double(d);
  444. }
  445. /**
  446. * Returns a new <code>double</code> initialized to the value
  447. * represented by the specified <code>String</code>, as performed
  448. * by the <code>valueOf</code> method of class
  449. * <code>Double</code>.
  450. *
  451. * @param s the string to be parsed.
  452. * @return the <code>double</code> value represented by the string
  453. * argument.
  454. * @exception NumberFormatException if the string does not contain
  455. * a parsable <code>double</code>.
  456. * @see java.lang.Double#valueOf(String)
  457. * @since 1.2
  458. */
  459. public static double parseDouble(String s) throws NumberFormatException {
  460. return FloatingDecimal.readJavaFormatString(s).doubleValue();
  461. }
  462. /**
  463. * Returns <code>true</code> if the specified number is a
  464. * Not-a-Number (NaN) value, <code>false</code> otherwise.
  465. *
  466. * @param v the value to be tested.
  467. * @return <code>true</code> if the value of the argument is NaN;
  468. * <code>false</code> otherwise.
  469. */
  470. static public boolean isNaN(double v) {
  471. return (v != v);
  472. }
  473. /**
  474. * Returns <code>true</code> if the specified number is infinitely
  475. * large in magnitude, <code>false</code> otherwise.
  476. *
  477. * @param v the value to be tested.
  478. * @return <code>true</code> if the value of the argument is positive
  479. * infinity or negative infinity; <code>false</code> otherwise.
  480. */
  481. static public boolean isInfinite(double v) {
  482. return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
  483. }
  484. /**
  485. * The value of the Double.
  486. *
  487. * @serial
  488. */
  489. private final double value;
  490. /**
  491. * Constructs a newly allocated <code>Double</code> object that
  492. * represents the primitive <code>double</code> argument.
  493. *
  494. * @param value the value to be represented by the <code>Double</code>.
  495. */
  496. public Double(double value) {
  497. this.value = value;
  498. }
  499. /**
  500. * Constructs a newly allocated <code>Double</code> object that
  501. * represents the floating-point value of type <code>double</code>
  502. * represented by the string. The string is converted to a
  503. * <code>double</code> value as if by the <code>valueOf</code> method.
  504. *
  505. * @param s a string to be converted to a <code>Double</code>.
  506. * @exception NumberFormatException if the string does not contain a
  507. * parsable number.
  508. * @see java.lang.Double#valueOf(java.lang.String)
  509. */
  510. public Double(String s) throws NumberFormatException {
  511. // REMIND: this is inefficient
  512. this(valueOf(s).doubleValue());
  513. }
  514. /**
  515. * Returns <code>true</code> if this <code>Double</code> value is
  516. * a Not-a-Number (NaN), <code>false</code> otherwise.
  517. *
  518. * @return <code>true</code> if the value represented by this object is
  519. * NaN; <code>false</code> otherwise.
  520. */
  521. public boolean isNaN() {
  522. return isNaN(value);
  523. }
  524. /**
  525. * Returns <code>true</code> if this <code>Double</code> value is
  526. * infinitely large in magnitude, <code>false</code> otherwise.
  527. *
  528. * @return <code>true</code> if the value represented by this object is
  529. * positive infinity or negative infinity;
  530. * <code>false</code> otherwise.
  531. */
  532. public boolean isInfinite() {
  533. return isInfinite(value);
  534. }
  535. /**
  536. * Returns a string representation of this <code>Double</code> object.
  537. * The primitive <code>double</code> value represented by this
  538. * object is converted to a string exactly as if by the method
  539. * <code>toString</code> of one argument.
  540. *
  541. * @return a <code>String</code> representation of this object.
  542. * @see java.lang.Double#toString(double)
  543. */
  544. public String toString() {
  545. return String.valueOf(value);
  546. }
  547. /**
  548. * Returns the value of this <code>Double</code> as a <code>byte</code> (by
  549. * casting to a <code>byte</code>).
  550. *
  551. * @return the <code>double</code> value represented by this object
  552. * converted to type <code>byte</code>
  553. * @since JDK1.1
  554. */
  555. public byte byteValue() {
  556. return (byte)value;
  557. }
  558. /**
  559. * Returns the value of this <code>Double</code> as a
  560. * <code>short</code> (by casting to a <code>short</code>).
  561. *
  562. * @return the <code>double</code> value represented by this object
  563. * converted to type <code>short</code>
  564. * @since JDK1.1
  565. */
  566. public short shortValue() {
  567. return (short)value;
  568. }
  569. /**
  570. * Returns the value of this <code>Double</code> as an
  571. * <code>int</code> (by casting to type <code>int</code>).
  572. *
  573. * @return the <code>double</code> value represented by this object
  574. * converted to type <code>int</code>
  575. */
  576. public int intValue() {
  577. return (int)value;
  578. }
  579. /**
  580. * Returns the value of this <code>Double</code> as a
  581. * <code>long</code> (by casting to type <code>long</code>).
  582. *
  583. * @return the <code>double</code> value represented by this object
  584. * converted to type <code>long</code>
  585. */
  586. public long longValue() {
  587. return (long)value;
  588. }
  589. /**
  590. * Returns the <code>float</code> value of this
  591. * <code>Double</code> object.
  592. *
  593. * @return the <code>double</code> value represented by this object
  594. * converted to type <code>float</code>
  595. * @since JDK1.0
  596. */
  597. public float floatValue() {
  598. return (float)value;
  599. }
  600. /**
  601. * Returns the <code>double</code> value of this
  602. * <code>Double</code> object.
  603. *
  604. * @return the <code>double</code> value represented by this object
  605. */
  606. public double doubleValue() {
  607. return (double)value;
  608. }
  609. /**
  610. * Returns a hash code for this <code>Double</code> object. The
  611. * result is the exclusive OR of the two halves of the
  612. * <code>long</code> integer bit representation, exactly as
  613. * produced by the method {@link #doubleToLongBits(double)}, of
  614. * the primitive <code>double</code> value represented by this
  615. * <code>Double</code> object. That is, the hash code is the value
  616. * of the expression:
  617. * <blockquote><pre>
  618. * (int)(v^(v>>>32))
  619. * </pre></blockquote>
  620. * where <code>v</code> is defined by:
  621. * <blockquote><pre>
  622. * long v = Double.doubleToLongBits(this.doubleValue());
  623. * </pre></blockquote>
  624. *
  625. * @return a <code>hash code</code> value for this object.
  626. */
  627. public int hashCode() {
  628. long bits = doubleToLongBits(value);
  629. return (int)(bits ^ (bits >>> 32));
  630. }
  631. /**
  632. * Compares this object against the specified object. The result
  633. * is <code>true</code> if and only if the argument is not
  634. * <code>null</code> and is a <code>Double</code> object that
  635. * represents a <code>double</code> that has the same value as the
  636. * <code>double</code> represented by this object. For this
  637. * purpose, two <code>double</code> values are considered to be
  638. * the same if and only if the method {@link
  639. * #doubleToLongBits(double)} returns the identical
  640. * <code>long</code> value when applied to each.
  641. * <p>
  642. * Note that in most cases, for two instances of class
  643. * <code>Double</code>, <code>d1</code> and <code>d2</code>, the
  644. * value of <code>d1.equals(d2)</code> is <code>true</code> if and
  645. * only if
  646. * <blockquote><pre>
  647. * d1.doubleValue() == d2.doubleValue()
  648. * </pre></blockquote>
  649. * <p>
  650. * also has the value <code>true</code>. However, there are two
  651. * exceptions:
  652. * <ul>
  653. * <li>If <code>d1</code> and <code>d2</code> both represent
  654. * <code>Double.NaN</code>, then the <code>equals</code> method
  655. * returns <code>true</code>, even though
  656. * <code>Double.NaN==Double.NaN</code> has the value
  657. * <code>false</code>.
  658. * <li>If <code>d1</code> represents <code>+0.0</code> while
  659. * <code>d2</code> represents <code>-0.0</code>, or vice versa,
  660. * the <code>equal</code> test has the value <code>false</code>,
  661. * even though <code>+0.0==-0.0</code> has the value <code>true</code>.
  662. * </ul>
  663. * This definition allows hash tables to operate properly.
  664. * @param obj the object to compare with.
  665. * @return <code>true</code> if the objects are the same;
  666. * <code>false</code> otherwise.
  667. * @see java.lang.Double#doubleToLongBits(double)
  668. */
  669. public boolean equals(Object obj) {
  670. return (obj instanceof Double)
  671. && (doubleToLongBits(((Double)obj).value) ==
  672. doubleToLongBits(value));
  673. }
  674. /**
  675. * Returns a representation of the specified floating-point value
  676. * according to the IEEE 754 floating-point "double
  677. * format" bit layout.
  678. * <p>
  679. * Bit 63 (the bit that is selected by the mask
  680. * <code>0x8000000000000000L</code>) represents the sign of the
  681. * floating-point number. Bits
  682. * 62-52 (the bits that are selected by the mask
  683. * <code>0x7ff0000000000000L</code>) represent the exponent. Bits 51-0
  684. * (the bits that are selected by the mask
  685. * <code>0x000fffffffffffffL</code>) represent the significand
  686. * (sometimes called the mantissa) of the floating-point number.
  687. * <p>
  688. * If the argument is positive infinity, the result is
  689. * <code>0x7ff0000000000000L</code>.
  690. * <p>
  691. * If the argument is negative infinity, the result is
  692. * <code>0xfff0000000000000L</code>.
  693. * <p>
  694. * If the argument is NaN, the result is
  695. * <code>0x7ff8000000000000L</code>.
  696. * <p>
  697. * In all cases, the result is a <code>long</code> integer that, when
  698. * given to the {@link #longBitsToDouble(long)} method, will produce a
  699. * floating-point value the same as the argument to
  700. * <code>doubleToLongBits</code> (except all NaN values are
  701. * collapsed to a single "canonical" NaN value).
  702. *
  703. * @param value a <code>double</code> precision floating-point number.
  704. * @return the bits that represent the floating-point number.
  705. */
  706. public static native long doubleToLongBits(double value);
  707. /**
  708. * Returns a representation of the specified floating-point value
  709. * according to the IEEE 754 floating-point "double
  710. * format" bit layout, preserving Not-a-Number (NaN) values.
  711. * <p>
  712. * Bit 63 (the bit that is selected by the mask
  713. * <code>0x8000000000000000L</code>) represents the sign of the
  714. * floating-point number. Bits
  715. * 62-52 (the bits that are selected by the mask
  716. * <code>0x7ff0000000000000L</code>) represent the exponent. Bits 51-0
  717. * (the bits that are selected by the mask
  718. * <code>0x000fffffffffffffL</code>) represent the significand
  719. * (sometimes called the mantissa) of the floating-point number.
  720. * <p>
  721. * If the argument is positive infinity, the result is
  722. * <code>0x7ff0000000000000L</code>.
  723. * <p>
  724. * If the argument is negative infinity, the result is
  725. * <code>0xfff0000000000000L</code>.
  726. * <p>
  727. * If the argument is NaN, the result is the <code>long</code>
  728. * integer representing the actual NaN value. Unlike the
  729. * <code>doubleToLongBits</code> method,
  730. * <code>doubleToRawLongBits</code> does not collapse all the bit
  731. * patterns encoding a NaN to a single "canonical" NaN
  732. * value.
  733. * <p>
  734. * In all cases, the result is a <code>long</code> integer that,
  735. * when given to the {@link #longBitsToDouble(long)} method, will
  736. * produce a floating-point value the same as the argument to
  737. * <code>doubleToRawLongBits</code>.
  738. *
  739. * @param value a <code>double</code> precision floating-point number.
  740. * @return the bits that represent the floating-point number.
  741. */
  742. public static native long doubleToRawLongBits(double value);
  743. /**
  744. * Returns the <code>double</code> value corresponding to a given
  745. * bit representation.
  746. * The argument is considered to be a representation of a
  747. * floating-point value according to the IEEE 754 floating-point
  748. * "double format" bit layout.
  749. * <p>
  750. * If the argument is <code>0x7ff0000000000000L</code>, the result
  751. * is positive infinity.
  752. * <p>
  753. * If the argument is <code>0xfff0000000000000L</code>, the result
  754. * is negative infinity.
  755. * <p>
  756. * If the argument is any value in the range
  757. * <code>0x7ff0000000000001L</code> through
  758. * <code>0x7fffffffffffffffL</code> or in the range
  759. * <code>0xfff0000000000001L</code> through
  760. * <code>0xffffffffffffffffL</code>, the result is a NaN. No IEEE
  761. * 754 floating-point operation provided by Java can distinguish
  762. * between two NaN values of the same type with different bit
  763. * patterns. Distinct values of NaN are only distinguishable by
  764. * use of the <code>Double.doubleToRawLongBits</code> method.
  765. * <p>
  766. * In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
  767. * values that can be computed from the argument:
  768. * <blockquote><pre>
  769. * int s = ((bits >> 63) == 0) ? 1 : -1;
  770. * int e = (int)((bits >> 52) & 0x7ffL);
  771. * long m = (e == 0) ?
  772. * (bits & 0xfffffffffffffL) << 1 :
  773. * (bits & 0xfffffffffffffL) | 0x10000000000000L;
  774. * </pre></blockquote>
  775. * Then the floating-point result equals the value of the mathematical
  776. * expression <i>s</i>·<i>m</i>·2<sup><i>e</i>-1075</sup>.
  777. *<p>
  778. * Note that this method may not be able to return a
  779. * <code>double</code> NaN with exactly same bit pattern as the
  780. * <code>long</code> argument. IEEE 754 distinguishes between two
  781. * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The
  782. * differences between the two kinds of NaN are generally not
  783. * visible in Java. Arithmetic operations on signaling NaNs turn
  784. * them into quiet NaNs with a different, but often similar, bit
  785. * pattern. However, on some processors merely copying a
  786. * signaling NaN also performs that conversion. In particular,
  787. * copying a signaling NaN to return it to the calling method
  788. * may perform this conversion. So <code>longBitsToDouble</code>
  789. * may not be able to return a <code>double</code> with a
  790. * signaling NaN bit pattern. Consequently, for some
  791. * <code>long</code> values,
  792. * <code>doubleToRawLongBits(longBitsToDouble(start))</code> may
  793. * <i>not</i> equal <code>start</code>. Moreover, which
  794. * particular bit patterns represent signaling NaNs is platform
  795. * dependent; although all NaN bit patterns, quiet or signaling,
  796. * must be in the NaN range identified above.
  797. *
  798. * @param bits any <code>long</code> integer.
  799. * @return the <code>double</code> floating-point value with the same
  800. * bit pattern.
  801. */
  802. public static native double longBitsToDouble(long bits);
  803. /**
  804. * Compares two <code>Double</code> objects numerically. There
  805. * are two ways in which comparisons performed by this method
  806. * differ from those performed by the Java language numerical
  807. * comparison operators (<code><, <=, ==, >= ></code>)
  808. * when applied to primitive <code>double</code> values:
  809. * <ul><li>
  810. * <code>Double.NaN</code> is considered by this method
  811. * to be equal to itself and greater than all other
  812. * <code>double</code> values (including
  813. * <code>Double.POSITIVE_INFINITY</code>).
  814. * <li>
  815. * <code>0.0d</code> is considered by this method to be greater
  816. * than <code>-0.0d</code>.
  817. * </ul>
  818. * This ensures that the <i>natural ordering</i> of
  819. * <tt>Double</tt> objects imposed by this method is <i>consistent
  820. * with equals</i>.
  821. *
  822. * @param anotherDouble the <code>Double</code> to be compared.
  823. * @return the value <code>0</code> if <code>anotherDouble</code> is
  824. * numerically equal to this <code>Double</code> a value
  825. * less than <code>0</code> if this <code>Double</code>
  826. * is numerically less than <code>anotherDouble</code>
  827. * and a value greater than <code>0</code> if this
  828. * <code>Double</code> is numerically greater than
  829. * <code>anotherDouble</code>.
  830. *
  831. * @since 1.2
  832. */
  833. public int compareTo(Double anotherDouble) {
  834. return Double.compare(value, anotherDouble.value);
  835. }
  836. /**
  837. * Compares the two specified <code>double</code> values. The sign
  838. * of the integer value returned is the same as that of the
  839. * integer that would be returned by the call:
  840. * <pre>
  841. * new Double(d1).compareTo(new Double(d2))
  842. * </pre>
  843. *
  844. * @param d1 the first <code>double</code> to compare
  845. * @param d2 the second <code>double</code> to compare
  846. * @return the value <code>0</code> if <code>d1</code> is
  847. * numerically equal to <code>d2</code> a value less than
  848. * <code>0</code> if <code>d1</code> is numerically less than
  849. * <code>d2</code> and a value greater than <code>0</code>
  850. * if <code>d1</code> is numerically greater than
  851. * <code>d2</code>.
  852. * @since 1.4
  853. */
  854. public static int compare(double d1, double d2) {
  855. if (d1 < d2)
  856. return -1; // Neither val is NaN, thisVal is smaller
  857. if (d1 > d2)
  858. return 1; // Neither val is NaN, thisVal is larger
  859. long thisBits = Double.doubleToLongBits(d1);
  860. long anotherBits = Double.doubleToLongBits(d2);
  861. return (thisBits == anotherBits ? 0 : // Values are equal
  862. (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
  863. 1)); // (0.0, -0.0) or (NaN, !NaN)
  864. }
  865. /** use serialVersionUID from JDK 1.0.2 for interoperability */
  866. private static final long serialVersionUID = -9172774392245257468L;
  867. }