Math.java 35 KB

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