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