1. /*
  2. * @(#)Math.java 1.57 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. import java.util.Random;
  9. /**
  10. * The class <code>Math</code> contains methods for performing basic
  11. * numeric operations such as the elementary exponential, logarithm,
  12. * square root, and trigonometric functions.
  13. * <p>
  14. * Unlike some of the numeric methods of class
  15. * <code>StrictMath</code>, all implementations of the equivalent
  16. * functions of class <code>Math</code> are not defined to return the
  17. * bit-for-bit same results. This relaxation permits
  18. * better-performing implementations where strict reproducibility is
  19. * not required.
  20. * <p>
  21. * By default many of the <code>Math</code> methods simply call
  22. * the equivalent method in <code>StrictMath</code> for their
  23. * implementation. Code generators are encouraged to use
  24. * platform-specific native libraries or microprocessor instructions,
  25. * where available, to provide higher-performance implementations of
  26. * <code>Math</code> methods. Such higher-performance
  27. * implementations still must conform to the specification for
  28. * <code>Math</code>.
  29. * <p>
  30. * The quality of implementation specifications concern two
  31. * properties, accuracy of the returned result and monotonicity of the
  32. * method. Accuracy of the floating-point <code>Math</code> methods
  33. * is measured in terms of <i>ulps</i>, units in the last place. For
  34. * a given floating-point format, an ulp of a specific real number
  35. * value is the difference between the two floating-point values
  36. * closest to that numerical value. When discussing the accuracy of a
  37. * method as a whole rather than at a specific argument, the number of
  38. * ulps cited is for the worst-case error at any argument. If a
  39. * method always has an error less than 0.5 ulps, the method always
  40. * returns the floating-point number nearest the exact result; such a
  41. * method is <i>correctly rounded</i>. A correctly rounded method is
  42. * generally the best a floating-point approximation can be; however,
  43. * it is impractical for many floating-point methods to be correctly
  44. * rounded. Instead, for the <code>Math</code> class, a larger error
  45. * bound of 1 or 2 ulps is allowed for certain methods. Informally,
  46. * with a 1 ulp error bound, when the exact result is a representable
  47. * number the exact result should be returned; otherwise, either of
  48. * the two floating-point numbers closest to the exact result may be
  49. * returned. Besides accuracy at individual arguments, maintaining
  50. * proper relations between the method at different arguments is also
  51. * important. Therefore, methods with more than 0.5 ulp errors are
  52. * required to be <i>semi-monotonic</i>: whenever the mathematical
  53. * function is non-decreasing, so is the floating-point approximation,
  54. * likewise, whenever the mathematical function is non-increasing, so
  55. * is the floating-point approximation. Not all approximations that
  56. * have 1 ulp accuracy will automatically meet the monotonicity
  57. * requirements.
  58. *
  59. * @author unascribed
  60. * @version 1.57, 01/23/03
  61. * @since JDK1.0
  62. */
  63. public final strictfp class Math {
  64. /**
  65. * Don't let anyone instantiate this class.
  66. */
  67. private Math() {}
  68. /**
  69. * The <code>double</code> value that is closer than any other to
  70. * <i>e</i>, the base of the natural logarithms.
  71. */
  72. public static final double E = 2.7182818284590452354;
  73. /**
  74. * The <code>double</code> value that is closer than any other to
  75. * <i>pi</i>, the ratio of the circumference of a circle to its
  76. * diameter.
  77. */
  78. public static final double PI = 3.14159265358979323846;
  79. /**
  80. * Returns the trigonometric sine of an angle. Special cases:
  81. * <ul><li>If the argument is NaN or an infinity, then the
  82. * result is NaN.
  83. * <li>If the argument is zero, then the result is a zero with the
  84. * same sign as the argument.</ul>
  85. * <p>
  86. * A result must be within 1 ulp of the correctly rounded result. Results
  87. * must be semi-monotonic.
  88. *
  89. * @param a an angle, in radians.
  90. * @return the sine of the argument.
  91. */
  92. public static double sin(double a) {
  93. return StrictMath.sin(a); // default impl. delegates to StrictMath
  94. }
  95. /**
  96. * Returns the trigonometric cosine of an angle. Special cases:
  97. * <ul><li>If the argument is NaN or an infinity, then the
  98. * result is NaN.</ul>
  99. * <p>
  100. * A result must be within 1 ulp of the correctly rounded result. Results
  101. * must be semi-monotonic.
  102. *
  103. * @param a an angle, in radians.
  104. * @return the cosine of the argument.
  105. */
  106. public static double cos(double a) {
  107. return StrictMath.cos(a); // default impl. delegates to StrictMath
  108. }
  109. /**
  110. * Returns the trigonometric tangent of an angle. Special cases:
  111. * <ul><li>If the argument is NaN or an infinity, then the result
  112. * is NaN.
  113. * <li>If the argument is zero, then the result is a zero with the
  114. * same sign as the argument.</ul>
  115. * <p>
  116. * A result must be within 1 ulp of the correctly rounded result. Results
  117. * must be semi-monotonic.
  118. *
  119. * @param a an angle, in radians.
  120. * @return the tangent of the argument.
  121. */
  122. public static double tan(double a) {
  123. return StrictMath.tan(a); // default impl. delegates to StrictMath
  124. }
  125. /**
  126. * Returns the arc sine of an angle, in the range of -<i>pi</i>/2 through
  127. * <i>pi</i>/2. Special cases:
  128. * <ul><li>If the argument is NaN or its absolute value is greater
  129. * than 1, then the result is NaN.
  130. * <li>If the argument is zero, then the result is a zero with the
  131. * same sign as the argument.</ul>
  132. * <p>
  133. * A result must be within 1 ulp of the correctly rounded result. Results
  134. * must be semi-monotonic.
  135. *
  136. * @param a the value whose arc sine is to be returned.
  137. * @return the arc sine of the argument.
  138. */
  139. public static double asin(double a) {
  140. return StrictMath.asin(a); // default impl. delegates to StrictMath
  141. }
  142. /**
  143. * Returns the arc cosine of an angle, in the range of 0.0 through
  144. * <i>pi</i>. Special case:
  145. * <ul><li>If the argument is NaN or its absolute value is greater
  146. * than 1, then the result is NaN.</ul>
  147. * <p>
  148. * A result must be within 1 ulp of the correctly rounded result. Results
  149. * must be semi-monotonic.
  150. *
  151. * @param a the value whose arc cosine is to be returned.
  152. * @return the arc cosine of the argument.
  153. */
  154. public static double acos(double a) {
  155. return StrictMath.acos(a); // default impl. delegates to StrictMath
  156. }
  157. /**
  158. * Returns the arc tangent of an angle, in the range of -<i>pi</i>/2
  159. * through <i>pi</i>/2. Special cases:
  160. * <ul><li>If the argument is NaN, then the result is NaN.
  161. * <li>If the argument is zero, then the result is a zero with the
  162. * same sign as the argument.</ul>
  163. * <p>
  164. * A result must be within 1 ulp of the correctly rounded result. Results
  165. * must be semi-monotonic.
  166. *
  167. * @param a the value whose arc tangent is to be returned.
  168. * @return the arc tangent of the argument.
  169. */
  170. public static double atan(double a) {
  171. return StrictMath.atan(a); // default impl. delegates to StrictMath
  172. }
  173. /**
  174. * Converts an angle measured in degrees to an approximately
  175. * equivalent angle measured in radians. The conversion from
  176. * degrees to radians is generally inexact.
  177. *
  178. * @param angdeg an angle, in degrees
  179. * @return the measurement of the angle <code>angdeg</code>
  180. * in radians.
  181. * @since 1.2
  182. */
  183. public static double toRadians(double angdeg) {
  184. return angdeg / 180.0 * PI;
  185. }
  186. /**
  187. * Converts an angle measured in radians to an approximately
  188. * equivalent angle measured in degrees. The conversion from
  189. * radians to degrees is generally inexact; users should
  190. * <i>not</i> expect <code>cos(toRadians(90.0))</code> to exactly
  191. * equal <code>0.0</code>.
  192. *
  193. * @param angrad an angle, in radians
  194. * @return the measurement of the angle <code>angrad</code>
  195. * in degrees.
  196. * @since 1.2
  197. */
  198. public static double toDegrees(double angrad) {
  199. return angrad * 180.0 / PI;
  200. }
  201. /**
  202. * Returns Euler's number <i>e</i> raised to the power of a
  203. * <code>double</code> value. Special cases:
  204. * <ul><li>If the argument is NaN, the result is NaN.
  205. * <li>If the argument is positive infinity, then the result is
  206. * positive infinity.
  207. * <li>If the argument is negative infinity, then the result is
  208. * positive zero.</ul>
  209. * <p>
  210. * A result must be within 1 ulp of the correctly rounded result. Results
  211. * must be semi-monotonic.
  212. *
  213. * @param a the exponent to raise <i>e</i> to.
  214. * @return the value <i>e</i><sup><code>a</code></sup>,
  215. * where <i>e</i> is the base of the natural logarithms.
  216. */
  217. public static double exp(double a) {
  218. return StrictMath.exp(a); // default impl. delegates to StrictMath
  219. }
  220. /**
  221. * Returns the natural logarithm (base <i>e</i>) of a <code>double</code>
  222. * value. Special cases:
  223. * <ul><li>If the argument is NaN or less than zero, then the result
  224. * is NaN.
  225. * <li>If the argument is positive infinity, then the result is
  226. * positive infinity.
  227. * <li>If the argument is positive zero or negative zero, then the
  228. * result is negative infinity.</ul>
  229. * <p>
  230. * A result must be within 1 ulp of the correctly rounded result. Results
  231. * must be semi-monotonic.
  232. *
  233. * @param a a number greater than <code>0.0</code>.
  234. * @return the value ln <code>a</code>, the natural logarithm of
  235. * <code>a</code>.
  236. */
  237. public static double log(double a) {
  238. return StrictMath.log(a); // default impl. delegates to StrictMath
  239. }
  240. /**
  241. * Returns the correctly rounded positive square root of a
  242. * <code>double</code> value.
  243. * Special cases:
  244. * <ul><li>If the argument is NaN or less than zero, then the result
  245. * is NaN.
  246. * <li>If the argument is positive infinity, then the result is positive
  247. * infinity.
  248. * <li>If the argument is positive zero or negative zero, then the
  249. * result is the same as the argument.</ul>
  250. * Otherwise, the result is the <code>double</code> value closest to
  251. * the true mathematical square root of the argument value.
  252. *
  253. * @param a a value.
  254. * <!--@return the value of √ <code>a</code>.-->
  255. * @return the positive square root of <code>a</code>.
  256. * If the argument is NaN or less than zero, the result is NaN.
  257. */
  258. public static double sqrt(double a) {
  259. return StrictMath.sqrt(a); // default impl. delegates to StrictMath
  260. // Note that hardware sqrt instructions
  261. // frequently can be directly used by JITs
  262. // and should be much faster than doing
  263. // Math.sqrt in software.
  264. }
  265. /**
  266. * Computes the remainder operation on two arguments as prescribed
  267. * by the IEEE 754 standard.
  268. * The remainder value is mathematically equal to
  269. * <code>f1 - f2</code> × <i>n</i>,
  270. * where <i>n</i> is the mathematical integer closest to the exact
  271. * mathematical value of the quotient <code>f1/f2</code>, and if two
  272. * mathematical integers are equally close to <code>f1/f2</code>,
  273. * then <i>n</i> is the integer that is even. If the remainder is
  274. * zero, its sign is the same as the sign of the first argument.
  275. * Special cases:
  276. * <ul><li>If either argument is NaN, or the first argument is infinite,
  277. * or the second argument is positive zero or negative zero, then the
  278. * result is NaN.
  279. * <li>If the first argument is finite and the second argument is
  280. * infinite, then the result is the same as the first argument.</ul>
  281. *
  282. * @param f1 the dividend.
  283. * @param f2 the divisor.
  284. * @return the remainder when <code>f1</code> is divided by
  285. * <code>f2</code>.
  286. */
  287. public static double IEEEremainder(double f1, double f2) {
  288. return StrictMath.IEEEremainder(f1, f2); // delegate to StrictMath
  289. }
  290. /**
  291. * Returns the smallest (closest to negative infinity)
  292. * <code>double</code> value that is not less than the argument and is
  293. * equal to a mathematical integer. Special cases:
  294. * <ul><li>If the argument value is already equal to a mathematical
  295. * integer, then the result is the same as the argument.
  296. * <li>If the argument is NaN or an infinity or positive zero or negative
  297. * zero, then the result is the same as the argument.
  298. * <li>If the argument value is less than zero but greater than -1.0,
  299. * then the result is negative zero.</ul>
  300. * Note that the value of <code>Math.ceil(x)</code> is exactly the
  301. * value of <code>-Math.floor(-x)</code>.
  302. *
  303. * @param a a value.
  304. * <!--@return the value ⌈ <code>a</code> ⌉.-->
  305. * @return the smallest (closest to negative infinity)
  306. * floating-point value that is not less than the argument
  307. * and is equal to a mathematical integer.
  308. */
  309. public static double ceil(double a) {
  310. return StrictMath.ceil(a); // default impl. delegates to StrictMath
  311. }
  312. /**
  313. * Returns the largest (closest to positive infinity)
  314. * <code>double</code> value that is not greater than the argument and
  315. * is equal to a mathematical integer. Special cases:
  316. * <ul><li>If the argument value is already equal to a mathematical
  317. * integer, then the result is the same as the argument.
  318. * <li>If the argument is NaN or an infinity or positive zero or
  319. * negative zero, then the result is the same as the argument.</ul>
  320. *
  321. * @param a a value.
  322. * <!--@return the value ⌊ <code>a</code> ⌋.-->
  323. * @return the largest (closest to positive infinity)
  324. * floating-point value that is not greater than the argument
  325. * and is equal to a mathematical integer.
  326. */
  327. public static double floor(double a) {
  328. return StrictMath.floor(a); // default impl. delegates to StrictMath
  329. }
  330. /**
  331. * Returns the <code>double</code> value that is closest in value
  332. * to the argument and is equal to a mathematical integer. If two
  333. * <code>double</code> values that are mathematical integers are
  334. * equally close, the result is the integer value that is
  335. * even. Special cases:
  336. * <ul><li>If the argument value is already equal to a mathematical
  337. * integer, then the result is the same as the argument.
  338. * <li>If the argument is NaN or an infinity or positive zero or negative
  339. * zero, then the result is the same as the argument.</ul>
  340. *
  341. * @param a a <code>double</code> value.
  342. * @return the closest floating-point value to <code>a</code> that is
  343. * equal to a mathematical integer.
  344. */
  345. public static double rint(double a) {
  346. return StrictMath.rint(a); // default impl. delegates to StrictMath
  347. }
  348. /**
  349. * Converts rectangular coordinates (<code>x</code>, <code>y</code>)
  350. * to polar (r, <i>theta</i>).
  351. * This method computes the phase <i>theta</i> by computing an arc tangent
  352. * of <code>y/x</code> in the range of -<i>pi</i> to <i>pi</i>. Special
  353. * cases:
  354. * <ul><li>If either argument is NaN, then the result is NaN.
  355. * <li>If the first argument is positive zero and the second argument
  356. * is positive, or the first argument is positive and finite and the
  357. * second argument is positive infinity, then the result is positive
  358. * zero.
  359. * <li>If the first argument is negative zero and the second argument
  360. * is positive, or the first argument is negative and finite and the
  361. * second argument is positive infinity, then the result is negative zero.
  362. * <li>If the first argument is positive zero and the second argument
  363. * is negative, or the first argument is positive and finite and the
  364. * second argument is negative infinity, then the result is the
  365. * <code>double</code> value closest to <i>pi</i>.
  366. * <li>If the first argument is negative zero and the second argument
  367. * is negative, or the first argument is negative and finite and the
  368. * second argument is negative infinity, then the result is the
  369. * <code>double</code> value closest to -<i>pi</i>.
  370. * <li>If the first argument is positive and the second argument is
  371. * positive zero or negative zero, or the first argument is positive
  372. * infinity and the second argument is finite, then the result is the
  373. * <code>double</code> value closest to <i>pi</i>/2.
  374. * <li>If the first argument is negative and the second argument is
  375. * positive zero or negative zero, or the first argument is negative
  376. * infinity and the second argument is finite, then the result is the
  377. * <code>double</code> value closest to -<i>pi</i>/2.
  378. * <li>If both arguments are positive infinity, then the result is the
  379. * <code>double</code> value closest to <i>pi</i>/4.
  380. * <li>If the first argument is positive infinity and the second argument
  381. * is negative infinity, then the result is the <code>double</code>
  382. * value closest to 3*<i>pi</i>/4.
  383. * <li>If the first argument is negative infinity and the second argument
  384. * is positive infinity, then the result is the <code>double</code> value
  385. * closest to -<i>pi</i>/4.
  386. * <li>If both arguments are negative infinity, then the result is the
  387. * <code>double</code> value closest to -3*<i>pi</i>/4.</ul>
  388. * <p>
  389. * A result must be within 2 ulps of the correctly rounded result. Results
  390. * must be semi-monotonic.
  391. *
  392. * @param y the ordinate coordinate
  393. * @param x the abscissa coordinate
  394. * @return the <i>theta</i> component of the point
  395. * (<i>r</i>, <i>theta</i>)
  396. * in polar coordinates that corresponds to the point
  397. * (<i>x</i>, <i>y</i>) in Cartesian coordinates.
  398. */
  399. public static double atan2(double y, double x) {
  400. return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
  401. }
  402. /**
  403. * Returns the value of the first argument raised to the power of the
  404. * second argument. Special cases:
  405. *
  406. * <ul><li>If the second argument is positive or negative zero, then the
  407. * result is 1.0.
  408. * <li>If the second argument is 1.0, then the result is the same as the
  409. * first argument.
  410. * <li>If the second argument is NaN, then the result is NaN.
  411. * <li>If the first argument is NaN and the second argument is nonzero,
  412. * then the result is NaN.
  413. *
  414. * <li>If
  415. * <ul>
  416. * <li>the absolute value of the first argument is greater than 1
  417. * and the second argument is positive infinity, or
  418. * <li>the absolute value of the first argument is less than 1 and
  419. * the second argument is negative infinity,
  420. * </ul>
  421. * then the result is positive infinity.
  422. *
  423. * <li>If
  424. * <ul>
  425. * <li>the absolute value of the first argument is greater than 1 and
  426. * the second argument is negative infinity, or
  427. * <li>the absolute value of the
  428. * first argument is less than 1 and the second argument is positive
  429. * infinity,
  430. * </ul>
  431. * then the result is positive zero.
  432. *
  433. * <li>If the absolute value of the first argument equals 1 and the
  434. * second argument is infinite, then the result is NaN.
  435. *
  436. * <li>If
  437. * <ul>
  438. * <li>the first argument is positive zero and the second argument
  439. * is greater than zero, or
  440. * <li>the first argument is positive infinity and the second
  441. * argument is less than zero,
  442. * </ul>
  443. * then the result is positive zero.
  444. *
  445. * <li>If
  446. * <ul>
  447. * <li>the first argument is positive zero and the second argument
  448. * is less than zero, or
  449. * <li>the first argument is positive infinity and the second
  450. * argument is greater than zero,
  451. * </ul>
  452. * then the result is positive infinity.
  453. *
  454. * <li>If
  455. * <ul>
  456. * <li>the first argument is negative zero and the second argument
  457. * is greater than zero but not a finite odd integer, or
  458. * <li>the first argument is negative infinity and the second
  459. * argument is less than zero but not a finite odd integer,
  460. * </ul>
  461. * then the result is positive zero.
  462. *
  463. * <li>If
  464. * <ul>
  465. * <li>the first argument is negative zero and the second argument
  466. * is a positive finite odd integer, or
  467. * <li>the first argument is negative infinity and the second
  468. * argument is a negative finite odd integer,
  469. * </ul>
  470. * then the result is negative zero.
  471. *
  472. * <li>If
  473. * <ul>
  474. * <li>the first argument is negative zero and the second argument
  475. * is less than zero but not a finite odd integer, or
  476. * <li>the first argument is negative infinity and the second
  477. * argument is greater than zero but not a finite odd integer,
  478. * </ul>
  479. * then the result is positive infinity.
  480. *
  481. * <li>If
  482. * <ul>
  483. * <li>the first argument is negative zero and the second argument
  484. * is a negative finite odd integer, or
  485. * <li>the first argument is negative infinity and the second
  486. * argument is a positive finite odd integer,
  487. * </ul>
  488. * then the result is negative infinity.
  489. *
  490. * <li>If the first argument is finite and less than zero
  491. * <ul>
  492. * <li> if the second argument is a finite even integer, the
  493. * result is equal to the result of raising the absolute value of
  494. * the first argument to the power of the second argument
  495. *
  496. * <li>if the second argument is a finite odd integer, the result
  497. * is equal to the negative of the result of raising the absolute
  498. * value of the first argument to the power of the second
  499. * argument
  500. *
  501. * <li>if the second argument is finite and not an integer, then
  502. * the result is NaN.
  503. * </ul>
  504. *
  505. * <li>If both arguments are integers, then the result is exactly equal
  506. * to the mathematical result of raising the first argument to the power
  507. * of the second argument if that result can in fact be represented
  508. * exactly as a <code>double</code> value.</ul>
  509. *
  510. * <p>(In the foregoing descriptions, a floating-point value is
  511. * considered to be an integer if and only if it is finite and a
  512. * fixed point of the method {@link #ceil <tt>ceil</tt>} or,
  513. * equivalently, a fixed point of the method {@link #floor
  514. * <tt>floor</tt>}. A value is a fixed point of a one-argument
  515. * method if and only if the result of applying the method to the
  516. * value is equal to the value.)
  517. *
  518. * <p>A result must be within 1 ulp of the correctly rounded
  519. * result. Results must be semi-monotonic.
  520. *
  521. * @param a the base.
  522. * @param b the exponent.
  523. * @return the value <code>a<sup>b</sup></code>.
  524. */
  525. public static double pow(double a, double b) {
  526. return StrictMath.pow(a, b); // default impl. delegates to StrictMath
  527. }
  528. /**
  529. * Returns the closest <code>int</code> to the argument. The
  530. * result is rounded to an integer by adding 1/2, taking the
  531. * floor of the result, and casting the result to type <code>int</code>.
  532. * In other words, the result is equal to the value of the expression:
  533. * <p><pre>(int)Math.floor(a + 0.5f)</pre>
  534. * <p>
  535. * Special cases:
  536. * <ul><li>If the argument is NaN, the result is 0.
  537. * <li>If the argument is negative infinity or any value less than or
  538. * equal to the value of <code>Integer.MIN_VALUE</code>, the result is
  539. * equal to the value of <code>Integer.MIN_VALUE</code>.
  540. * <li>If the argument is positive infinity or any value greater than or
  541. * equal to the value of <code>Integer.MAX_VALUE</code>, the result is
  542. * equal to the value of <code>Integer.MAX_VALUE</code>.</ul>
  543. *
  544. * @param a a floating-point value to be rounded to an integer.
  545. * @return the value of the argument rounded to the nearest
  546. * <code>int</code> value.
  547. * @see java.lang.Integer#MAX_VALUE
  548. * @see java.lang.Integer#MIN_VALUE
  549. */
  550. public static int round(float a) {
  551. return (int)floor(a + 0.5f);
  552. }
  553. /**
  554. * Returns the closest <code>long</code> to the argument. The result
  555. * is rounded to an integer by adding 1/2, taking the floor of the
  556. * result, and casting the result to type <code>long</code>. In other
  557. * words, the result is equal to the value of the expression:
  558. * <p><pre>(long)Math.floor(a + 0.5d)</pre>
  559. * <p>
  560. * Special cases:
  561. * <ul><li>If the argument is NaN, the result is 0.
  562. * <li>If the argument is negative infinity or any value less than or
  563. * equal to the value of <code>Long.MIN_VALUE</code>, the result is
  564. * equal to the value of <code>Long.MIN_VALUE</code>.
  565. * <li>If the argument is positive infinity or any value greater than or
  566. * equal to the value of <code>Long.MAX_VALUE</code>, the result is
  567. * equal to the value of <code>Long.MAX_VALUE</code>.</ul>
  568. *
  569. * @param a a floating-point value to be rounded to a
  570. * <code>long</code>.
  571. * @return the value of the argument rounded to the nearest
  572. * <code>long</code> value.
  573. * @see java.lang.Long#MAX_VALUE
  574. * @see java.lang.Long#MIN_VALUE
  575. */
  576. public static long round(double a) {
  577. return (long)floor(a + 0.5d);
  578. }
  579. private static Random randomNumberGenerator;
  580. private static synchronized void initRNG() {
  581. if (randomNumberGenerator == null)
  582. randomNumberGenerator = new Random();
  583. }
  584. /**
  585. * Returns a <code>double</code> value with a positive sign, greater
  586. * than or equal to <code>0.0</code> and less than <code>1.0</code>.
  587. * Returned values are chosen pseudorandomly with (approximately)
  588. * uniform distribution from that range.
  589. * <p>
  590. * When this method is first called, it creates a single new
  591. * pseudorandom-number generator, exactly as if by the expression
  592. * <blockquote><pre>new java.util.Random</pre></blockquote>
  593. * This new pseudorandom-number generator is used thereafter for all
  594. * calls to this method and is used nowhere else.
  595. * <p>
  596. * This method is properly synchronized to allow correct use by more
  597. * than one thread. However, if many threads need to generate
  598. * pseudorandom numbers at a great rate, it may reduce contention for
  599. * each thread to have its own pseudorandom-number generator.
  600. *
  601. * @return a pseudorandom <code>double</code> greater than or equal
  602. * to <code>0.0</code> and less than <code>1.0</code>.
  603. * @see java.util.Random#nextDouble()
  604. */
  605. public static double random() {
  606. if (randomNumberGenerator == null) initRNG();
  607. return randomNumberGenerator.nextDouble();
  608. }
  609. /**
  610. * Returns the absolute value of an <code>int</code> value.
  611. * If the argument is not negative, the argument is returned.
  612. * If the argument is negative, the negation of the argument is returned.
  613. * <p>
  614. * Note that if the argument is equal to the value of
  615. * <code>Integer.MIN_VALUE</code>, the most negative representable
  616. * <code>int</code> value, the result is that same value, which is
  617. * negative.
  618. *
  619. * @param a the argument whose absolute value is to be determined
  620. * @return the absolute value of the argument.
  621. * @see java.lang.Integer#MIN_VALUE
  622. */
  623. public static int abs(int a) {
  624. return (a < 0) ? -a : a;
  625. }
  626. /**
  627. * Returns the absolute value of a <code>long</code> value.
  628. * If the argument is not negative, the argument is returned.
  629. * If the argument is negative, the negation of the argument is returned.
  630. * <p>
  631. * Note that if the argument is equal to the value of
  632. * <code>Long.MIN_VALUE</code>, the most negative representable
  633. * <code>long</code> value, the result is that same value, which is
  634. * negative.
  635. *
  636. * @param a the argument whose absolute value is to be determined
  637. * @return the absolute value of the argument.
  638. * @see java.lang.Long#MIN_VALUE
  639. */
  640. public static long abs(long a) {
  641. return (a < 0) ? -a : a;
  642. }
  643. /**
  644. * Returns the absolute value of a <code>float</code> value.
  645. * If the argument is not negative, the argument is returned.
  646. * If the argument is negative, the negation of the argument is returned.
  647. * Special cases:
  648. * <ul><li>If the argument is positive zero or negative zero, the
  649. * result is positive zero.
  650. * <li>If the argument is infinite, the result is positive infinity.
  651. * <li>If the argument is NaN, the result is NaN.</ul>
  652. * In other words, the result is the same as the value of the expression:
  653. * <p><pre>Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))</pre>
  654. *
  655. * @param a the argument whose absolute value is to be determined
  656. * @return the absolute value of the argument.
  657. */
  658. public static float abs(float a) {
  659. return (a <= 0.0F) ? 0.0F - a : a;
  660. }
  661. /**
  662. * Returns the absolute value of a <code>double</code> value.
  663. * If the argument is not negative, the argument is returned.
  664. * If the argument is negative, the negation of the argument is returned.
  665. * Special cases:
  666. * <ul><li>If the argument is positive zero or negative zero, the result
  667. * is positive zero.
  668. * <li>If the argument is infinite, the result is positive infinity.
  669. * <li>If the argument is NaN, the result is NaN.</ul>
  670. * In other words, the result is the same as the value of the expression:
  671. * <p><code>Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)</code>
  672. *
  673. * @param a the argument whose absolute value is to be determined
  674. * @return the absolute value of the argument.
  675. */
  676. public static double abs(double a) {
  677. return (a <= 0.0D) ? 0.0D - a : a;
  678. }
  679. /**
  680. * Returns the greater of two <code>int</code> values. That is, the
  681. * result is the argument closer to the value of
  682. * <code>Integer.MAX_VALUE</code>. If the arguments have the same value,
  683. * the result is that same value.
  684. *
  685. * @param a an argument.
  686. * @param b another argument.
  687. * @return the larger of <code>a</code> and <code>b</code>.
  688. * @see java.lang.Long#MAX_VALUE
  689. */
  690. public static int max(int a, int b) {
  691. return (a >= b) ? a : b;
  692. }
  693. /**
  694. * Returns the greater of two <code>long</code> values. That is, the
  695. * result is the argument closer to the value of
  696. * <code>Long.MAX_VALUE</code>. If the arguments have the same value,
  697. * the result is that same value.
  698. *
  699. * @param a an argument.
  700. * @param b another argument.
  701. * @return the larger of <code>a</code> and <code>b</code>.
  702. * @see java.lang.Long#MAX_VALUE
  703. */
  704. public static long max(long a, long b) {
  705. return (a >= b) ? a : b;
  706. }
  707. private static long negativeZeroFloatBits = Float.floatToIntBits(-0.0f);
  708. private static long negativeZeroDoubleBits = Double.doubleToLongBits(-0.0d);
  709. /**
  710. * Returns the greater of two <code>float</code> values. That is,
  711. * the result is the argument closer to positive infinity. If the
  712. * arguments have the same value, the result is that same
  713. * value. If either value is NaN, then the result is NaN. Unlike
  714. * the the numerical comparison operators, this method considers
  715. * negative zero to be strictly smaller than positive zero. If one
  716. * argument is positive zero and the other negative zero, the
  717. * result is positive zero.
  718. *
  719. * @param a an argument.
  720. * @param b another argument.
  721. * @return the larger of <code>a</code> and <code>b</code>.
  722. */
  723. public static float max(float a, float b) {
  724. if (a != a) return a; // a is NaN
  725. if ((a == 0.0f) && (b == 0.0f)
  726. && (Float.floatToIntBits(a) == negativeZeroFloatBits)) {
  727. return b;
  728. }
  729. return (a >= b) ? a : b;
  730. }
  731. /**
  732. * Returns the greater of two <code>double</code> values. That
  733. * is, the result is the argument closer to positive infinity. If
  734. * the arguments have the same value, the result is that same
  735. * value. If either value is NaN, then the result is NaN. Unlike
  736. * the the numerical comparison operators, this method considers
  737. * negative zero to be strictly smaller than positive zero. If one
  738. * argument is positive zero and the other negative zero, the
  739. * result is positive zero.
  740. *
  741. * @param a an argument.
  742. * @param b another argument.
  743. * @return the larger of <code>a</code> and <code>b</code>.
  744. */
  745. public static double max(double a, double b) {
  746. if (a != a) return a; // a is NaN
  747. if ((a == 0.0d) && (b == 0.0d)
  748. && (Double.doubleToLongBits(a) == negativeZeroDoubleBits)) {
  749. return b;
  750. }
  751. return (a >= b) ? a : b;
  752. }
  753. /**
  754. * Returns the smaller of two <code>int</code> values. That is,
  755. * the result the argument closer to the value of
  756. * <code>Integer.MIN_VALUE</code>. If the arguments have the same
  757. * value, the result is that same value.
  758. *
  759. * @param a an argument.
  760. * @param b another argument.
  761. * @return the smaller of <code>a</code> and <code>b</code>.
  762. * @see java.lang.Long#MIN_VALUE
  763. */
  764. public static int min(int a, int b) {
  765. return (a <= b) ? a : b;
  766. }
  767. /**
  768. * Returns the smaller of two <code>long</code> values. That is,
  769. * the result is the argument closer to the value of
  770. * <code>Long.MIN_VALUE</code>. If the arguments have the same
  771. * value, the result is that same value.
  772. *
  773. * @param a an argument.
  774. * @param b another argument.
  775. * @return the smaller of <code>a</code> and <code>b</code>.
  776. * @see java.lang.Long#MIN_VALUE
  777. */
  778. public static long min(long a, long b) {
  779. return (a <= b) ? a : b;
  780. }
  781. /**
  782. * Returns the smaller of two <code>float</code> values. That is,
  783. * the result is the value closer to negative infinity. If the
  784. * arguments have the same value, the result is that same
  785. * value. If either value is NaN, then the result is NaN. Unlike
  786. * the the numerical comparison operators, this method considers
  787. * negative zero to be strictly smaller than positive zero. If
  788. * one argument is positive zero and the other is negative zero,
  789. * the result is negative zero.
  790. *
  791. * @param a an argument.
  792. * @param b another argument.
  793. * @return the smaller of <code>a</code> and <code>b.</code>
  794. */
  795. public static float min(float a, float b) {
  796. if (a != a) return a; // a is NaN
  797. if ((a == 0.0f) && (b == 0.0f)
  798. && (Float.floatToIntBits(b) == negativeZeroFloatBits)) {
  799. return b;
  800. }
  801. return (a <= b) ? a : b;
  802. }
  803. /**
  804. * Returns the smaller of two <code>double</code> values. That
  805. * is, the result is the value closer to negative infinity. If the
  806. * arguments have the same value, the result is that same
  807. * value. If either value is NaN, then the result is NaN. Unlike
  808. * the the numerical comparison operators, this method considers
  809. * negative zero to be strictly smaller than positive zero. If one
  810. * argument is positive zero and the other is negative zero, the
  811. * result is negative zero.
  812. *
  813. * @param a an argument.
  814. * @param b another argument.
  815. * @return the smaller of <code>a</code> and <code>b</code>.
  816. */
  817. public static double min(double a, double b) {
  818. if (a != a) return a; // a is NaN
  819. if ((a == 0.0d) && (b == 0.0d)
  820. && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
  821. return b;
  822. }
  823. return (a <= b) ? a : b;
  824. }
  825. }