1. /*
  2. * @(#)Math.java 1.69 04/06/14
  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 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. *
  14. * <p>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. *
  21. * <p>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. *
  30. * <p>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 distance between the two floating-point values
  36. * bracketing 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 as the computed result;
  48. * otherwise, either of the two floating-point values which bracket
  49. * the exact result may be returned. For exact results large in
  50. * magnitude, one of the endpoints of the bracket may be infinite.
  51. * Besides accuracy at individual arguments, maintaining proper
  52. * relations between the method at different arguments is also
  53. * important. Therefore, most methods with more than 0.5 ulp errors
  54. * are required to be <i>semi-monotonic</i>: whenever the mathematical
  55. * function is non-decreasing, so is the floating-point approximation,
  56. * likewise, whenever the mathematical function is non-increasing, so
  57. * is the floating-point approximation. Not all approximations that
  58. * have 1 ulp accuracy will automatically meet the monotonicity
  59. * requirements.
  60. *
  61. * @author unascribed
  62. * @author Joseph D. Darcy
  63. * @version 1.69, 06/14/04
  64. * @since JDK1.0
  65. */
  66. public final class Math {
  67. /**
  68. * Don't let anyone instantiate this class.
  69. */
  70. private Math() {}
  71. /**
  72. * The <code>double</code> value that is closer than any other to
  73. * <i>e</i>, the base of the natural logarithms.
  74. */
  75. public static final double E = 2.7182818284590452354;
  76. /**
  77. * The <code>double</code> value that is closer than any other to
  78. * <i>pi</i>, the ratio of the circumference of a circle to its
  79. * diameter.
  80. */
  81. public static final double PI = 3.14159265358979323846;
  82. /**
  83. * Returns the trigonometric sine of an angle. Special cases:
  84. * <ul><li>If the argument is NaN or an infinity, then the
  85. * result is NaN.
  86. * <li>If the argument is zero, then the result is a zero with the
  87. * same sign as the argument.</ul>
  88. *
  89. * <p>The computed result must be within 1 ulp of the exact result.
  90. * Results must be semi-monotonic.
  91. *
  92. * @param a an angle, in radians.
  93. * @return the sine of the argument.
  94. */
  95. public static double sin(double a) {
  96. return StrictMath.sin(a); // default impl. delegates to StrictMath
  97. }
  98. /**
  99. * Returns the trigonometric cosine of an angle. Special cases:
  100. * <ul><li>If the argument is NaN or an infinity, then the
  101. * result is NaN.</ul>
  102. *
  103. * <p>The computed result must be within 1 ulp of the exact result.
  104. * Results must be semi-monotonic.
  105. *
  106. * @param a an angle, in radians.
  107. * @return the cosine of the argument.
  108. */
  109. public static double cos(double a) {
  110. return StrictMath.cos(a); // default impl. delegates to StrictMath
  111. }
  112. /**
  113. * Returns the trigonometric tangent of an angle. Special cases:
  114. * <ul><li>If the argument is NaN or an infinity, then the result
  115. * is NaN.
  116. * <li>If the argument is zero, then the result is a zero with the
  117. * same sign as the argument.</ul>
  118. *
  119. * <p>The computed result must be within 1 ulp of the exact result.
  120. * Results must be semi-monotonic.
  121. *
  122. * @param a an angle, in radians.
  123. * @return the tangent of the argument.
  124. */
  125. public static double tan(double a) {
  126. return StrictMath.tan(a); // default impl. delegates to StrictMath
  127. }
  128. /**
  129. * Returns the arc sine of an angle, in the range of -<i>pi</i>/2 through
  130. * <i>pi</i>/2. Special cases:
  131. * <ul><li>If the argument is NaN or its absolute value is greater
  132. * than 1, then the result is NaN.
  133. * <li>If the argument is zero, then the result is a zero with the
  134. * same sign as the argument.</ul>
  135. *
  136. * <p>The computed result must be within 1 ulp of the exact result.
  137. * Results must be semi-monotonic.
  138. *
  139. * @param a the value whose arc sine is to be returned.
  140. * @return the arc sine of the argument.
  141. */
  142. public static double asin(double a) {
  143. return StrictMath.asin(a); // default impl. delegates to StrictMath
  144. }
  145. /**
  146. * Returns the arc cosine of an angle, in the range of 0.0 through
  147. * <i>pi</i>. Special case:
  148. * <ul><li>If the argument is NaN or its absolute value is greater
  149. * than 1, then the result is NaN.</ul>
  150. *
  151. * <p>The computed result must be within 1 ulp of the exact result.
  152. * Results must be semi-monotonic.
  153. *
  154. * @param a the value whose arc cosine is to be returned.
  155. * @return the arc cosine of the argument.
  156. */
  157. public static double acos(double a) {
  158. return StrictMath.acos(a); // default impl. delegates to StrictMath
  159. }
  160. /**
  161. * Returns the arc tangent of an angle, in the range of -<i>pi</i>/2
  162. * through <i>pi</i>/2. Special cases:
  163. * <ul><li>If the argument is NaN, then the result is NaN.
  164. * <li>If the argument is zero, then the result is a zero with the
  165. * same sign as the argument.</ul>
  166. *
  167. * <p>The computed result must be within 1 ulp of the exact result.
  168. * Results must be semi-monotonic.
  169. *
  170. * @param a the value whose arc tangent is to be returned.
  171. * @return the arc tangent of the argument.
  172. */
  173. public static double atan(double a) {
  174. return StrictMath.atan(a); // default impl. delegates to StrictMath
  175. }
  176. /**
  177. * Converts an angle measured in degrees to an approximately
  178. * equivalent angle measured in radians. The conversion from
  179. * degrees to radians is generally inexact.
  180. *
  181. * @param angdeg an angle, in degrees
  182. * @return the measurement of the angle <code>angdeg</code>
  183. * in radians.
  184. * @since 1.2
  185. */
  186. public static double toRadians(double angdeg) {
  187. return angdeg / 180.0 * PI;
  188. }
  189. /**
  190. * Converts an angle measured in radians to an approximately
  191. * equivalent angle measured in degrees. The conversion from
  192. * radians to degrees is generally inexact; users should
  193. * <i>not</i> expect <code>cos(toRadians(90.0))</code> to exactly
  194. * equal <code>0.0</code>.
  195. *
  196. * @param angrad an angle, in radians
  197. * @return the measurement of the angle <code>angrad</code>
  198. * in degrees.
  199. * @since 1.2
  200. */
  201. public static double toDegrees(double angrad) {
  202. return angrad * 180.0 / PI;
  203. }
  204. /**
  205. * Returns Euler's number <i>e</i> raised to the power of a
  206. * <code>double</code> value. Special cases:
  207. * <ul><li>If the argument is NaN, the result is NaN.
  208. * <li>If the argument is positive infinity, then the result is
  209. * positive infinity.
  210. * <li>If the argument is negative infinity, then the result is
  211. * positive zero.</ul>
  212. *
  213. * <p>The computed result must be within 1 ulp of the exact result.
  214. * Results must be semi-monotonic.
  215. *
  216. * @param a the exponent to raise <i>e</i> to.
  217. * @return the value <i>e</i><sup><code>a</code></sup>,
  218. * where <i>e</i> is the base of the natural logarithms.
  219. */
  220. public static double exp(double a) {
  221. return StrictMath.exp(a); // default impl. delegates to StrictMath
  222. }
  223. /**
  224. * Returns the natural logarithm (base <i>e</i>) of a <code>double</code>
  225. * value. Special cases:
  226. * <ul><li>If the argument is NaN or less than zero, then the result
  227. * is NaN.
  228. * <li>If the argument is positive infinity, then the result is
  229. * positive infinity.
  230. * <li>If the argument is positive zero or negative zero, then the
  231. * result is negative infinity.</ul>
  232. *
  233. * <p>The computed result must be within 1 ulp of the exact result.
  234. * Results must be semi-monotonic.
  235. *
  236. * @param a a value
  237. * @return the value ln <code>a</code>, the natural logarithm of
  238. * <code>a</code>.
  239. */
  240. public static double log(double a) {
  241. return StrictMath.log(a); // default impl. delegates to StrictMath
  242. }
  243. /**
  244. * Returns the base 10 logarithm of a <code>double</code> value.
  245. * Special cases:
  246. *
  247. * <ul><li>If the argument is NaN or less than zero, then the result
  248. * is NaN.
  249. * <li>If the argument is positive infinity, then the result is
  250. * positive infinity.
  251. * <li>If the argument is positive zero or negative zero, then the
  252. * result is negative infinity.
  253. * <li> If the argument is equal to 10<sup><i>n</i></sup> for
  254. * integer <i>n</i>, then the result is <i>n</i>.
  255. * </ul>
  256. *
  257. * <p>The computed result must be within 1 ulp of the exact result.
  258. * Results must be semi-monotonic.
  259. *
  260. * @param a a value
  261. * @return the base 10 logarithm of <code>a</code>.
  262. * @since 1.5
  263. */
  264. public static double log10(double a) {
  265. return StrictMath.log10(a); // default impl. delegates to StrictMath
  266. }
  267. /**
  268. * Returns the correctly rounded positive square root of a
  269. * <code>double</code> value.
  270. * Special cases:
  271. * <ul><li>If the argument is NaN or less than zero, then the result
  272. * is NaN.
  273. * <li>If the argument is positive infinity, then the result is positive
  274. * infinity.
  275. * <li>If the argument is positive zero or negative zero, then the
  276. * result is the same as the argument.</ul>
  277. * Otherwise, the result is the <code>double</code> value closest to
  278. * the true mathematical square root of the argument value.
  279. *
  280. * @param a a value.
  281. * @return the positive square root of <code>a</code>.
  282. * If the argument is NaN or less than zero, the result is NaN.
  283. */
  284. public static double sqrt(double a) {
  285. return StrictMath.sqrt(a); // default impl. delegates to StrictMath
  286. // Note that hardware sqrt instructions
  287. // frequently can be directly used by JITs
  288. // and should be much faster than doing
  289. // Math.sqrt in software.
  290. }
  291. /**
  292. * Returns the cube root of a <code>double</code> value. For
  293. * positive finite <code>x</code>, <code>cbrt(-x) ==
  294. * -cbrt(x)</code> that is, the cube root of a negative value is
  295. * the negative of the cube root of that value's magnitude.
  296. *
  297. * Special cases:
  298. *
  299. * <ul>
  300. *
  301. * <li>If the argument is NaN, then the result is NaN.
  302. *
  303. * <li>If the argument is infinite, then the result is an infinity
  304. * with the same sign as the argument.
  305. *
  306. * <li>If the argument is zero, then the result is a zero with the
  307. * same sign as the argument.
  308. *
  309. * </ul>
  310. *
  311. * <p>The computed result must be within 1 ulp of the exact result.
  312. *
  313. * @param a a value.
  314. * @return the cube root of <code>a</code>.
  315. * @since 1.5
  316. */
  317. public static double cbrt(double a) {
  318. return StrictMath.cbrt(a);
  319. }
  320. /**
  321. * Computes the remainder operation on two arguments as prescribed
  322. * by the IEEE 754 standard.
  323. * The remainder value is mathematically equal to
  324. * <code>f1 - f2</code> × <i>n</i>,
  325. * where <i>n</i> is the mathematical integer closest to the exact
  326. * mathematical value of the quotient <code>f1/f2</code>, and if two
  327. * mathematical integers are equally close to <code>f1/f2</code>,
  328. * then <i>n</i> is the integer that is even. If the remainder is
  329. * zero, its sign is the same as the sign of the first argument.
  330. * Special cases:
  331. * <ul><li>If either argument is NaN, or the first argument is infinite,
  332. * or the second argument is positive zero or negative zero, then the
  333. * result is NaN.
  334. * <li>If the first argument is finite and the second argument is
  335. * infinite, then the result is the same as the first argument.</ul>
  336. *
  337. * @param f1 the dividend.
  338. * @param f2 the divisor.
  339. * @return the remainder when <code>f1</code> is divided by
  340. * <code>f2</code>.
  341. */
  342. public static double IEEEremainder(double f1, double f2) {
  343. return StrictMath.IEEEremainder(f1, f2); // delegate to StrictMath
  344. }
  345. /**
  346. * Returns the smallest (closest to negative infinity)
  347. * <code>double</code> value that is greater than or equal to the
  348. * argument and is equal to a mathematical integer. Special cases:
  349. * <ul><li>If the argument value is already equal to a
  350. * mathematical integer, then the result is the same as the
  351. * argument. <li>If the argument is NaN or an infinity or
  352. * positive zero or negative zero, then the result is the same as
  353. * the argument. <li>If the argument value is less than zero but
  354. * greater than -1.0, then the result is negative zero.</ul> Note
  355. * that the value of <code>Math.ceil(x)</code> is exactly the
  356. * value of <code>-Math.floor(-x)</code>.
  357. *
  358. *
  359. * @param a a value.
  360. * @return the smallest (closest to negative infinity)
  361. * floating-point value that is greater than or equal to
  362. * the argument and is equal to a mathematical integer.
  363. */
  364. public static double ceil(double a) {
  365. return StrictMath.ceil(a); // default impl. delegates to StrictMath
  366. }
  367. /**
  368. * Returns the largest (closest to positive infinity)
  369. * <code>double</code> value that is less than or equal to the
  370. * argument and is equal to a mathematical integer. Special cases:
  371. * <ul><li>If the argument value is already equal to a
  372. * mathematical integer, then the result is the same as the
  373. * argument. <li>If the argument is NaN or an infinity or
  374. * positive zero or negative zero, then the result is the same as
  375. * the argument.</ul>
  376. *
  377. * @param a a value.
  378. * @return the largest (closest to positive infinity)
  379. * floating-point value that less than or equal to the argument
  380. * and is equal to a mathematical integer.
  381. */
  382. public static double floor(double a) {
  383. return StrictMath.floor(a); // default impl. delegates to StrictMath
  384. }
  385. /**
  386. * Returns the <code>double</code> value that is closest in value
  387. * to the argument and is equal to a mathematical integer. If two
  388. * <code>double</code> values that are mathematical integers are
  389. * equally close, the result is the integer value that is
  390. * even. Special cases:
  391. * <ul><li>If the argument value is already equal to a mathematical
  392. * integer, then the result is the same as the argument.
  393. * <li>If the argument is NaN or an infinity or positive zero or negative
  394. * zero, then the result is the same as the argument.</ul>
  395. *
  396. * @param a a <code>double</code> value.
  397. * @return the closest floating-point value to <code>a</code> that is
  398. * equal to a mathematical integer.
  399. */
  400. public static double rint(double a) {
  401. return StrictMath.rint(a); // default impl. delegates to StrictMath
  402. }
  403. /**
  404. * Converts rectangular coordinates (<code>x</code>, <code>y</code>)
  405. * to polar (r, <i>theta</i>).
  406. * This method computes the phase <i>theta</i> by computing an arc tangent
  407. * of <code>y/x</code> in the range of -<i>pi</i> to <i>pi</i>. Special
  408. * cases:
  409. * <ul><li>If either argument is NaN, then the result is NaN.
  410. * <li>If the first argument is positive zero and the second argument
  411. * is positive, or the first argument is positive and finite and the
  412. * second argument is positive infinity, then the result is positive
  413. * zero.
  414. * <li>If the first argument is negative zero and the second argument
  415. * is positive, or the first argument is negative and finite and the
  416. * second argument is positive infinity, then the result is negative zero.
  417. * <li>If the first argument is positive zero and the second argument
  418. * is negative, or the first argument is positive and finite and the
  419. * second argument is negative infinity, then the result is the
  420. * <code>double</code> value closest to <i>pi</i>.
  421. * <li>If the first argument is negative zero and the second argument
  422. * is negative, or the first argument is negative and finite and the
  423. * second argument is negative infinity, then the result is the
  424. * <code>double</code> value closest to -<i>pi</i>.
  425. * <li>If the first argument is positive and the second argument is
  426. * positive zero or negative zero, or the first argument is positive
  427. * infinity and the second argument is finite, then the result is the
  428. * <code>double</code> value closest to <i>pi</i>/2.
  429. * <li>If the first argument is negative and the second argument is
  430. * positive zero or negative zero, or the first argument is negative
  431. * infinity and the second argument is finite, then the result is the
  432. * <code>double</code> value closest to -<i>pi</i>/2.
  433. * <li>If both arguments are positive infinity, then the result is the
  434. * <code>double</code> value closest to <i>pi</i>/4.
  435. * <li>If the first argument is positive infinity and the second argument
  436. * is negative infinity, then the result is the <code>double</code>
  437. * value closest to 3*<i>pi</i>/4.
  438. * <li>If the first argument is negative infinity and the second argument
  439. * is positive infinity, then the result is the <code>double</code> value
  440. * closest to -<i>pi</i>/4.
  441. * <li>If both arguments are negative infinity, then the result is the
  442. * <code>double</code> value closest to -3*<i>pi</i>/4.</ul>
  443. *
  444. * <p>The computed result must be within 2 ulps of the exact result.
  445. * Results must be semi-monotonic.
  446. *
  447. * @param y the ordinate coordinate
  448. * @param x the abscissa coordinate
  449. * @return the <i>theta</i> component of the point
  450. * (<i>r</i>, <i>theta</i>)
  451. * in polar coordinates that corresponds to the point
  452. * (<i>x</i>, <i>y</i>) in Cartesian coordinates.
  453. */
  454. public static double atan2(double y, double x) {
  455. return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
  456. }
  457. /**
  458. * Returns the value of the first argument raised to the power of the
  459. * second argument. Special cases:
  460. *
  461. * <ul><li>If the second argument is positive or negative zero, then the
  462. * result is 1.0.
  463. * <li>If the second argument is 1.0, then the result is the same as the
  464. * first argument.
  465. * <li>If the second argument is NaN, then the result is NaN.
  466. * <li>If the first argument is NaN and the second argument is nonzero,
  467. * then the result is NaN.
  468. *
  469. * <li>If
  470. * <ul>
  471. * <li>the absolute value of the first argument is greater than 1
  472. * and the second argument is positive infinity, or
  473. * <li>the absolute value of the first argument is less than 1 and
  474. * the second argument is negative infinity,
  475. * </ul>
  476. * then the result is positive infinity.
  477. *
  478. * <li>If
  479. * <ul>
  480. * <li>the absolute value of the first argument is greater than 1 and
  481. * the second argument is negative infinity, or
  482. * <li>the absolute value of the
  483. * first argument is less than 1 and the second argument is positive
  484. * infinity,
  485. * </ul>
  486. * then the result is positive zero.
  487. *
  488. * <li>If the absolute value of the first argument equals 1 and the
  489. * second argument is infinite, then the result is NaN.
  490. *
  491. * <li>If
  492. * <ul>
  493. * <li>the first argument is positive zero and the second argument
  494. * is greater than zero, or
  495. * <li>the first argument is positive infinity and the second
  496. * argument is less than zero,
  497. * </ul>
  498. * then the result is positive zero.
  499. *
  500. * <li>If
  501. * <ul>
  502. * <li>the first argument is positive zero and the second argument
  503. * is less than zero, or
  504. * <li>the first argument is positive infinity and the second
  505. * argument is greater than zero,
  506. * </ul>
  507. * then the result is positive infinity.
  508. *
  509. * <li>If
  510. * <ul>
  511. * <li>the first argument is negative zero and the second argument
  512. * is greater than zero but not a finite odd integer, or
  513. * <li>the first argument is negative infinity and the second
  514. * argument is less than zero but not a finite odd integer,
  515. * </ul>
  516. * then the result is positive zero.
  517. *
  518. * <li>If
  519. * <ul>
  520. * <li>the first argument is negative zero and the second argument
  521. * is a positive finite odd integer, or
  522. * <li>the first argument is negative infinity and the second
  523. * argument is a negative finite odd integer,
  524. * </ul>
  525. * then the result is negative zero.
  526. *
  527. * <li>If
  528. * <ul>
  529. * <li>the first argument is negative zero and the second argument
  530. * is less than zero but not a finite odd integer, or
  531. * <li>the first argument is negative infinity and the second
  532. * argument is greater than zero but not a finite odd integer,
  533. * </ul>
  534. * then the result is positive infinity.
  535. *
  536. * <li>If
  537. * <ul>
  538. * <li>the first argument is negative zero and the second argument
  539. * is a negative finite odd integer, or
  540. * <li>the first argument is negative infinity and the second
  541. * argument is a positive finite odd integer,
  542. * </ul>
  543. * then the result is negative infinity.
  544. *
  545. * <li>If the first argument is finite and less than zero
  546. * <ul>
  547. * <li> if the second argument is a finite even integer, the
  548. * result is equal to the result of raising the absolute value of
  549. * the first argument to the power of the second argument
  550. *
  551. * <li>if the second argument is a finite odd integer, the result
  552. * is equal to the negative of the result of raising the absolute
  553. * value of the first argument to the power of the second
  554. * argument
  555. *
  556. * <li>if the second argument is finite and not an integer, then
  557. * the result is NaN.
  558. * </ul>
  559. *
  560. * <li>If both arguments are integers, then the result is exactly equal
  561. * to the mathematical result of raising the first argument to the power
  562. * of the second argument if that result can in fact be represented
  563. * exactly as a <code>double</code> value.</ul>
  564. *
  565. * <p>(In the foregoing descriptions, a floating-point value is
  566. * considered to be an integer if and only if it is finite and a
  567. * fixed point of the method {@link #ceil <tt>ceil</tt>} or,
  568. * equivalently, a fixed point of the method {@link #floor
  569. * <tt>floor</tt>}. A value is a fixed point of a one-argument
  570. * method if and only if the result of applying the method to the
  571. * value is equal to the value.)
  572. *
  573. * <p>The computed result must be within 1 ulp of the exact result.
  574. * Results must be semi-monotonic.
  575. *
  576. * @param a the base.
  577. * @param b the exponent.
  578. * @return the value <code>a<sup>b</sup></code>.
  579. */
  580. public static double pow(double a, double b) {
  581. return StrictMath.pow(a, b); // default impl. delegates to StrictMath
  582. }
  583. /**
  584. * Returns the closest <code>int</code> to the argument. The
  585. * result is rounded to an integer by adding 1/2, taking the
  586. * floor of the result, and casting the result to type <code>int</code>.
  587. * In other words, the result is equal to the value of the expression:
  588. * <p><pre>(int)Math.floor(a + 0.5f)</pre>
  589. * <p>
  590. * Special cases:
  591. * <ul><li>If the argument is NaN, the result is 0.
  592. * <li>If the argument is negative infinity or any value less than or
  593. * equal to the value of <code>Integer.MIN_VALUE</code>, the result is
  594. * equal to the value of <code>Integer.MIN_VALUE</code>.
  595. * <li>If the argument is positive infinity or any value greater than or
  596. * equal to the value of <code>Integer.MAX_VALUE</code>, the result is
  597. * equal to the value of <code>Integer.MAX_VALUE</code>.</ul>
  598. *
  599. * @param a a floating-point value to be rounded to an integer.
  600. * @return the value of the argument rounded to the nearest
  601. * <code>int</code> value.
  602. * @see java.lang.Integer#MAX_VALUE
  603. * @see java.lang.Integer#MIN_VALUE
  604. */
  605. public static int round(float a) {
  606. return (int)floor(a + 0.5f);
  607. }
  608. /**
  609. * Returns the closest <code>long</code> to the argument. The result
  610. * is rounded to an integer by adding 1/2, taking the floor of the
  611. * result, and casting the result to type <code>long</code>. In other
  612. * words, the result is equal to the value of the expression:
  613. * <p><pre>(long)Math.floor(a + 0.5d)</pre>
  614. * <p>
  615. * Special cases:
  616. * <ul><li>If the argument is NaN, the result is 0.
  617. * <li>If the argument is negative infinity or any value less than or
  618. * equal to the value of <code>Long.MIN_VALUE</code>, the result is
  619. * equal to the value of <code>Long.MIN_VALUE</code>.
  620. * <li>If the argument is positive infinity or any value greater than or
  621. * equal to the value of <code>Long.MAX_VALUE</code>, the result is
  622. * equal to the value of <code>Long.MAX_VALUE</code>.</ul>
  623. *
  624. * @param a a floating-point value to be rounded to a
  625. * <code>long</code>.
  626. * @return the value of the argument rounded to the nearest
  627. * <code>long</code> value.
  628. * @see java.lang.Long#MAX_VALUE
  629. * @see java.lang.Long#MIN_VALUE
  630. */
  631. public static long round(double a) {
  632. return (long)floor(a + 0.5d);
  633. }
  634. private static Random randomNumberGenerator;
  635. private static synchronized void initRNG() {
  636. if (randomNumberGenerator == null)
  637. randomNumberGenerator = new Random();
  638. }
  639. /**
  640. * Returns a <code>double</code> value with a positive sign, greater
  641. * than or equal to <code>0.0</code> and less than <code>1.0</code>.
  642. * Returned values are chosen pseudorandomly with (approximately)
  643. * uniform distribution from that range.
  644. *
  645. * <p>When this method is first called, it creates a single new
  646. * pseudorandom-number generator, exactly as if by the expression
  647. * <blockquote><pre>new java.util.Random</pre></blockquote> This
  648. * new pseudorandom-number generator is used thereafter for all
  649. * calls to this method and is used nowhere else.
  650. *
  651. * <p>This method is properly synchronized to allow correct use by
  652. * more than one thread. However, if many threads need to generate
  653. * pseudorandom numbers at a great rate, it may reduce contention
  654. * for each thread to have its own pseudorandom-number generator.
  655. *
  656. * @return a pseudorandom <code>double</code> greater than or equal
  657. * to <code>0.0</code> and less than <code>1.0</code>.
  658. * @see java.util.Random#nextDouble()
  659. */
  660. public static double random() {
  661. if (randomNumberGenerator == null) initRNG();
  662. return randomNumberGenerator.nextDouble();
  663. }
  664. /**
  665. * Returns the absolute value of an <code>int</code> value.
  666. * If the argument is not negative, the argument is returned.
  667. * If the argument is negative, the negation of the argument is returned.
  668. *
  669. * <p>Note that if the argument is equal to the value of
  670. * <code>Integer.MIN_VALUE</code>, the most negative representable
  671. * <code>int</code> value, the result is that same value, which is
  672. * negative.
  673. *
  674. * @param a the argument whose absolute value is to be determined
  675. * @return the absolute value of the argument.
  676. * @see java.lang.Integer#MIN_VALUE
  677. */
  678. public static int abs(int a) {
  679. return (a < 0) ? -a : a;
  680. }
  681. /**
  682. * Returns the absolute value of a <code>long</code> value.
  683. * If the argument is not negative, the argument is returned.
  684. * If the argument is negative, the negation of the argument is returned.
  685. *
  686. * <p>Note that if the argument is equal to the value of
  687. * <code>Long.MIN_VALUE</code>, the most negative representable
  688. * <code>long</code> value, the result is that same value, which
  689. * is negative.
  690. *
  691. * @param a the argument whose absolute value is to be determined
  692. * @return the absolute value of the argument.
  693. * @see java.lang.Long#MIN_VALUE
  694. */
  695. public static long abs(long a) {
  696. return (a < 0) ? -a : a;
  697. }
  698. /**
  699. * Returns the absolute value of a <code>float</code> value.
  700. * If the argument is not negative, the argument is returned.
  701. * If the argument is negative, the negation of the argument is returned.
  702. * Special cases:
  703. * <ul><li>If the argument is positive zero or negative zero, the
  704. * result is positive zero.
  705. * <li>If the argument is infinite, the result is positive infinity.
  706. * <li>If the argument is NaN, the result is NaN.</ul>
  707. * In other words, the result is the same as the value of the expression:
  708. * <p><pre>Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))</pre>
  709. *
  710. * @param a the argument whose absolute value is to be determined
  711. * @return the absolute value of the argument.
  712. */
  713. public static float abs(float a) {
  714. return (a <= 0.0F) ? 0.0F - a : a;
  715. }
  716. /**
  717. * Returns the absolute value of a <code>double</code> value.
  718. * If the argument is not negative, the argument is returned.
  719. * If the argument is negative, the negation of the argument is returned.
  720. * Special cases:
  721. * <ul><li>If the argument is positive zero or negative zero, the result
  722. * is positive zero.
  723. * <li>If the argument is infinite, the result is positive infinity.
  724. * <li>If the argument is NaN, the result is NaN.</ul>
  725. * In other words, the result is the same as the value of the expression:
  726. * <p><code>Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)</code>
  727. *
  728. * @param a the argument whose absolute value is to be determined
  729. * @return the absolute value of the argument.
  730. */
  731. public static double abs(double a) {
  732. return (a <= 0.0D) ? 0.0D - a : a;
  733. }
  734. /**
  735. * Returns the greater of two <code>int</code> values. That is, the
  736. * result is the argument closer to the value of
  737. * <code>Integer.MAX_VALUE</code>. If the arguments have the same value,
  738. * the result is that same value.
  739. *
  740. * @param a an argument.
  741. * @param b another argument.
  742. * @return the larger of <code>a</code> and <code>b</code>.
  743. * @see java.lang.Long#MAX_VALUE
  744. */
  745. public static int max(int a, int b) {
  746. return (a >= b) ? a : b;
  747. }
  748. /**
  749. * Returns the greater of two <code>long</code> values. That is, the
  750. * result is the argument closer to the value of
  751. * <code>Long.MAX_VALUE</code>. If the arguments have the same value,
  752. * the result is that same value.
  753. *
  754. * @param a an argument.
  755. * @param b another argument.
  756. * @return the larger of <code>a</code> and <code>b</code>.
  757. * @see java.lang.Long#MAX_VALUE
  758. */
  759. public static long max(long a, long b) {
  760. return (a >= b) ? a : b;
  761. }
  762. private static long negativeZeroFloatBits = Float.floatToIntBits(-0.0f);
  763. private static long negativeZeroDoubleBits = Double.doubleToLongBits(-0.0d);
  764. /**
  765. * Returns the greater of two <code>float</code> values. That is,
  766. * the result is the argument closer to positive infinity. If the
  767. * arguments have the same value, the result is that same
  768. * value. If either value is NaN, then the result is NaN. Unlike
  769. * the numerical comparison operators, this method considers
  770. * negative zero to be strictly smaller than positive zero. If one
  771. * argument is positive zero and the other negative zero, the
  772. * result is positive zero.
  773. *
  774. * @param a an argument.
  775. * @param b another argument.
  776. * @return the larger of <code>a</code> and <code>b</code>.
  777. */
  778. public static float max(float a, float b) {
  779. if (a != a) return a; // a is NaN
  780. if ((a == 0.0f) && (b == 0.0f)
  781. && (Float.floatToIntBits(a) == negativeZeroFloatBits)) {
  782. return b;
  783. }
  784. return (a >= b) ? a : b;
  785. }
  786. /**
  787. * Returns the greater of two <code>double</code> values. That
  788. * is, the result is the argument closer to positive infinity. If
  789. * the arguments have the same value, the result is that same
  790. * value. If either value is NaN, then the result is NaN. Unlike
  791. * the numerical comparison operators, this method considers
  792. * negative zero to be strictly smaller than positive zero. If one
  793. * argument is positive zero and the other negative zero, the
  794. * result is positive zero.
  795. *
  796. * @param a an argument.
  797. * @param b another argument.
  798. * @return the larger of <code>a</code> and <code>b</code>.
  799. */
  800. public static double max(double a, double b) {
  801. if (a != a) return a; // a is NaN
  802. if ((a == 0.0d) && (b == 0.0d)
  803. && (Double.doubleToLongBits(a) == negativeZeroDoubleBits)) {
  804. return b;
  805. }
  806. return (a >= b) ? a : b;
  807. }
  808. /**
  809. * Returns the smaller of two <code>int</code> values. That is,
  810. * the result the argument closer to the value of
  811. * <code>Integer.MIN_VALUE</code>. If the arguments have the same
  812. * value, the result is that same value.
  813. *
  814. * @param a an argument.
  815. * @param b another argument.
  816. * @return the smaller of <code>a</code> and <code>b</code>.
  817. * @see java.lang.Long#MIN_VALUE
  818. */
  819. public static int min(int a, int b) {
  820. return (a <= b) ? a : b;
  821. }
  822. /**
  823. * Returns the smaller of two <code>long</code> values. That is,
  824. * the result is the argument closer to the value of
  825. * <code>Long.MIN_VALUE</code>. If the arguments have the same
  826. * value, the result is that same value.
  827. *
  828. * @param a an argument.
  829. * @param b another argument.
  830. * @return the smaller of <code>a</code> and <code>b</code>.
  831. * @see java.lang.Long#MIN_VALUE
  832. */
  833. public static long min(long a, long b) {
  834. return (a <= b) ? a : b;
  835. }
  836. /**
  837. * Returns the smaller of two <code>float</code> values. That is,
  838. * the result is the value closer to negative infinity. If the
  839. * arguments have the same value, the result is that same
  840. * value. If either value is NaN, then the result is NaN. Unlike
  841. * the numerical comparison operators, this method considers
  842. * negative zero to be strictly smaller than positive zero. If
  843. * one argument is positive zero and the other is negative zero,
  844. * the result is negative zero.
  845. *
  846. * @param a an argument.
  847. * @param b another argument.
  848. * @return the smaller of <code>a</code> and <code>b.</code>
  849. */
  850. public static float min(float a, float b) {
  851. if (a != a) return a; // a is NaN
  852. if ((a == 0.0f) && (b == 0.0f)
  853. && (Float.floatToIntBits(b) == negativeZeroFloatBits)) {
  854. return b;
  855. }
  856. return (a <= b) ? a : b;
  857. }
  858. /**
  859. * Returns the smaller of two <code>double</code> values. That
  860. * is, the result is the value closer to negative infinity. If the
  861. * arguments have the same value, the result is that same
  862. * value. If either value is NaN, then the result is NaN. Unlike
  863. * the numerical comparison operators, this method considers
  864. * negative zero to be strictly smaller than positive zero. If one
  865. * argument is positive zero and the other is negative zero, the
  866. * result is negative zero.
  867. *
  868. * @param a an argument.
  869. * @param b another argument.
  870. * @return the smaller of <code>a</code> and <code>b</code>.
  871. */
  872. public static double min(double a, double b) {
  873. if (a != a) return a; // a is NaN
  874. if ((a == 0.0d) && (b == 0.0d)
  875. && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
  876. return b;
  877. }
  878. return (a <= b) ? a : b;
  879. }
  880. /**
  881. * Returns the size of an ulp of the argument. An ulp of a
  882. * <code>double</code> value is the positive distance between this
  883. * floating-point value and the <code>double</code> value next
  884. * larger in magnitude. Note that for non-NaN <i>x</i>,
  885. * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
  886. *
  887. * <p>Special Cases:
  888. * <ul>
  889. * <li> If the argument is NaN, then the result is NaN.
  890. * <li> If the argument is positive or negative infinity, then the
  891. * result is positive infinity.
  892. * <li> If the argument is positive or negative zero, then the result is
  893. * <code>Double.MIN_VALUE</code>.
  894. * <li> If the argument is ±<code>Double.MAX_VALUE</code>, then
  895. * the result is equal to 2<sup>971</sup>.
  896. * </ul>
  897. *
  898. * @param d the floating-point value whose ulp is to be returned
  899. * @return the size of an ulp of the argument
  900. * @author Joseph D. Darcy
  901. * @since 1.5
  902. */
  903. public static double ulp(double d) {
  904. return sun.misc.FpUtils.ulp(d);
  905. }
  906. /**
  907. * Returns the size of an ulp of the argument. An ulp of a
  908. * <code>float</code> value is the positive distance between this
  909. * floating-point value and the <code>float</code> value next
  910. * larger in magnitude. Note that for non-NaN <i>x</i>,
  911. * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
  912. *
  913. * <p>Special Cases:
  914. * <ul>
  915. * <li> If the argument is NaN, then the result is NaN.
  916. * <li> If the argument is positive or negative infinity, then the
  917. * result is positive infinity.
  918. * <li> If the argument is positive or negative zero, then the result is
  919. * <code>Float.MIN_VALUE</code>.
  920. * <li> If the argument is ±<code>Float.MAX_VALUE</code>, then
  921. * the result is equal to 2<sup>104</sup>.
  922. * </ul>
  923. *
  924. * @param f the floating-point value whose ulp is to be returned
  925. * @return the size of an ulp of the argument
  926. * @author Joseph D. Darcy
  927. * @since 1.5
  928. */
  929. public static float ulp(float f) {
  930. return sun.misc.FpUtils.ulp(f);
  931. }
  932. /**
  933. * Returns the signum function of the argument; zero if the argument
  934. * is zero, 1.0 if the argument is greater than zero, -1.0 if the
  935. * argument is less than zero.
  936. *
  937. * <p>Special Cases:
  938. * <ul>
  939. * <li> If the argument is NaN, then the result is NaN.
  940. * <li> If the argument is positive zero or negative zero, then the
  941. * result is the same as the argument.
  942. * </ul>
  943. *
  944. * @param d the floating-point value whose signum is to be returned
  945. * @return the signum function of the argument
  946. * @author Joseph D. Darcy
  947. * @since 1.5
  948. */
  949. public static double signum(double d) {
  950. return sun.misc.FpUtils.signum(d);
  951. }
  952. /**
  953. * Returns the signum function of the argument; zero if the argument
  954. * is zero, 1.0f if the argument is greater than zero, -1.0f if the
  955. * argument is less than zero.
  956. *
  957. * <p>Special Cases:
  958. * <ul>
  959. * <li> If the argument is NaN, then the result is NaN.
  960. * <li> If the argument is positive zero or negative zero, then the
  961. * result is the same as the argument.
  962. * </ul>
  963. *
  964. * @param f the floating-point value whose signum is to be returned
  965. * @return the signum function of the argument
  966. * @author Joseph D. Darcy
  967. * @since 1.5
  968. */
  969. public static float signum(float f) {
  970. return sun.misc.FpUtils.signum(f);
  971. }
  972. /**
  973. * Returns the hyperbolic sine of a <code>double</code> value.
  974. * The hyperbolic sine of <i>x</i> is defined to be
  975. * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2
  976. * where <i>e</i> is {@linkplain Math#E Euler's number}.
  977. *
  978. * <p>Special cases:
  979. * <ul>
  980. *
  981. * <li>If the argument is NaN, then the result is NaN.
  982. *
  983. * <li>If the argument is infinite, then the result is an infinity
  984. * with the same sign as the argument.
  985. *
  986. * <li>If the argument is zero, then the result is a zero with the
  987. * same sign as the argument.
  988. *
  989. * </ul>
  990. *
  991. * <p>The computed result must be within 2.5 ulps of the exact result.
  992. *
  993. * @param x The number whose hyperbolic sine is to be returned.
  994. * @return The hyperbolic sine of <code>x</code>.
  995. * @since 1.5
  996. */
  997. public static double sinh(double x) {
  998. return StrictMath.sinh(x);
  999. }
  1000. /**
  1001. * Returns the hyperbolic cosine of a <code>double</code> value.
  1002. * The hyperbolic cosine of <i>x</i> is defined to be
  1003. * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2
  1004. * where <i>e</i> is {@linkplain Math#E Euler's number}.
  1005. *
  1006. * <p>Special cases:
  1007. * <ul>
  1008. *
  1009. * <li>If the argument is NaN, then the result is NaN.
  1010. *
  1011. * <li>If the argument is infinite, then the result is positive
  1012. * infinity.
  1013. *
  1014. * <li>If the argument is zero, then the result is <code>1.0</code>.
  1015. *
  1016. * </ul>
  1017. *
  1018. * <p>The computed result must be within 2.5 ulps of the exact result.
  1019. *
  1020. * @param x The number whose hyperbolic cosine is to be returned.
  1021. * @return The hyperbolic cosine of <code>x</code>.
  1022. * @since 1.5
  1023. */
  1024. public static double cosh(double x) {
  1025. return StrictMath.cosh(x);
  1026. }
  1027. /**
  1028. * Returns the hyperbolic tangent of a <code>double</code> value.
  1029. * The hyperbolic tangent of <i>x</i> is defined to be
  1030. * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>),
  1031. * in other words, {@linkplain Math#sinh
  1032. * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note
  1033. * that the absolute value of the exact tanh is always less than
  1034. * 1.
  1035. *
  1036. * <p>Special cases:
  1037. * <ul>
  1038. *
  1039. * <li>If the argument is NaN, then the result is NaN.
  1040. *
  1041. * <li>If the argument is zero, then the result is a zero with the
  1042. * same sign as the argument.
  1043. *
  1044. * <li>If the argument is positive infinity, then the result is
  1045. * <code>+1.0</code>.
  1046. *
  1047. * <li>If the argument is negative infinity, then the result is
  1048. * <code>-1.0</code>.
  1049. *
  1050. * </ul>
  1051. *
  1052. * <p>The computed result must be within 2.5 ulps of the exact result.
  1053. * The result of <code>tanh</code> for any finite input must have
  1054. * an absolute value less than or equal to 1. Note that once the
  1055. * exact result of tanh is within 1/2 of an ulp of the limit value
  1056. * of ±1, correctly signed ±<code>1.0</code> should
  1057. * be returned.
  1058. *
  1059. * @param x The number whose hyperbolic tangent is to be returned.
  1060. * @return The hyperbolic tangent of <code>x</code>.
  1061. * @since 1.5
  1062. */
  1063. public static double tanh(double x) {
  1064. return StrictMath.tanh(x);
  1065. }
  1066. /**
  1067. * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
  1068. * without intermediate overflow or underflow.
  1069. *
  1070. * <p>Special cases:
  1071. * <ul>
  1072. *
  1073. * <li> If either argument is infinite, then the result
  1074. * is positive infinity.
  1075. *
  1076. * <li> If either argument is NaN and neither argument is infinite,
  1077. * then the result is NaN.
  1078. *
  1079. * </ul>
  1080. *
  1081. * <p>The computed result must be within 1 ulp of the exact
  1082. * result. If one parameter is held constant, the results must be
  1083. * semi-monotonic in the other parameter.
  1084. *
  1085. * @param x a value
  1086. * @param y a value
  1087. * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
  1088. * without intermediate overflow or underflow
  1089. * @since 1.5
  1090. */
  1091. public static double hypot(double x, double y) {
  1092. return StrictMath.hypot(x, y);
  1093. }
  1094. /**
  1095. * Returns <i>e</i><sup>x</sup> -1. Note that for values of
  1096. * <i>x</i> near 0, the exact sum of
  1097. * <code>expm1(x)</code> + 1 is much closer to the true
  1098. * result of <i>e</i><sup>x</sup> than <code>exp(x)</code>.
  1099. *
  1100. * <p>Special cases:
  1101. * <ul>
  1102. * <li>If the argument is NaN, the result is NaN.
  1103. *
  1104. * <li>If the argument is positive infinity, then the result is
  1105. * positive infinity.
  1106. *
  1107. * <li>If the argument is negative infinity, then the result is
  1108. * -1.0.
  1109. *
  1110. * <li>If the argument is zero, then the result is a zero with the
  1111. * same sign as the argument.
  1112. *
  1113. * </ul>
  1114. *
  1115. * <p>The computed result must be within 1 ulp of the exact result.
  1116. * Results must be semi-monotonic. The result of
  1117. * <code>expm1</code> for any finite input must be greater than or
  1118. * equal to <code>-1.0</code>. Note that once the exact result of
  1119. * <i>e</i><sup><code>x</code></sup> - 1 is within 1/2
  1120. * ulp of the limit value -1, <code>-1.0</code> should be
  1121. * returned.
  1122. *
  1123. * @param x the exponent to raise <i>e</i> to in the computation of
  1124. * <i>e</i><sup><code>x</code></sup> -1.
  1125. * @return the value <i>e</i><sup><code>x</code></sup> - 1.
  1126. */
  1127. public static double expm1(double x) {
  1128. return StrictMath.expm1(x);
  1129. }
  1130. /**
  1131. * Returns the natural logarithm of the sum of the argument and 1.
  1132. * Note that for small values <code>x</code>, the result of
  1133. * <code>log1p(x)</code> is much closer to the true result of ln(1
  1134. * + <code>x</code>) than the floating-point evaluation of
  1135. * <code>log(1.0+x)</code>.
  1136. *
  1137. * <p>Special cases:
  1138. *
  1139. * <ul>
  1140. *
  1141. * <li>If the argument is NaN or less than -1, then the result is
  1142. * NaN.
  1143. *
  1144. * <li>If the argument is positive infinity, then the result is
  1145. * positive infinity.
  1146. *
  1147. * <li>If the argument is negative one, then the result is
  1148. * negative infinity.
  1149. *
  1150. * <li>If the argument is zero, then the result is a zero with the
  1151. * same sign as the argument.
  1152. *
  1153. * </ul>
  1154. *
  1155. * <p>The computed result must be within 1 ulp of the exact result.
  1156. * Results must be semi-monotonic.
  1157. *
  1158. * @param x a value
  1159. * @return the value ln(<code>x</code> + 1), the natural
  1160. * log of <code>x</code> + 1
  1161. */
  1162. public static double log1p(double x) {
  1163. return StrictMath.log1p(x);
  1164. }
  1165. }