Math.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /* java.lang.Math -- common mathematical functions, native allowed
  2. Copyright (C) 1998, 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.lang;
  32. import gnu.classpath.Configuration;
  33. import java.util.Random;
  34. /**
  35. * Helper class containing useful mathematical functions and constants.
  36. * <P>
  37. *
  38. * Note that angles are specified in radians. Conversion functions are
  39. * provided for your convenience.
  40. *
  41. * @author Paul Fisher
  42. * @author John Keiser
  43. * @author Eric Blake (ebb9@email.byu.edu)
  44. * @since 1.0
  45. */
  46. public final class Math
  47. {
  48. /**
  49. * Math is non-instantiable
  50. */
  51. private Math()
  52. {
  53. }
  54. static
  55. {
  56. if (Configuration.INIT_LOAD_LIBRARY)
  57. {
  58. System.loadLibrary("javalang");
  59. }
  60. }
  61. /**
  62. * A random number generator, initialized on first use.
  63. */
  64. private static Random rand;
  65. /**
  66. * The most accurate approximation to the mathematical constant <em>e</em>:
  67. * <code>2.718281828459045</code>. Used in natural log and exp.
  68. *
  69. * @see #log(double)
  70. * @see #exp(double)
  71. */
  72. public static final double E = 2.718281828459045;
  73. /**
  74. * The most accurate approximation to the mathematical constant <em>pi</em>:
  75. * <code>3.141592653589793</code>. This is the ratio of a circle's diameter
  76. * to its circumference.
  77. */
  78. public static final double PI = 3.141592653589793;
  79. /**
  80. * Take the absolute value of the argument.
  81. * (Absolute value means make it positive.)
  82. * <P>
  83. *
  84. * Note that the the largest negative value (Integer.MIN_VALUE) cannot
  85. * be made positive. In this case, because of the rules of negation in
  86. * a computer, MIN_VALUE is what will be returned.
  87. * This is a <em>negative</em> value. You have been warned.
  88. *
  89. * @param i the number to take the absolute value of
  90. * @return the absolute value
  91. * @see Integer#MIN_VALUE
  92. */
  93. public static int abs(int i)
  94. {
  95. return (i < 0) ? -i : i;
  96. }
  97. /**
  98. * Take the absolute value of the argument.
  99. * (Absolute value means make it positive.)
  100. * <P>
  101. *
  102. * Note that the the largest negative value (Long.MIN_VALUE) cannot
  103. * be made positive. In this case, because of the rules of negation in
  104. * a computer, MIN_VALUE is what will be returned.
  105. * This is a <em>negative</em> value. You have been warned.
  106. *
  107. * @param l the number to take the absolute value of
  108. * @return the absolute value
  109. * @see Long#MIN_VALUE
  110. */
  111. public static long abs(long l)
  112. {
  113. return (l < 0) ? -l : l;
  114. }
  115. /**
  116. * Take the absolute value of the argument.
  117. * (Absolute value means make it positive.)
  118. * <P>
  119. *
  120. * This is equivalent, but faster than, calling
  121. * <code>Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))</code>.
  122. *
  123. * @param f the number to take the absolute value of
  124. * @return the absolute value
  125. */
  126. public static float abs(float f)
  127. {
  128. return (f <= 0) ? 0 - f : f;
  129. }
  130. /**
  131. * Take the absolute value of the argument.
  132. * (Absolute value means make it positive.)
  133. *
  134. * This is equivalent, but faster than, calling
  135. * <code>Double.longBitsToDouble(Double.doubleToLongBits(a)
  136. * &lt;&lt; 1) &gt;&gt;&gt; 1);</code>.
  137. *
  138. * @param d the number to take the absolute value of
  139. * @return the absolute value
  140. */
  141. public static double abs(double d)
  142. {
  143. return (d <= 0) ? 0 - d : d;
  144. }
  145. /**
  146. * Return whichever argument is smaller.
  147. *
  148. * @param a the first number
  149. * @param b a second number
  150. * @return the smaller of the two numbers
  151. */
  152. public static int min(int a, int b)
  153. {
  154. return (a < b) ? a : b;
  155. }
  156. /**
  157. * Return whichever argument is smaller.
  158. *
  159. * @param a the first number
  160. * @param b a second number
  161. * @return the smaller of the two numbers
  162. */
  163. public static long min(long a, long b)
  164. {
  165. return (a < b) ? a : b;
  166. }
  167. /**
  168. * Return whichever argument is smaller. If either argument is NaN, the
  169. * result is NaN, and when comparing 0 and -0, -0 is always smaller.
  170. *
  171. * @param a the first number
  172. * @param b a second number
  173. * @return the smaller of the two numbers
  174. */
  175. public static float min(float a, float b)
  176. {
  177. // this check for NaN, from JLS 15.21.1, saves a method call
  178. if (a != a)
  179. return a;
  180. // no need to check if b is NaN; < will work correctly
  181. // recall that -0.0 == 0.0, but [+-]0.0 - [+-]0.0 behaves special
  182. if (a == 0 && b == 0)
  183. return -(-a - b);
  184. return (a < b) ? a : b;
  185. }
  186. /**
  187. * Return whichever argument is smaller. If either argument is NaN, the
  188. * result is NaN, and when comparing 0 and -0, -0 is always smaller.
  189. *
  190. * @param a the first number
  191. * @param b a second number
  192. * @return the smaller of the two numbers
  193. */
  194. public static double min(double a, double b)
  195. {
  196. // this check for NaN, from JLS 15.21.1, saves a method call
  197. if (a != a)
  198. return a;
  199. // no need to check if b is NaN; < will work correctly
  200. // recall that -0.0 == 0.0, but [+-]0.0 - [+-]0.0 behaves special
  201. if (a == 0 && b == 0)
  202. return -(-a - b);
  203. return (a < b) ? a : b;
  204. }
  205. /**
  206. * Return whichever argument is larger.
  207. *
  208. * @param a the first number
  209. * @param b a second number
  210. * @return the larger of the two numbers
  211. */
  212. public static int max(int a, int b)
  213. {
  214. return (a > b) ? a : b;
  215. }
  216. /**
  217. * Return whichever argument is larger.
  218. *
  219. * @param a the first number
  220. * @param b a second number
  221. * @return the larger of the two numbers
  222. */
  223. public static long max(long a, long b)
  224. {
  225. return (a > b) ? a : b;
  226. }
  227. /**
  228. * Return whichever argument is larger. If either argument is NaN, the
  229. * result is NaN, and when comparing 0 and -0, 0 is always larger.
  230. *
  231. * @param a the first number
  232. * @param b a second number
  233. * @return the larger of the two numbers
  234. */
  235. public static float max(float a, float b)
  236. {
  237. // this check for NaN, from JLS 15.21.1, saves a method call
  238. if (a != a)
  239. return a;
  240. // no need to check if b is NaN; > will work correctly
  241. // recall that -0.0 == 0.0, but [+-]0.0 - [+-]0.0 behaves special
  242. if (a == 0 && b == 0)
  243. return a - -b;
  244. return (a > b) ? a : b;
  245. }
  246. /**
  247. * Return whichever argument is larger. If either argument is NaN, the
  248. * result is NaN, and when comparing 0 and -0, 0 is always larger.
  249. *
  250. * @param a the first number
  251. * @param b a second number
  252. * @return the larger of the two numbers
  253. */
  254. public static double max(double a, double b)
  255. {
  256. // this check for NaN, from JLS 15.21.1, saves a method call
  257. if (a != a)
  258. return a;
  259. // no need to check if b is NaN; > will work correctly
  260. // recall that -0.0 == 0.0, but [+-]0.0 - [+-]0.0 behaves special
  261. if (a == 0 && b == 0)
  262. return a - -b;
  263. return (a > b) ? a : b;
  264. }
  265. /**
  266. * The trigonometric function <em>sin</em>. The sine of NaN or infinity is
  267. * NaN, and the sine of 0 retains its sign. This is accurate within 1 ulp,
  268. * and is semi-monotonic.
  269. *
  270. * @param a the angle (in radians)
  271. * @return sin(a)
  272. */
  273. public static native double sin(double a);
  274. /**
  275. * The trigonometric function <em>cos</em>. The cosine of NaN or infinity is
  276. * NaN. This is accurate within 1 ulp, and is semi-monotonic.
  277. *
  278. * @param a the angle (in radians)
  279. * @return cos(a)
  280. */
  281. public static native double cos(double a);
  282. /**
  283. * The trigonometric function <em>tan</em>. The tangent of NaN or infinity
  284. * is NaN, and the tangent of 0 retains its sign. This is accurate within 1
  285. * ulp, and is semi-monotonic.
  286. *
  287. * @param a the angle (in radians)
  288. * @return tan(a)
  289. */
  290. public static native double tan(double a);
  291. /**
  292. * The trigonometric function <em>arcsin</em>. The range of angles returned
  293. * is -pi/2 to pi/2 radians (-90 to 90 degrees). If the argument is NaN or
  294. * its absolute value is beyond 1, the result is NaN; and the arcsine of
  295. * 0 retains its sign. This is accurate within 1 ulp, and is semi-monotonic.
  296. *
  297. * @param a the sin to turn back into an angle
  298. * @return arcsin(a)
  299. */
  300. public static native double asin(double a);
  301. /**
  302. * The trigonometric function <em>arccos</em>. The range of angles returned
  303. * is 0 to pi radians (0 to 180 degrees). If the argument is NaN or
  304. * its absolute value is beyond 1, the result is NaN. This is accurate
  305. * within 1 ulp, and is semi-monotonic.
  306. *
  307. * @param a the cos to turn back into an angle
  308. * @return arccos(a)
  309. */
  310. public static native double acos(double a);
  311. /**
  312. * The trigonometric function <em>arcsin</em>. The range of angles returned
  313. * is -pi/2 to pi/2 radians (-90 to 90 degrees). If the argument is NaN, the
  314. * result is NaN; and the arctangent of 0 retains its sign. This is accurate
  315. * within 1 ulp, and is semi-monotonic.
  316. *
  317. * @param a the tan to turn back into an angle
  318. * @return arcsin(a)
  319. * @see #atan2(double, double)
  320. */
  321. public static native double atan(double a);
  322. /**
  323. * A special version of the trigonometric function <em>arctan</em>, for
  324. * converting rectangular coordinates <em>(x, y)</em> to polar
  325. * <em>(r, theta)</em>. This computes the arctangent of x/y in the range
  326. * of -pi to pi radians (-180 to 180 degrees). Special cases:<ul>
  327. * <li>If either argument is NaN, the result is NaN.</li>
  328. * <li>If the first argument is positive zero and the second argument is
  329. * positive, or the first argument is positive and finite and the second
  330. * argument is positive infinity, then the result is positive zero.</li>
  331. * <li>If the first argument is negative zero and the second argument is
  332. * positive, or the first argument is negative and finite and the second
  333. * argument is positive infinity, then the result is negative zero.</li>
  334. * <li>If the first argument is positive zero and the second argument is
  335. * negative, or the first argument is positive and finite and the second
  336. * argument is negative infinity, then the result is the double value
  337. * closest to pi.</li>
  338. * <li>If the first argument is negative zero and the second argument is
  339. * negative, or the first argument is negative and finite and the second
  340. * argument is negative infinity, then the result is the double value
  341. * closest to -pi.</li>
  342. * <li>If the first argument is positive and the second argument is
  343. * positive zero or negative zero, or the first argument is positive
  344. * infinity and the second argument is finite, then the result is the
  345. * double value closest to pi/2.</li>
  346. * <li>If the first argument is negative and the second argument is
  347. * positive zero or negative zero, or the first argument is negative
  348. * infinity and the second argument is finite, then the result is the
  349. * double value closest to -pi/2.</li>
  350. * <li>If both arguments are positive infinity, then the result is the
  351. * double value closest to pi/4.</li>
  352. * <li>If the first argument is positive infinity and the second argument
  353. * is negative infinity, then the result is the double value closest to
  354. * 3*pi/4.</li>
  355. * <li>If the first argument is negative infinity and the second argument
  356. * is positive infinity, then the result is the double value closest to
  357. * -pi/4.</li>
  358. * <li>If both arguments are negative infinity, then the result is the
  359. * double value closest to -3*pi/4.</li>
  360. *
  361. * </ul><p>This is accurate within 2 ulps, and is semi-monotonic. To get r,
  362. * use sqrt(x*x+y*y).
  363. *
  364. * @param y the y position
  365. * @param x the x position
  366. * @return <em>theta</em> in the conversion of (x, y) to (r, theta)
  367. * @see #atan(double)
  368. */
  369. public static native double atan2(double y, double x);
  370. /**
  371. * Take <em>e</em><sup>a</sup>. The opposite of <code>log()</code>. If the
  372. * argument is NaN, the result is NaN; if the argument is positive infinity,
  373. * the result is positive infinity; and if the argument is negative
  374. * infinity, the result is positive zero. This is accurate within 1 ulp,
  375. * and is semi-monotonic.
  376. *
  377. * @param a the number to raise to the power
  378. * @return the number raised to the power of <em>e</em>
  379. * @see #log(double)
  380. * @see #pow(double, double)
  381. */
  382. public static native double exp(double a);
  383. /**
  384. * Take ln(a) (the natural log). The opposite of <code>exp()</code>. If the
  385. * argument is NaN or negative, the result is NaN; if the argument is
  386. * positive infinity, the result is positive infinity; and if the argument
  387. * is either zero, the result is negative infinity. This is accurate within
  388. * 1 ulp, and is semi-monotonic.
  389. *
  390. * <p>Note that the way to get log<sub>b</sub>(a) is to do this:
  391. * <code>ln(a) / ln(b)</code>.
  392. *
  393. * @param a the number to take the natural log of
  394. * @return the natural log of <code>a</code>
  395. * @see #exp(double)
  396. */
  397. public static native double log(double a);
  398. /**
  399. * Take a square root. If the argument is NaN or negative, the result is
  400. * NaN; if the argument is positive infinity, the result is positive
  401. * infinity; and if the result is either zero, the result is the same.
  402. * This is accurate within the limits of doubles.
  403. *
  404. * <p>For other roots, use pow(a, 1 / rootNumber).
  405. *
  406. * @param a the numeric argument
  407. * @return the square root of the argument
  408. * @see #pow(double, double)
  409. */
  410. public static native double sqrt(double a);
  411. /**
  412. * Raise a number to a power. Special cases:<ul>
  413. * <li>If the second argument is positive or negative zero, then the result
  414. * is 1.0.</li>
  415. * <li>If the second argument is 1.0, then the result is the same as the
  416. * first argument.</li>
  417. * <li>If the second argument is NaN, then the result is NaN.</li>
  418. * <li>If the first argument is NaN and the second argument is nonzero,
  419. * then the result is NaN.</li>
  420. * <li>If the absolute value of the first argument is greater than 1 and
  421. * the second argument is positive infinity, or the absolute value of the
  422. * first argument is less than 1 and the second argument is negative
  423. * infinity, then the result is positive infinity.</li>
  424. * <li>If the absolute value of the first argument is greater than 1 and
  425. * the second argument is negative infinity, or the absolute value of the
  426. * first argument is less than 1 and the second argument is positive
  427. * infinity, then the result is positive zero.</li>
  428. * <li>If the absolute value of the first argument equals 1 and the second
  429. * argument is infinite, then the result is NaN.</li>
  430. * <li>If the first argument is positive zero and the second argument is
  431. * greater than zero, or the first argument is positive infinity and the
  432. * second argument is less than zero, then the result is positive zero.</li>
  433. * <li>If the first argument is positive zero and the second argument is
  434. * less than zero, or the first argument is positive infinity and the
  435. * second argument is greater than zero, then the result is positive
  436. * infinity.</li>
  437. * <li>If the first argument is negative zero and the second argument is
  438. * greater than zero but not a finite odd integer, or the first argument is
  439. * negative infinity and the second argument is less than zero but not a
  440. * finite odd integer, then the result is positive zero.</li>
  441. * <li>If the first argument is negative zero and the second argument is a
  442. * positive finite odd integer, or the first argument is negative infinity
  443. * and the second argument is a negative finite odd integer, then the result
  444. * is negative zero.</li>
  445. * <li>If the first argument is negative zero and the second argument is
  446. * less than zero but not a finite odd integer, or the first argument is
  447. * negative infinity and the second argument is greater than zero but not a
  448. * finite odd integer, then the result is positive infinity.</li>
  449. * <li>If the first argument is negative zero and the second argument is a
  450. * negative finite odd integer, or the first argument is negative infinity
  451. * and the second argument is a positive finite odd integer, then the result
  452. * is negative infinity.</li>
  453. * <li>If the first argument is less than zero and the second argument is a
  454. * finite even integer, then the result is equal to the result of raising
  455. * the absolute value of the first argument to the power of the second
  456. * argument.</li>
  457. * <li>If the first argument is less than zero and the second argument is a
  458. * finite odd integer, then the result is equal to the negative of the
  459. * result of raising the absolute value of the first argument to the power
  460. * of the second argument.</li>
  461. * <li>If the first argument is finite and less than zero and the second
  462. * argument is finite and not an integer, then the result is NaN.</li>
  463. * <li>If both arguments are integers, then the result is exactly equal to
  464. * the mathematical result of raising the first argument to the power of
  465. * the second argument if that result can in fact be represented exactly as
  466. * a double value.</li>
  467. *
  468. * </ul><p>(In the foregoing descriptions, a floating-point value is
  469. * considered to be an integer if and only if it is a fixed point of the
  470. * method {@link #ceil(double)} or, equivalently, a fixed point of the
  471. * method {@link #floor(double)}. A value is a fixed point of a one-argument
  472. * method if and only if the result of applying the method to the value is
  473. * equal to the value.) This is accurate within 1 ulp, and is semi-monotonic.
  474. *
  475. * @param a the number to raise
  476. * @param b the power to raise it to
  477. * @return a<sup>b</sup>
  478. */
  479. public static native double pow(double a, double b);
  480. /**
  481. * Get the IEEE 754 floating point remainder on two numbers. This is the
  482. * value of <code>x - y * <em>n</em></code>, where <em>n</em> is the closest
  483. * double to <code>x / y</code> (ties go to the even n); for a zero
  484. * remainder, the sign is that of <code>x</code>. If either argument is NaN,
  485. * the first argument is infinite, or the second argument is zero, the result
  486. * is NaN; if x is finite but y is infinite, the result is x. This is
  487. * accurate within the limits of doubles.
  488. *
  489. * @param x the dividend (the top half)
  490. * @param y the divisor (the bottom half)
  491. * @return the IEEE 754-defined floating point remainder of x/y
  492. * @see #rint(double)
  493. */
  494. public static native double IEEEremainder(double x, double y);
  495. /**
  496. * Take the nearest integer that is that is greater than or equal to the
  497. * argument. If the argument is NaN, infinite, or zero, the result is the
  498. * same; if the argument is between -1 and 0, the result is negative zero.
  499. * Note that <code>Math.ceil(x) == -Math.floor(-x)</code>.
  500. *
  501. * @param a the value to act upon
  502. * @return the nearest integer &gt;= <code>a</code>
  503. */
  504. public static native double ceil(double a);
  505. /**
  506. * Take the nearest integer that is that is less than or equal to the
  507. * argument. If the argument is NaN, infinite, or zero, the result is the
  508. * same. Note that <code>Math.ceil(x) == -Math.floor(-x)</code>.
  509. *
  510. * @param a the value to act upon
  511. * @return the nearest integer &lt;= <code>a</code>
  512. */
  513. public static native double floor(double a);
  514. /**
  515. * Take the nearest integer to the argument. If it is exactly between
  516. * two integers, the even integer is taken. If the argument is NaN,
  517. * infinite, or zero, the result is the same.
  518. *
  519. * @param a the value to act upon
  520. * @return the nearest integer to <code>a</code>
  521. */
  522. public static native double rint(double a);
  523. /**
  524. * Take the nearest integer to the argument. This is equivalent to
  525. * <code>(int) Math.floor(a + 0.5f)</code>. If the argument is NaN, the result
  526. * is 0; otherwise if the argument is outside the range of int, the result
  527. * will be Integer.MIN_VALUE or Integer.MAX_VALUE, as appropriate.
  528. *
  529. * @param a the argument to round
  530. * @return the nearest integer to the argument
  531. * @see Integer#MIN_VALUE
  532. * @see Integer#MAX_VALUE
  533. */
  534. public static int round(float a)
  535. {
  536. // this check for NaN, from JLS 15.21.1, saves a method call
  537. if (a != a)
  538. return 0;
  539. return (int) floor(a + 0.5f);
  540. }
  541. /**
  542. * Take the nearest long to the argument. This is equivalent to
  543. * <code>(long) Math.floor(a + 0.5)</code>. If the argument is NaN, the
  544. * result is 0; otherwise if the argument is outside the range of long, the
  545. * result will be Long.MIN_VALUE or Long.MAX_VALUE, as appropriate.
  546. *
  547. * @param a the argument to round
  548. * @return the nearest long to the argument
  549. * @see Long#MIN_VALUE
  550. * @see Long#MAX_VALUE
  551. */
  552. public static long round(double a)
  553. {
  554. // this check for NaN, from JLS 15.21.1, saves a method call
  555. if (a != a)
  556. return 0;
  557. return (long) floor(a + 0.5d);
  558. }
  559. /**
  560. * Get a random number. This behaves like Random.nextDouble(), seeded by
  561. * System.currentTimeMillis() when first called. In other words, the number
  562. * is from a pseudorandom sequence, and lies in the range [+0.0, 1.0).
  563. * This random sequence is only used by this method, and is threadsafe,
  564. * although you may want your own random number generator if it is shared
  565. * among threads.
  566. *
  567. * @return a random number
  568. * @see Random#nextDouble()
  569. * @see System#currentTimeMillis()
  570. */
  571. public static synchronized double random()
  572. {
  573. if (rand == null)
  574. rand = new Random();
  575. return rand.nextDouble();
  576. }
  577. /**
  578. * Convert from degrees to radians. The formula for this is
  579. * radians = degrees * (pi/180); however it is not always exact given the
  580. * limitations of floating point numbers.
  581. *
  582. * @param degrees an angle in degrees
  583. * @return the angle in radians
  584. * @since 1.2
  585. */
  586. public static double toRadians(double degrees)
  587. {
  588. return (degrees * PI) / 180;
  589. }
  590. /**
  591. * Convert from radians to degrees. The formula for this is
  592. * degrees = radians * (180/pi); however it is not always exact given the
  593. * limitations of floating point numbers.
  594. *
  595. * @param rads an angle in radians
  596. * @return the angle in degrees
  597. * @since 1.2
  598. */
  599. public static double toDegrees(double rads)
  600. {
  601. return (rads * 180) / PI;
  602. }
  603. /**
  604. * <p>
  605. * Take a cube root. If the argument is <code>NaN</code>, an infinity or
  606. * zero, then the original value is returned. The returned result is
  607. * within 1 ulp of the exact result. For a finite value, <code>x</code>,
  608. * the cube root of <code>-x</code> is equal to the negation of the cube root
  609. * of <code>x</code>.
  610. * </p>
  611. * <p>
  612. * For a square root, use <code>sqrt</code>. For other roots, use
  613. * <code>pow(a, 1 / rootNumber)</code>.
  614. * </p>
  615. *
  616. * @param a the numeric argument
  617. * @return the cube root of the argument
  618. * @see #sqrt(double)
  619. * @see #pow(double, double)
  620. * @since 1.5
  621. */
  622. public static native double cbrt(double a);
  623. /**
  624. * <p>
  625. * Returns the hyperbolic cosine of the given value. For a value,
  626. * <code>x</code>, the hyperbolic cosine is <code>(e<sup>x</sup> +
  627. * e<sup>-x</sup>)/2</code>
  628. * with <code>e</code> being <a href="#E">Euler's number</a>. The returned
  629. * result is within 2.5 ulps of the exact result.
  630. * </p>
  631. * <p>
  632. * If the supplied value is <code>NaN</code>, then the original value is
  633. * returned. For either infinity, positive infinity is returned.
  634. * The hyperbolic cosine of zero is 1.0.
  635. * </p>
  636. *
  637. * @param a the numeric argument
  638. * @return the hyperbolic cosine of <code>a</code>.
  639. * @since 1.5
  640. */
  641. public static native double cosh(double a);
  642. /**
  643. * <p>
  644. * Returns <code>e<sup>a</sup> - 1. For values close to 0, the
  645. * result of <code>expm1(a) + 1</code> tend to be much closer to the
  646. * exact result than simply <code>exp(x)</code>. The result is within
  647. * 1 ulp of the exact result, and results are semi-monotonic. For finite
  648. * inputs, the returned value is greater than or equal to -1.0. Once
  649. * a result enters within half a ulp of this limit, the limit is returned.
  650. * </p>
  651. * <p>
  652. * For <code>NaN</code>, positive infinity and zero, the original value
  653. * is returned. Negative infinity returns a result of -1.0 (the limit).
  654. * </p>
  655. *
  656. * @param a the numeric argument
  657. * @return <code>e<sup>a</sup> - 1</code>
  658. * @since 1.5
  659. */
  660. public static native double expm1(double a);
  661. /**
  662. * <p>
  663. * Returns the hypotenuse, <code>a<sup>2</sup> + b<sup>2</sup></code>,
  664. * without intermediate overflow or underflow. The returned result is
  665. * within 1 ulp of the exact result. If one parameter is held constant,
  666. * then the result in the other parameter is semi-monotonic.
  667. * </p>
  668. * <p>
  669. * If either of the arguments is an infinity, then the returned result
  670. * is positive infinity. Otherwise, if either argument is <code>NaN</code>,
  671. * then <code>NaN</code> is returned.
  672. * </p>
  673. *
  674. * @param a the first parameter.
  675. * @param b the second parameter.
  676. * @return the hypotenuse matching the supplied parameters.
  677. * @since 1.5
  678. */
  679. public static native double hypot(double a, double b);
  680. /**
  681. * <p>
  682. * Returns the base 10 logarithm of the supplied value. The returned
  683. * result is within 1 ulp of the exact result, and the results are
  684. * semi-monotonic.
  685. * </p>
  686. * <p>
  687. * Arguments of either <code>NaN</code> or less than zero return
  688. * <code>NaN</code>. An argument of positive infinity returns positive
  689. * infinity. Negative infinity is returned if either positive or negative
  690. * zero is supplied. Where the argument is the result of
  691. * <code>10<sup>n</sup</code>, then <code>n</code> is returned.
  692. * </p>
  693. *
  694. * @param a the numeric argument.
  695. * @return the base 10 logarithm of <code>a</code>.
  696. * @since 1.5
  697. */
  698. public static native double log10(double a);
  699. /**
  700. * <p>
  701. * Returns the natural logarithm resulting from the sum of the argument,
  702. * <code>a</code> and 1. For values close to 0, the
  703. * result of <code>log1p(a)</code> tend to be much closer to the
  704. * exact result than simply <code>log(1.0+a)</code>. The returned
  705. * result is within 1 ulp of the exact result, and the results are
  706. * semi-monotonic.
  707. * </p>
  708. * <p>
  709. * Arguments of either <code>NaN</code> or less than -1 return
  710. * <code>NaN</code>. An argument of positive infinity or zero
  711. * returns the original argument. Negative infinity is returned from an
  712. * argument of -1.
  713. * </p>
  714. *
  715. * @param a the numeric argument.
  716. * @return the natural logarithm of <code>a</code> + 1.
  717. * @since 1.5
  718. */
  719. public static native double log1p(double a);
  720. /**
  721. * <p>
  722. * Returns the sign of the argument as follows:
  723. * </p>
  724. * <ul>
  725. * <li>If <code>a</code> is greater than zero, the result is 1.0.</li>
  726. * <li>If <code>a</code> is less than zero, the result is -1.0.</li>
  727. * <li>If <code>a</code> is <code>NaN</code>, the result is <code>NaN</code>.
  728. * <li>If <code>a</code> is positive or negative zero, the result is the
  729. * same.</li>
  730. * </ul>
  731. *
  732. * @param a the numeric argument.
  733. * @return the sign of the argument.
  734. * @since 1.5.
  735. */
  736. public static double signum(double a)
  737. {
  738. if (Double.isNaN(a))
  739. return Double.NaN;
  740. if (a > 0)
  741. return 1.0;
  742. if (a < 0)
  743. return -1.0;
  744. return a;
  745. }
  746. /**
  747. * <p>
  748. * Returns the sign of the argument as follows:
  749. * </p>
  750. * <ul>
  751. * <li>If <code>a</code> is greater than zero, the result is 1.0f.</li>
  752. * <li>If <code>a</code> is less than zero, the result is -1.0f.</li>
  753. * <li>If <code>a</code> is <code>NaN</code>, the result is <code>NaN</code>.
  754. * <li>If <code>a</code> is positive or negative zero, the result is the
  755. * same.</li>
  756. * </ul>
  757. *
  758. * @param a the numeric argument.
  759. * @return the sign of the argument.
  760. * @since 1.5.
  761. */
  762. public static float signum(float a)
  763. {
  764. if (Float.isNaN(a))
  765. return Float.NaN;
  766. if (a > 0)
  767. return 1.0f;
  768. if (a < 0)
  769. return -1.0f;
  770. return a;
  771. }
  772. /**
  773. * <p>
  774. * Returns the hyperbolic sine of the given value. For a value,
  775. * <code>x</code>, the hyperbolic sine is <code>(e<sup>x</sup> -
  776. * e<sup>-x</sup>)/2</code>
  777. * with <code>e</code> being <a href="#E">Euler's number</a>. The returned
  778. * result is within 2.5 ulps of the exact result.
  779. * </p>
  780. * <p>
  781. * If the supplied value is <code>NaN</code>, an infinity or a zero, then the
  782. * original value is returned.
  783. * </p>
  784. *
  785. * @param a the numeric argument
  786. * @return the hyperbolic sine of <code>a</code>.
  787. * @since 1.5
  788. */
  789. public static native double sinh(double a);
  790. /**
  791. * <p>
  792. * Returns the hyperbolic tangent of the given value. For a value,
  793. * <code>x</code>, the hyperbolic tangent is <code>(e<sup>x</sup> -
  794. * e<sup>-x</sup>)/(e<sup>x</sup> + e<sup>-x</sup>)</code>
  795. * (i.e. <code>sinh(a)/cosh(a)</code>)
  796. * with <code>e</code> being <a href="#E">Euler's number</a>. The returned
  797. * result is within 2.5 ulps of the exact result. The absolute value
  798. * of the exact result is always less than 1. Computed results are thus
  799. * less than or equal to 1 for finite arguments, with results within
  800. * half a ulp of either positive or negative 1 returning the appropriate
  801. * limit value (i.e. as if the argument was an infinity).
  802. * </p>
  803. * <p>
  804. * If the supplied value is <code>NaN</code> or zero, then the original
  805. * value is returned. Positive infinity returns +1.0 and negative infinity
  806. * returns -1.0.
  807. * </p>
  808. *
  809. * @param a the numeric argument
  810. * @return the hyperbolic tangent of <code>a</code>.
  811. * @since 1.5
  812. */
  813. public static native double tanh(double a);
  814. /**
  815. * Return the ulp for the given double argument. The ulp is the
  816. * difference between the argument and the next larger double. Note
  817. * that the sign of the double argument is ignored, that is,
  818. * ulp(x) == ulp(-x). If the argument is a NaN, then NaN is returned.
  819. * If the argument is an infinity, then +Inf is returned. If the
  820. * argument is zero (either positive or negative), then
  821. * {@link Double#MIN_VALUE} is returned.
  822. * @param d the double whose ulp should be returned
  823. * @return the difference between the argument and the next larger double
  824. * @since 1.5
  825. */
  826. public static double ulp(double d)
  827. {
  828. if (Double.isNaN(d))
  829. return d;
  830. if (Double.isInfinite(d))
  831. return Double.POSITIVE_INFINITY;
  832. // This handles both +0.0 and -0.0.
  833. if (d == 0.0)
  834. return Double.MIN_VALUE;
  835. long bits = Double.doubleToLongBits(d);
  836. final int mantissaBits = 52;
  837. final int exponentBits = 11;
  838. final long mantMask = (1L << mantissaBits) - 1;
  839. long mantissa = bits & mantMask;
  840. final long expMask = (1L << exponentBits) - 1;
  841. long exponent = (bits >>> mantissaBits) & expMask;
  842. // Denormal number, so the answer is easy.
  843. if (exponent == 0)
  844. {
  845. long result = (exponent << mantissaBits) | 1L;
  846. return Double.longBitsToDouble(result);
  847. }
  848. // Conceptually we want to have '1' as the mantissa. Then we would
  849. // shift the mantissa over to make a normal number. If this underflows
  850. // the exponent, we will make a denormal result.
  851. long newExponent = exponent - mantissaBits;
  852. long newMantissa;
  853. if (newExponent > 0)
  854. newMantissa = 0;
  855. else
  856. {
  857. newMantissa = 1L << -(newExponent - 1);
  858. newExponent = 0;
  859. }
  860. return Double.longBitsToDouble((newExponent << mantissaBits) | newMantissa);
  861. }
  862. /**
  863. * Return the ulp for the given float argument. The ulp is the
  864. * difference between the argument and the next larger float. Note
  865. * that the sign of the float argument is ignored, that is,
  866. * ulp(x) == ulp(-x). If the argument is a NaN, then NaN is returned.
  867. * If the argument is an infinity, then +Inf is returned. If the
  868. * argument is zero (either positive or negative), then
  869. * {@link Float#MIN_VALUE} is returned.
  870. * @param f the float whose ulp should be returned
  871. * @return the difference between the argument and the next larger float
  872. * @since 1.5
  873. */
  874. public static float ulp(float f)
  875. {
  876. if (Float.isNaN(f))
  877. return f;
  878. if (Float.isInfinite(f))
  879. return Float.POSITIVE_INFINITY;
  880. // This handles both +0.0 and -0.0.
  881. if (f == 0.0)
  882. return Float.MIN_VALUE;
  883. int bits = Float.floatToIntBits(f);
  884. final int mantissaBits = 23;
  885. final int exponentBits = 8;
  886. final int mantMask = (1 << mantissaBits) - 1;
  887. int mantissa = bits & mantMask;
  888. final int expMask = (1 << exponentBits) - 1;
  889. int exponent = (bits >>> mantissaBits) & expMask;
  890. // Denormal number, so the answer is easy.
  891. if (exponent == 0)
  892. {
  893. int result = (exponent << mantissaBits) | 1;
  894. return Float.intBitsToFloat(result);
  895. }
  896. // Conceptually we want to have '1' as the mantissa. Then we would
  897. // shift the mantissa over to make a normal number. If this underflows
  898. // the exponent, we will make a denormal result.
  899. int newExponent = exponent - mantissaBits;
  900. int newMantissa;
  901. if (newExponent > 0)
  902. newMantissa = 0;
  903. else
  904. {
  905. newMantissa = 1 << -(newExponent - 1);
  906. newExponent = 0;
  907. }
  908. return Float.intBitsToFloat((newExponent << mantissaBits) | newMantissa);
  909. }
  910. }