1. /*
  2. * @(#)Math.java 1.40 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. import java.util.Random;
  9. /**
  10. * The class <code>Math</code> contains methods for performing basic
  11. * numeric operations such as the elementary exponential, logarithm,
  12. * square root, and trigonometric functions.
  13. * <p>
  14. * To help ensure portability of Java programs, the definitions of
  15. * many of the numeric functions in this package require that they
  16. * produce the same results as certain published algorithms. These
  17. * algorithms are available from the well-known network library
  18. * <code>netlib</code> as the package "Freely Distributable
  19. * Math Library" (<code>fdlibm</code>). These algorithms, which
  20. * are written in the C programming language, are then to be
  21. * understood as executed with all floating-point operations
  22. * following the rules of Java floating-point arithmetic.
  23. * <p>
  24. * The network library may be found on the World Wide Web at:
  25. * <blockquote><pre>
  26. * <a href="http://metalab.unc.edu/">http://metalab.unc.edu/</a>
  27. * </pre></blockquote>
  28. * <p>
  29. * The Java math library is defined with respect to the version of
  30. * <code>fdlibm</code> dated January 4, 1995. Where
  31. * <code>fdlibm</code> provides more than one definition for a
  32. * function (such as <code>acos</code>), use the "IEEE 754 core
  33. * function" version (residing in a file whose name begins with
  34. * the letter <code>e</code>).
  35. *
  36. * @author unascribed
  37. * @version 1.40, 11/29/01
  38. * @since JDK1.0
  39. */
  40. public final class Math {
  41. /**
  42. * Don't let anyone instantiate this class.
  43. */
  44. private Math() {}
  45. /**
  46. * The <code>double</code> value that is closer than any other to
  47. * <code>e</code>, the base of the natural logarithms.
  48. */
  49. public static final double E = 2.7182818284590452354;
  50. /**
  51. * The <code>double</code> value that is closer than any other to
  52. * <i>pi</i>, the ratio of the circumference of a circle to its diameter.
  53. */
  54. public static final double PI = 3.14159265358979323846;
  55. /**
  56. * Returns the trigonometric sine of an angle.
  57. *
  58. * @param a an angle, in radians.
  59. * @return the sine of the argument.
  60. */
  61. public static native double sin(double a);
  62. /**
  63. * Returns the trigonometric cosine of an angle.
  64. *
  65. * @param a an angle, in radians.
  66. * @return the cosine of the argument.
  67. */
  68. public static native double cos(double a);
  69. /**
  70. * Returns the trigonometric tangent of an angle.
  71. *
  72. * @param a an angle, in radians.
  73. * @return the tangent of the argument.
  74. */
  75. public static native double tan(double a);
  76. /**
  77. * Returns the arc sine of an angle, in the range of -<i>pi</i>/2 through
  78. * <i>pi</i>/2.
  79. *
  80. * @param a the <code>double</code> value whose arc sine is to
  81. * be returned.
  82. * @return the arc sine of the argument.
  83. */
  84. public static native double asin(double a);
  85. /**
  86. * Returns the arc cosine of an angle, in the range of 0.0 through
  87. * <i>pi</i>.
  88. *
  89. * @param a the <code>double</code> value whose arc cosine is to
  90. * be returned.
  91. * @return the arc cosine of the argument.
  92. */
  93. public static native double acos(double a);
  94. /**
  95. * Returns the arc tangent of an angle, in the range of -<i>pi</i>/2
  96. * through <i>pi</i>/2.
  97. *
  98. * @param a the <code>double</code> value whose arc tangent is to
  99. * be returned.
  100. * @return the arc tangent of the argument.
  101. */
  102. public static native double atan(double a);
  103. /**
  104. * Converts an angle measured in degrees to the equivalent angle
  105. * measured in radians.
  106. *
  107. * @param angdeg an angle, in degrees
  108. * @return the measurement of the angle <code>angdeg</code>
  109. * in radians.
  110. * @since JDK1.2
  111. */
  112. public static double toRadians(double angdeg) {
  113. return angdeg / 180.0 * PI;
  114. }
  115. /**
  116. * Converts an angle measured in radians to the equivalent angle
  117. * measured in degrees.
  118. *
  119. * @param angrad an angle, in radians
  120. * @return the measurement of the angle <code>angrad</code>
  121. * in degrees.
  122. * @since JDK1.2
  123. */
  124. public static double toDegrees(double angrad) {
  125. return angrad * 180.0 / PI;
  126. }
  127. /**
  128. * Returns the exponential number <i>e</i> (i.e., 2.718...) raised to
  129. * the power of a <code>double</code> value.
  130. *
  131. * @param a a <code>double</code> value.
  132. * @return the value <i>e</i><sup>a</sup>, where <i>e</i> is the base of
  133. * the natural logarithms.
  134. */
  135. public static native double exp(double a);
  136. /**
  137. * Returns the natural logarithm (base <i>e</i>) of a <code>double</code>
  138. * value.
  139. *
  140. * @param a a number greater than <code>0.0</code>.
  141. * @return the value ln <code>a</code>, the natural logarithm of
  142. * <code>a</code>.
  143. */
  144. public static native double log(double a);
  145. /**
  146. * Returns the square root of a <code>double</code> value.
  147. *
  148. * @param a a <code>double</code> value.
  149. * <!--@return the value of √ <code>a</code>.-->
  150. * @return the square root of <code>a</code>.
  151. * If the argument is NaN or less than zero, the result is NaN.
  152. */
  153. public static native double sqrt(double a);
  154. /**
  155. * Computes the remainder operation on two arguments as prescribed
  156. * by the IEEE 754 standard.
  157. * The remainder value is mathematically equal to
  158. * <code>f1 - f2</code> × <i>n</i>,
  159. * where <i>n</i> is the mathematical integer closest to the exact
  160. * mathematical value of the quotient <code>f1/f2</code>, and if two
  161. * mathematical integers are equally close to <code>f1/f2</code>,
  162. * then <i>n</i> is the integer that is even. If the remainder is
  163. * zero, its sign is the same as the sign of the first argument.
  164. *
  165. * @param f1 the dividend.
  166. * @param f2 the divisor.
  167. * @return the remainder when <code>f1</code> is divided by
  168. * <code>f2</code>.
  169. */
  170. public static native double IEEEremainder(double f1, double f2);
  171. /**
  172. * Returns the smallest (closest to negative infinity)
  173. * <code>double</code> value that is not less than the argument and is
  174. * equal to a mathematical integer.
  175. *
  176. * @param a a <code>double</code> value.
  177. * <!--@return the value ⌈ <code>a</code> ⌉.-->
  178. * @return the smallest (closest to negative infinity)
  179. * <code>double</code> value that is not less than the argument
  180. * and is equal to a mathematical integer.
  181. */
  182. public static native double ceil(double a);
  183. /**
  184. * Returns the largest (closest to positive infinity)
  185. * <code>double</code> value that is not greater than the argument and
  186. * is equal to a mathematical integer.
  187. *
  188. * @param a a <code>double</code> value.
  189. * @param a an assigned value.
  190. * <!--@return the value ⌊ <code>a</code> ⌋.-->
  191. * @return the largest (closest to positive infinity)
  192. * <code>double</code> value that is not greater than the argument
  193. * and is equal to a mathematical integer.
  194. */
  195. public static native double floor(double a);
  196. /**
  197. * returns the closest integer to the argument.
  198. *
  199. * @param a a <code>double</code> value.
  200. * @return the closest <code>double</code> value to <code>a</code> that is
  201. * equal to a mathematical integer. If two <code>double</code>
  202. * values that are mathematical integers are equally close to the
  203. * value of the argument, the result is the integer value that
  204. * is even.
  205. */
  206. public static native double rint(double a);
  207. /**
  208. * Converts rectangular coordinates (<code>b</code>, <code>a</code>)
  209. * to polar (r, <i>theta</i>).
  210. * This method computes the phase <i>theta</i> by computing an arc tangent
  211. * of <code>a/b</code> in the range of -<i>pi</i> to <i>pi</i>.
  212. *
  213. * @param a a <code>double</code> value.
  214. * @param b a <code>double</code> value.
  215. * @return the <i>theta</i> component of the point
  216. * (<i>r</i>, <i>theta</i>)
  217. * in polar coordinates that corresponds to the point
  218. * (<i>b</i>, <i>a</i>) in Cartesian coordinates.
  219. */
  220. public static native double atan2(double a, double b);
  221. /**
  222. * Returns of value of the first argument raised to the power of the
  223. * second argument.
  224. * <p>
  225. * If (<code>a == 0.0</code>), then <code>b</code> must be
  226. * greater than <code>0.0</code> otherwise an exception is thrown.
  227. * An exception also will occur if (<code>a <= 0.0</code>)
  228. * and <code>b</code> is not equal to a whole number.
  229. *
  230. * @param a a <code>double</code> value.
  231. * @param b a <code>double</code> value.
  232. * @return the value <code>a<sup>b</sup></code>.
  233. * @exception ArithmeticException if (<code>a == 0.0</code>) and
  234. * (<code>b <= 0.0</code>), or
  235. * if (<code>a <= 0.0</code>) and <code>b</code>
  236. * is not equal to a whole number.
  237. */
  238. public static native double pow(double a, double b);
  239. /**
  240. * Returns the closest <code>int</code> to the argument.
  241. * <p>
  242. * If the argument is negative infinity or any value less than or
  243. * equal to the value of <code>Integer.MIN_VALUE</code>, the result is
  244. * equal to the value of <code>Integer.MIN_VALUE</code>.
  245. * <p>
  246. * If the argument is positive infinity or any value greater than or
  247. * equal to the value of <code>Integer.MAX_VALUE</code>, the result is
  248. * equal to the value of <code>Integer.MAX_VALUE</code>.
  249. *
  250. * @param a a <code>float</code> value.
  251. * @return the value of the argument rounded to the nearest
  252. * <code>int</code> value.
  253. * @see java.lang.Integer#MAX_VALUE
  254. * @see java.lang.Integer#MIN_VALUE
  255. */
  256. public static int round(float a) {
  257. return (int)floor(a + 0.5f);
  258. }
  259. /**
  260. * Returns the closest <code>long</code> to the argument.
  261. * <p>
  262. * If the argument is negative infinity or any value less than or
  263. * equal to the value of <code>Long.MIN_VALUE</code>, the result is
  264. * equal to the value of <code>Long.MIN_VALUE</code>.
  265. * <p>
  266. * If the argument is positive infinity or any value greater than or
  267. * equal to the value of <code>Long.MAX_VALUE</code>, the result is
  268. * equal to the value of <code>Long.MAX_VALUE</code>.
  269. *
  270. * @param a a <code>double</code> value.
  271. * @return the value of the argument rounded to the nearest
  272. * <code>long</code> value.
  273. * @see java.lang.Long#MAX_VALUE
  274. * @see java.lang.Long#MIN_VALUE
  275. */
  276. public static long round(double a) {
  277. return (long)floor(a + 0.5d);
  278. }
  279. private static Random randomNumberGenerator;
  280. /**
  281. * Returns a random number greater than or equal to <code>0.0</code>
  282. * and less than <code>1.0</code>. Returned values are chosen
  283. * pseudorandomly with (approximately) uniform distribution from that
  284. * range.
  285. * <p>
  286. * When this method is first called, it creates a single new
  287. * pseudorandom-number generator, exactly as if by the expression
  288. * <blockquote><pre>new java.util.Random</pre></blockquote>
  289. * This new pseudorandom-number generator is used thereafter for all
  290. * calls to this method and is used nowhere else.
  291. * <p>
  292. * This method is properly synchronized to allow correct use by more
  293. * than one thread. However, if many threads need to generate
  294. * pseudorandom numbers at a great rate, it may reduce contention for
  295. * each thread to have its own pseudorandom-number generator.
  296. *
  297. * @return a pseudorandom <code>double</code> greater than or equal
  298. * to <code>0.0</code> and less than <code>1.0</code>.
  299. * @see java.util.Random#nextDouble()
  300. */
  301. public static synchronized double random() {
  302. if (randomNumberGenerator == null)
  303. randomNumberGenerator = new Random();
  304. return randomNumberGenerator.nextDouble();
  305. }
  306. /**
  307. * Returns the absolute value of an <code>int</code> value.
  308. * If the argument is not negative, the argument is returned.
  309. * If the argument is negative, the negation of the argument is returned.
  310. * <p>
  311. * Note that if the argument is equal to the value of
  312. * <code>Integer.MIN_VALUE</code>, the most negative representable
  313. * <code>int</code> value, the result is that same value, which is
  314. * negative.
  315. *
  316. * @param a an <code>int</code> value.
  317. * @return the absolute value of the argument.
  318. * @see java.lang.Integer#MIN_VALUE
  319. */
  320. public static int abs(int a) {
  321. return (a < 0) ? -a : a;
  322. }
  323. /**
  324. * Returns the absolute value of a <code>long</code> value.
  325. * If the argument is not negative, the argument is returned.
  326. * If the argument is negative, the negation of the argument is returned.
  327. * <p>
  328. * Note that if the argument is equal to the value of
  329. * <code>Long.MIN_VALUE</code>, the most negative representable
  330. * <code>long</code> value, the result is that same value, which is
  331. * negative.
  332. *
  333. * @param a a <code>long</code> value.
  334. * @return the absolute value of the argument.
  335. * @see java.lang.Long#MIN_VALUE
  336. */
  337. public static long abs(long a) {
  338. return (a < 0) ? -a : a;
  339. }
  340. /**
  341. * Returns the absolute value of a <code>float</code> value.
  342. * If the argument is not negative, the argument is returned.
  343. * If the argument is negative, the negation of the argument is returned.
  344. *
  345. * @param a a <code>float</code> value.
  346. * @return the absolute value of the argument.
  347. */
  348. public static float abs(float a) {
  349. return (a <= 0.0F) ? 0.0F - a : a;
  350. }
  351. /**
  352. * Returns the absolute value of a <code>double</code> value.
  353. * If the argument is not negative, the argument is returned.
  354. * If the argument is negative, the negation of the argument is returned.
  355. *
  356. * @param a a <code>double</code> value.
  357. * @return the absolute value of the argument.
  358. */
  359. public static double abs(double a) {
  360. return (a <= 0.0D) ? 0.0D - a : a;
  361. }
  362. /**
  363. * Returns the greater of two <code>int</code> values.
  364. *
  365. * @param a an <code>int</code> value.
  366. * @param b an <code>int</code> value.
  367. * @return the larger of <code>a</code> and <code>b</code>.
  368. */
  369. public static int max(int a, int b) {
  370. return (a >= b) ? a : b;
  371. }
  372. /**
  373. * Returns the greater of two <code>long</code> values.
  374. *
  375. * @param a a <code>long</code> value.
  376. * @param b a <code>long</code> value.
  377. * @return the larger of <code>a</code> and <code>b</code>.
  378. */
  379. public static long max(long a, long b) {
  380. return (a >= b) ? a : b;
  381. }
  382. private static long negativeZeroFloatBits = Float.floatToIntBits(-0.0f);
  383. private static long negativeZeroDoubleBits = Double.doubleToLongBits(-0.0d);
  384. /**
  385. * Returns the greater of two <code>float</code> values. If either value
  386. * is <code>NaN</code>, then the result is <code>NaN</code>. Unlike the
  387. * the numerical comparison operators, this method considers negative zero
  388. * to be strictly smaller than positive zero.
  389. *
  390. * @param a a <code>float</code> value.
  391. * @param b a <code>float</code> value.
  392. * @return the larger of <code>a</code> and <code>b</code>.
  393. */
  394. public static float max(float a, float b) {
  395. if (a != a) return a; // a is NaN
  396. if ((a == 0.0f) && (b == 0.0f)
  397. && (Float.floatToIntBits(a) == negativeZeroFloatBits)) {
  398. return b;
  399. }
  400. return (a >= b) ? a : b;
  401. }
  402. /**
  403. * Returns the greater of two <code>double</code> values. If either value
  404. * is <code>NaN</code>, then the result is <code>NaN</code>. Unlike the
  405. * the numerical comparison operators, this method considers negative zero
  406. * to be strictly smaller than positive zero.
  407. *
  408. * @param a a <code>double</code> value.
  409. * @param b a <code>double</code> value.
  410. * @return the larger of <code>a</code> and <code>b</code>.
  411. */
  412. public static double max(double a, double b) {
  413. if (a != a) return a; // a is NaN
  414. if ((a == 0.0d) && (b == 0.0d)
  415. && (Double.doubleToLongBits(a) == negativeZeroDoubleBits)) {
  416. return b;
  417. }
  418. return (a >= b) ? a : b;
  419. }
  420. /**
  421. * Returns the smaller of two <code>int</code> values.
  422. *
  423. * @param a an <code>int</code> value.
  424. * @param b an <code>int</code> value.
  425. * @return the smaller of <code>a</code> and <code>b</code>.
  426. */
  427. public static int min(int a, int b) {
  428. return (a <= b) ? a : b;
  429. }
  430. /**
  431. * Returns the smaller of two <code>long</code> values.
  432. *
  433. * @param a a <code>long</code> value.
  434. * @param b a <code>long</code> value.
  435. * @return the smaller of <code>a</code> and <code>b</code>.
  436. */
  437. public static long min(long a, long b) {
  438. return (a <= b) ? a : b;
  439. }
  440. /**
  441. * Returns the smaller of two <code>float</code> values. If either value
  442. * is <code>NaN</code>, then the result is <code>NaN</code>. Unlike the
  443. * the numerical comparison operators, this method considers negative zero
  444. * to be strictly smaller than positive zero.
  445. *
  446. * @param a a <code>float</code> value.
  447. * @param b a <code>float</code> value.
  448. * @return the smaller of <code>a</code> and <code>b.</code>
  449. */
  450. public static float min(float a, float b) {
  451. if (a != a) return a; // a is NaN
  452. if ((a == 0.0f) && (b == 0.0f)
  453. && (Float.floatToIntBits(b) == negativeZeroFloatBits)) {
  454. return b;
  455. }
  456. return (a <= b) ? a : b;
  457. }
  458. /**
  459. * Returns the smaller of two <code>double</code> values. If either value
  460. * is <code>NaN</code>, then the result is <code>NaN</code>. Unlike the
  461. * the numerical comparison operators, this method considers negative zero
  462. * to be strictly smaller than positive zero.
  463. *
  464. * @param a a <code>double</code> value.
  465. * @param b a <code>double</code> value.
  466. * @return the smaller of <code>a</code> and <code>b</code>.
  467. */
  468. public static double min(double a, double b) {
  469. if (a != a) return a; // a is NaN
  470. if ((a == 0.0d) && (b == 0.0d)
  471. && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
  472. return b;
  473. }
  474. return (a <= b) ? a : b;
  475. }
  476. }