1. /*
  2. * @(#)Math.java 1.50 00/02/02
  3. *
  4. * Copyright 1994-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.lang;
  11. import java.util.Random;
  12. /**
  13. * The class <code>Math</code> contains methods for performing basic
  14. * numeric operations such as the elementary exponential, logarithm,
  15. * square root, and trigonometric functions.
  16. * <p>
  17. * Unlike some of the numeric functions of class <code>StrictMath</code>,
  18. * all implementations of the equivalent functions of class <code>Math</code>
  19. * are not defined to return the bit-for-bit same results. This relaxation
  20. * permits better-performing implementations where strict reproducibility
  21. * is not required.
  22. * <p>
  23. * By default many of the <code>Math</code> functions simply delegate to the
  24. * equivalent functions in <code>StrictMath</code> for their implementations.
  25. * Code generators are encouraged to use platform-specific native libraries
  26. * or microprocessor instructions, where available, to provide higher-performance
  27. * implementations of <code>Math</code> functions. Such higher-performance
  28. * implementations still must conform to the specification for <code>Math</code>.
  29. *
  30. * @author unascribed
  31. * @version 1.50, 02/02/00
  32. * @since JDK1.0
  33. */
  34. public final strictfp class Math {
  35. /**
  36. * Don't let anyone instantiate this class.
  37. */
  38. private Math() {}
  39. /**
  40. * The <code>double</code> value that is closer than any other to
  41. * <code>e</code>, the base of the natural logarithms.
  42. */
  43. public static final double E = 2.7182818284590452354;
  44. /**
  45. * The <code>double</code> value that is closer than any other to
  46. * <i>pi</i>, the ratio of the circumference of a circle to its diameter.
  47. */
  48. public static final double PI = 3.14159265358979323846;
  49. /**
  50. * Returns the trigonometric sine of an angle. Special cases:
  51. * <ul><li>If the argument is NaN or an infinity, then the
  52. * result is NaN.
  53. * <li>If the argument is positive zero, then the result is
  54. * positive zero; if the argument is negative zero, then the
  55. * result is negative zero.</ul>
  56. * <p>
  57. * A result must be within 1 ulp of the correctly rounded result. Results
  58. * must be semi-monotonic.
  59. *
  60. * @param a an angle, in radians.
  61. * @return the sine of the argument.
  62. */
  63. public static double sin(double a) {
  64. return StrictMath.sin(a); // default impl. delegates to StrictMath
  65. }
  66. /**
  67. * Returns the trigonometric cosine of an angle. Special case:
  68. * <ul><li>If the argument is NaN or an infinity, then the
  69. * result is NaN.</ul>
  70. * <p>
  71. * A result must be within 1 ulp of the correctly rounded result. Results
  72. * must be semi-monotonic.
  73. *
  74. * @param a an angle, in radians.
  75. * @return the cosine of the argument.
  76. */
  77. public static double cos(double a) {
  78. return StrictMath.cos(a); // default impl. delegates to StrictMath
  79. }
  80. /**
  81. * Returns the trigonometric tangent of an angle. Special cases:
  82. * <ul><li>If the argument is NaN or an infinity, then the result
  83. * is NaN.
  84. * <li>If the argument is positive zero, then the result is
  85. * positive zero; if the argument is negative zero, then the
  86. * result is negative zero</ul>
  87. * <p>
  88. * A result must be within 1 ulp of the correctly rounded result. Results
  89. * must be semi-monotonic.
  90. *
  91. * @param a an angle, in radians.
  92. * @return the tangent of the argument.
  93. */
  94. public static double tan(double a) {
  95. return StrictMath.tan(a); // default impl. delegates to StrictMath
  96. }
  97. /**
  98. * Returns the arc sine of an angle, in the range of -<i>pi</i>/2 through
  99. * <i>pi</i>/2. Special cases:
  100. * <ul><li>If the argument is NaN or its absolute value is greater
  101. * than 1, then the result is NaN.
  102. * <li>If the argument is positive zero, then the result is positive
  103. * zero; if the argument is negative zero, then the result is
  104. * negative zero.</ul>
  105. * <p>
  106. * A result must be within 1 ulp of the correctly rounded result. Results
  107. * must be semi-monotonic.
  108. *
  109. * @param a the <code>double</code> value whose arc sine is to
  110. * be returned.
  111. * @return the arc sine of the argument.
  112. */
  113. public static double asin(double a) {
  114. return StrictMath.asin(a); // default impl. delegates to StrictMath
  115. }
  116. /**
  117. * Returns the arc cosine of an angle, in the range of 0.0 through
  118. * <i>pi</i>. Special case:
  119. * <ul><li>If the argument is NaN or its absolute value is greater
  120. * than 1, then the result is NaN.</ul>
  121. * <p>
  122. * A result must be within 1 ulp of the correctly rounded result. Results
  123. * must be semi-monotonic.
  124. *
  125. * @param a the <code>double</code> value whose arc cosine is to
  126. * be returned.
  127. * @return the arc cosine of the argument.
  128. */
  129. public static double acos(double a) {
  130. return StrictMath.acos(a); // default impl. delegates to StrictMath
  131. }
  132. /**
  133. * Returns the arc tangent of an angle, in the range of -<i>pi</i>/2
  134. * through <i>pi</i>/2. Special cases:
  135. * <ul><li>If the argument is NaN, then the result is NaN.
  136. * <li>If the argument is positive zero, then the result is positive
  137. * zero; if the argument is negative zero, then the result is
  138. * negative zero.</ul>
  139. * <p>
  140. * A result must be within 1 ulp of the correctly rounded result. Results
  141. * must be semi-monotonic.
  142. *
  143. * @param a the <code>double</code> value whose arc tangent is to
  144. * be returned.
  145. * @return the arc tangent of the argument.
  146. */
  147. public static double atan(double a) {
  148. return StrictMath.atan(a); // default impl. delegates to StrictMath
  149. }
  150. /**
  151. * Converts an angle measured in degrees to the equivalent angle
  152. * measured in radians.
  153. *
  154. * @param angdeg an angle, in degrees
  155. * @return the measurement of the angle <code>angdeg</code>
  156. * in radians.
  157. * @since 1.2
  158. */
  159. public static double toRadians(double angdeg) {
  160. return angdeg / 180.0 * PI;
  161. }
  162. /**
  163. * Converts an angle measured in radians to the equivalent angle
  164. * measured in degrees.
  165. *
  166. * @param angrad an angle, in radians
  167. * @return the measurement of the angle <code>angrad</code>
  168. * in degrees.
  169. * @since 1.2
  170. */
  171. public static double toDegrees(double angrad) {
  172. return angrad * 180.0 / PI;
  173. }
  174. /**
  175. * Returns the exponential number <i>e</i> (i.e., 2.718...) raised to
  176. * the power of a <code>double</code> value. Special cases:
  177. * <ul><li>If the argument is NaN, the result is NaN.
  178. * <li>If the argument is positive infinity, then the result is
  179. * positive infinity.
  180. * <li>If the argument is negative infinity, then the result is
  181. * positive zero.</ul>
  182. * <p>
  183. * A result must be within 1 ulp of the correctly rounded result. Results
  184. * must be semi-monotonic.
  185. *
  186. * @param a a <code>double</code> value.
  187. * @return the value <i>e</i><sup>a</sup>, where <i>e</i> is the base of
  188. * the natural logarithms.
  189. */
  190. public static double exp(double a) {
  191. return StrictMath.exp(a); // default impl. delegates to StrictMath
  192. }
  193. /**
  194. * Returns the natural logarithm (base <i>e</i>) of a <code>double</code>
  195. * value. Special cases:
  196. * <ul><li>If the argument is NaN or less than zero, then the result
  197. * is NaN.
  198. * <li>If the argument is positive infinity, then the result is
  199. * positive infinity.
  200. * <li>If the argument is positive zero or negative zero, then the
  201. * result is negative infinity.</ul>
  202. * <p>
  203. * A result must be within 1 ulp of the correctly rounded result. Results
  204. * must be semi-monotonic.
  205. *
  206. * @param a a number greater than <code>0.0</code>.
  207. * @return the value ln <code>a</code>, the natural logarithm of
  208. * <code>a</code>.
  209. */
  210. public static double log(double a) {
  211. return StrictMath.log(a); // default impl. delegates to StrictMath
  212. }
  213. /**
  214. * Returns the correctly rounded positive square root of a
  215. * <code>double</code> value.
  216. * Special cases:
  217. * <ul><li>If the argument is NaN or less than zero, then the result
  218. * is NaN.
  219. * <li>If the argument is positive infinity, then the result is positive
  220. * infinity.
  221. * <li>If the argument is positive zero or negative zero, then the
  222. * result is the same as the argument.</ul>
  223. * Otherwise, the result is the <code>double</code> value closest to
  224. * the true mathetmatical square root of the argument value.
  225. *
  226. * @param a a <code>double</code> value.
  227. * <!--@return the value of √ <code>a</code>.-->
  228. * @return the positive square root of <code>a</code>.
  229. * If the argument is NaN or less than zero, the result is NaN.
  230. */
  231. public static double sqrt(double a) {
  232. return StrictMath.sqrt(a); // default impl. delegates to StrictMath
  233. // Note that hardware sqrt instructions
  234. // frequently can be directly used by JITs
  235. // and should be much faster than doing
  236. // Math.sqrt in software.
  237. }
  238. /**
  239. * Computes the remainder operation on two arguments as prescribed
  240. * by the IEEE 754 standard.
  241. * The remainder value is mathematically equal to
  242. * <code>f1 - f2</code> × <i>n</i>,
  243. * where <i>n</i> is the mathematical integer closest to the exact
  244. * mathematical value of the quotient <code>f1/f2</code>, and if two
  245. * mathematical integers are equally close to <code>f1/f2</code>,
  246. * then <i>n</i> is the integer that is even. If the remainder is
  247. * zero, its sign is the same as the sign of the first argument.
  248. * Special cases:
  249. * <ul><li>If either argument is NaN, or the first argument is infinite,
  250. * or the second argument is positive zero or negative zero, then the
  251. * result is NaN.
  252. * <li>If the first argument is finite and the second argument is
  253. * infinite, then the result is the same as the first argument.</ul>
  254. *
  255. * @param f1 the dividend.
  256. * @param f2 the divisor.
  257. * @return the remainder when <code>f1</code> is divided by
  258. * <code>f2</code>.
  259. */
  260. public static double IEEEremainder(double f1, double f2) {
  261. return StrictMath.IEEEremainder(f1, f2); // delegate to StrictMath
  262. }
  263. /**
  264. * Returns the smallest (closest to negative infinity)
  265. * <code>double</code> value that is not less than the argument and is
  266. * equal to a mathematical integer. Special cases:
  267. * <ul><li>If the argument value is already equal to a mathematical
  268. * integer, then the result is the same as the argument.
  269. * <li>If the argument is NaN or an infinity or positive zero or negative
  270. * zero, then the result is the same as the argument.
  271. * <li>If the argument value is less than zero but greater than -1.0,
  272. * then the result is negative zero.</ul>
  273. * Note that the value of <code>Math.ceil(x)</code> is exactly the
  274. * value of <code>-Math.floor(-x)</code>.
  275. *
  276. * @param a a <code>double</code> value.
  277. * <!--@return the value ⌈ <code>a</code> ⌉.-->
  278. * @return the smallest (closest to negative infinity)
  279. * <code>double</code> value that is not less than the argument
  280. * and is equal to a mathematical integer.
  281. */
  282. public static double ceil(double a) {
  283. return StrictMath.ceil(a); // default impl. delegates to StrictMath
  284. }
  285. /**
  286. * Returns the largest (closest to positive infinity)
  287. * <code>double</code> value that is not greater than the argument and
  288. * is equal to a mathematical integer. Special cases:
  289. * <ul><li>If the argument value is already equal to a mathematical
  290. * integer, then the result is the same as the argument.
  291. * <li>If the argument is NaN or an infinity or positive zero or
  292. * negative zero, then the result is the same as the argument.</ul>
  293. *
  294. * @param a a <code>double</code> value.
  295. * <!--@return the value ⌊ <code>a</code> ⌋.-->
  296. * @return the largest (closest to positive infinity)
  297. * <code>double</code> value that is not greater than the argument
  298. * and is equal to a mathematical integer.
  299. */
  300. public static double floor(double a) {
  301. return StrictMath.floor(a); // default impl. delegates to StrictMath
  302. }
  303. /**
  304. * Returns the <code>double</code> value that is closest in value to
  305. * <code>a</code> and is equal to a mathematical integer. If two
  306. * <code>double</code> values that are mathematical integers are equally
  307. * close to the value of the argument, the result is the integer value
  308. * that is even. Special cases:
  309. * <ul><li>If the argument value is already equal to a mathematical
  310. * integer, then the result is the same as the argument.
  311. * <li>If the argument is NaN or an infinity or positive zero or negative
  312. * zero, then the result is the same as the argument.</ul>
  313. *
  314. * @param a a <code>double</code> value.
  315. * @return the closest <code>double</code> value to <code>a</code> that is
  316. * equal to a mathematical integer.
  317. */
  318. public static double rint(double a) {
  319. return StrictMath.rint(a); // default impl. delegates to StrictMath
  320. }
  321. /**
  322. * Converts rectangular coordinates (<code>b</code>, <code>a</code>)
  323. * to polar (r, <i>theta</i>).
  324. * This method computes the phase <i>theta</i> by computing an arc tangent
  325. * of <code>a/b</code> in the range of -<i>pi</i> to <i>pi</i>. Special
  326. * cases:
  327. * <ul><li>If either argument is NaN, then the result is NaN.
  328. * <li>If the first argument is positive zero and the second argument
  329. * is positive, or the first argument is positive and finite and the
  330. * second argument is positive infinity, then the result is positive
  331. * zero.
  332. * <li>If the first argument is negative zero and the second argument
  333. * is positive, or the first argument is negative and finite and the
  334. * second argument is positive infinity, then the result is negative zero.
  335. * <li>If the first argument is positive zero and the second argument
  336. * is negative, or the first argument is positive and finite and the
  337. * second argument is negative infinity, then the result is the
  338. * <code>double</code> value closest to pi.
  339. * <li>If the first argument is negative zero and the second argument
  340. * is negative, or the first argument is negative and finite and the
  341. * second argument is negative infinity, then the result is the
  342. * <code>double</code> value closest to -pi.
  343. * <li>If the first argument is positive and the second argument is
  344. * positive zero or negative zero, or the first argument is positive
  345. * infinity and the second argument is finite, then the result is the
  346. * <code>double</code> value closest to pi/2.
  347. * <li>If the first argument is negative and the second argument is
  348. * positive zero or negative zero, or the first argument is negative
  349. * infinity and the second argument is finite, then the result is the
  350. * <code>double</code> value closest to -pi/2.
  351. * <li>If both arguments are positive infinity, then the result is the
  352. * <code>double</code> value closest to pi/4.
  353. * <li>If the first argument is positive infinity and the second argument
  354. * is negative infinity, then the result is the <code>double</code>
  355. * value closest to 3*pi/4.
  356. * <li>If the first argument is negative infinity and the second argument
  357. * is positive infinity, then the result is the <code>double</code> value
  358. * closest to -pi/4.
  359. * <li>If both arguments are negative infinity, then the result is the
  360. * <code>double</code> value closest to -3*pi/4.</ul>
  361. * <p>
  362. * A result must be within 2 ulps of the correctly rounded result. Results
  363. * must be semi-monotonic.
  364. *
  365. * @param a a <code>double</code> value.
  366. * @param b a <code>double</code> value.
  367. * @return the <i>theta</i> component of the point
  368. * (<i>r</i>, <i>theta</i>)
  369. * in polar coordinates that corresponds to the point
  370. * (<i>b</i>, <i>a</i>) in Cartesian coordinates.
  371. */
  372. public static double atan2(double a, double b) {
  373. return StrictMath.atan2(a, b); // default impl. delegates to StrictMath
  374. }
  375. /**
  376. * Returns of value of the first argument raised to the power of the
  377. * second argument. Special cases:
  378. * <ul><li>If the second argument is positive or negative zero, then the
  379. * result is 1.0.
  380. * <li>If the second argument is 1.0, then the result is the same as the
  381. * first argument.
  382. * <li>If the second argument is NaN, then the result is NaN.
  383. * <li>If the first argument is NaN and the second argument is nonzero,
  384. * then the result is NaN.
  385. * <li>If the absolute value of the first argument is greater than 1 and
  386. * the second argument is positive infinity, or the absolute value of the
  387. * first argument is less than 1 and the second argument is negative
  388. * infinity, then the result is positive infinity.
  389. * <li>If the absolute value of the first argument is greater than 1 and
  390. * the second argument is negative infinity, or the absolute value of the
  391. * first argument is less than 1 and the second argument is positive
  392. * infinity, then the result is positive zero.
  393. * <li>If the absolute value of the first argument equals 1 and the
  394. * second argument is infinite, then the result is NaN.
  395. * <li>If the first argument is positive zero and the second argument is
  396. * greater than zero, or the first argument is positive infinity and the
  397. * second argument is less than zero, then the result is positive zero.
  398. * <li>If the first argument is positive zero and the second argument is
  399. * less than zero, or the first argument is positive infinity and the
  400. * second argument is greater than zero, then the result is positive
  401. * infinity.
  402. * <li>If the first argument is negative zero and the second argument is
  403. * greater than zero but not a finite odd integer, or the first argument
  404. * is negative infinity and the second argument is less than zero but not
  405. * a finite odd integer, then the result is positive zero.
  406. * <li>If the first argument is negative zero and the second argument is
  407. * a positive finite odd integer, or the first argument is negative
  408. * infinity and the second argument is a negative finite odd integer,
  409. * then the result is negative zero.
  410. * <li>If the first argument is negative zero and the second argument is
  411. * less than zero but not a finite odd integer, or the first argument is
  412. * negative infinity and the second argument is greater than zero but not
  413. * a finite odd integer, then the result is positive infinity.
  414. * <li>If the first argument is negative zero and the second argument is
  415. * a negative finite odd integer, or the first argument is negative
  416. * infinity and the second argument is a positive finite odd integer,
  417. * then the result is negative infinity.
  418. * <li>If the first argument is less than zero and the second argument is
  419. * a finite even integer, then the result is equal to the result of
  420. * raising the absolute value of the first argument to the power of the
  421. * second argument.
  422. * <li>If the first argument is less than zero and the second argument
  423. * is a finite odd integer, then the result is equal to the negative of
  424. * the result of raising the absolute value of the first argument to the
  425. * power of the second argument.
  426. * <li>If the first argument is finite and less than zero and the second
  427. * argument is finite and not an integer, then the result is NaN.
  428. * <li>If both arguments are integers, then the result is exactly equal
  429. * to the mathematical result of raising the first argument to the power
  430. * of the second argument if that result can in fact be represented
  431. * exactly as a double value.</ul>
  432. *
  433. * <p>(In the foregoing descriptions, a floating-point value is
  434. * considered to be an integer if and only if it is a fixed point of the
  435. * method {@link #ceil <tt>ceil</tt>} or, which is the same thing, a fixed
  436. * point of the method {@link #floor <tt>floor</tt>}. A value is a fixed
  437. * point of a one-argument method if and only if the result of applying
  438. * the method to the value is equal to the value.)
  439. * <p>
  440. * A result must be within 1 ulp of the correctly rounded result. Results
  441. * must be semi-monotonic.
  442. *
  443. * @param a a <code>double</code> value.
  444. * @param b a <code>double</code> value.
  445. * @return the value <code>a<sup>b</sup></code>.
  446. */
  447. public static double pow(double a, double b) {
  448. return StrictMath.pow(a, b); // default impl. delegates to StrictMath
  449. }
  450. /**
  451. * Returns the closest <code>int</code> to the argument. The
  452. * result is rounded to an integer by adding 1/2, taking the
  453. * floor of the result, and casting the result to type <code>int</code>.
  454. * In other words, the result is equal to the value of the expression:
  455. * <p><pre>(int)Math.floor(a + 0.5f)</pre>
  456. * <p>
  457. * Special cases:
  458. * <ul><li>If the argument is NaN, the result is 0.
  459. * <li>If the argument is negative infinity or any value less than or
  460. * equal to the value of <code>Integer.MIN_VALUE</code>, the result is
  461. * equal to the value of <code>Integer.MIN_VALUE</code>.
  462. * <li>If the argument is positive infinity or any value greater than or
  463. * equal to the value of <code>Integer.MAX_VALUE</code>, the result is
  464. * equal to the value of <code>Integer.MAX_VALUE</code>.</ul>
  465. *
  466. * @param a a <code>float</code> value.
  467. * @return the value of the argument rounded to the nearest
  468. * <code>int</code> value.
  469. * @see java.lang.Integer#MAX_VALUE
  470. * @see java.lang.Integer#MIN_VALUE
  471. */
  472. public static int round(float a) {
  473. return (int)floor(a + 0.5f);
  474. }
  475. /**
  476. * Returns the closest <code>long</code> to the argument. The result
  477. * is rounded to an integer by adding 1/2, taking the floor of the
  478. * result, and casting the result to type <code>long</code>. In other
  479. * words, the result is equal to the value of the expression:
  480. * <p><pre>(long)Math.floor(a + 0.5d)</pre>
  481. * <p>
  482. * Special cases:
  483. * <ul><li>If the argument is NaN, the result is 0.
  484. * <li>If the argument is negative infinity or any value less than or
  485. * equal to the value of <code>Long.MIN_VALUE</code>, the result is
  486. * equal to the value of <code>Long.MIN_VALUE</code>.
  487. * <li>If the argument is positive infinity or any value greater than or
  488. * equal to the value of <code>Long.MAX_VALUE</code>, the result is
  489. * equal to the value of <code>Long.MAX_VALUE</code>.</ul>
  490. *
  491. * @param a a <code>double</code> value.
  492. * @return the value of the argument rounded to the nearest
  493. * <code>long</code> value.
  494. * @see java.lang.Long#MAX_VALUE
  495. * @see java.lang.Long#MIN_VALUE
  496. */
  497. public static long round(double a) {
  498. return (long)floor(a + 0.5d);
  499. }
  500. private static Random randomNumberGenerator;
  501. private static synchronized void initRNG() {
  502. if (randomNumberGenerator == null)
  503. randomNumberGenerator = new Random();
  504. }
  505. /**
  506. * Returns a <code>double</code> value with a positive sign, greater
  507. * than or equal to <code>0.0</code> and less than <code>1.0</code>.
  508. * Returned values are chosen pseudorandomly with (approximately)
  509. * uniform distribution from that range.
  510. * <p>
  511. * When this method is first called, it creates a single new
  512. * pseudorandom-number generator, exactly as if by the expression
  513. * <blockquote><pre>new java.util.Random</pre></blockquote>
  514. * This new pseudorandom-number generator is used thereafter for all
  515. * calls to this method and is used nowhere else.
  516. * <p>
  517. * This method is properly synchronized to allow correct use by more
  518. * than one thread. However, if many threads need to generate
  519. * pseudorandom numbers at a great rate, it may reduce contention for
  520. * each thread to have its own pseudorandom-number generator.
  521. *
  522. * @return a pseudorandom <code>double</code> greater than or equal
  523. * to <code>0.0</code> and less than <code>1.0</code>.
  524. * @see java.util.Random#nextDouble()
  525. */
  526. public static double random() {
  527. if (randomNumberGenerator == null) initRNG();
  528. return randomNumberGenerator.nextDouble();
  529. }
  530. /**
  531. * Returns the absolute value of an <code>int</code> value.
  532. * If the argument is not negative, the argument is returned.
  533. * If the argument is negative, the negation of the argument is returned.
  534. * <p>
  535. * Note that if the argument is equal to the value of
  536. * <code>Integer.MIN_VALUE</code>, the most negative representable
  537. * <code>int</code> value, the result is that same value, which is
  538. * negative.
  539. *
  540. * @param a an <code>int</code> value.
  541. * @return the absolute value of the argument.
  542. * @see java.lang.Integer#MIN_VALUE
  543. */
  544. public static int abs(int a) {
  545. return (a < 0) ? -a : a;
  546. }
  547. /**
  548. * Returns the absolute value of a <code>long</code> value.
  549. * If the argument is not negative, the argument is returned.
  550. * If the argument is negative, the negation of the argument is returned.
  551. * <p>
  552. * Note that if the argument is equal to the value of
  553. * <code>Long.MIN_VALUE</code>, the most negative representable
  554. * <code>long</code> value, the result is that same value, which is
  555. * negative.
  556. *
  557. * @param a a <code>long</code> value.
  558. * @return the absolute value of the argument.
  559. * @see java.lang.Long#MIN_VALUE
  560. */
  561. public static long abs(long a) {
  562. return (a < 0) ? -a : a;
  563. }
  564. /**
  565. * Returns the absolute value of a <code>float</code> value.
  566. * If the argument is not negative, the argument is returned.
  567. * If the argument is negative, the negation of the argument is returned.
  568. * Special cases:
  569. * <ul><li>If the argument is positive zero or negative zero, the
  570. * result is positive zero.
  571. * <li>If the argument is infinite, the result is positive infinity.
  572. * <li>If the argument is NaN, the result is NaN.</ul>
  573. * In other words, the result is equal to the value of the expression:
  574. * <p><pre>Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))</pre>
  575. *
  576. * @param a a <code>float</code> value.
  577. * @return the absolute value of the argument.
  578. */
  579. public static float abs(float a) {
  580. return (a <= 0.0F) ? 0.0F - a : a;
  581. }
  582. /**
  583. * Returns the absolute value of a <code>double</code> value.
  584. * If the argument is not negative, the argument is returned.
  585. * If the argument is negative, the negation of the argument is returned.
  586. * Special cases:
  587. * <ul><li>If the argument is positive zero or negative zero, the result
  588. * is positive zero.
  589. * <li>If the argument is infinite, the result is positive infinity.
  590. * <li>If the argument is NaN, the result is NaN.</ul>
  591. * In other words, the result is equal to the value of the expression:
  592. * <p><pre>Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)</pre>
  593. *
  594. * @param a a <code>double</code> value.
  595. * @return the absolute value of the argument.
  596. */
  597. public static double abs(double a) {
  598. return (a <= 0.0D) ? 0.0D - a : a;
  599. }
  600. /**
  601. * Returns the greater of two <code>int</code> values. That is, the
  602. * result is the argument closer to the value of
  603. * <code>Integer.MAX_VALUE</code>. If the arguments have the same value,
  604. * the result is that same value.
  605. *
  606. * @param a an <code>int</code> value.
  607. * @param b an <code>int</code> value.
  608. * @return the larger of <code>a</code> and <code>b</code>.
  609. * @see java.lang.Long#MAX_VALUE
  610. */
  611. public static int max(int a, int b) {
  612. return (a >= b) ? a : b;
  613. }
  614. /**
  615. * Returns the greater of two <code>long</code> values. That is, the
  616. * result is the argument closer to the value of
  617. * <code>Long.MAX_VALUE</code>. If the argumens have the same value,
  618. * the result is that same value.
  619. *
  620. * @param a a <code>long</code> value.
  621. * @param b a <code>long</code> value.
  622. * @return the larger of <code>a</code> and <code>b</code>.
  623. * @see java.lang.Long#MAX_VALUE
  624. */
  625. public static long max(long a, long b) {
  626. return (a >= b) ? a : b;
  627. }
  628. private static long negativeZeroFloatBits = Float.floatToIntBits(-0.0f);
  629. private static long negativeZeroDoubleBits = Double.doubleToLongBits(-0.0d);
  630. /**
  631. * Returns the greater of two <code>float</code> values. That is, the
  632. * result is the argument closer to positive infinity. If the
  633. * arguments have the same value, the result is that same value. If
  634. * either value is <code>NaN</code>, then the result is <code>NaN</code>.
  635. * Unlike the the numerical comparison operators, this method considers
  636. * negative zero to be strictly smaller than positive zero. If one
  637. * argument is positive zero and the other negative zero, the result
  638. * is positive zero.
  639. *
  640. * @param a a <code>float</code> value.
  641. * @param b a <code>float</code> value.
  642. * @return the larger of <code>a</code> and <code>b</code>.
  643. */
  644. public static float max(float a, float b) {
  645. if (a != a) return a; // a is NaN
  646. if ((a == 0.0f) && (b == 0.0f)
  647. && (Float.floatToIntBits(a) == negativeZeroFloatBits)) {
  648. return b;
  649. }
  650. return (a >= b) ? a : b;
  651. }
  652. /**
  653. * Returns the greater of two <code>double</code> values. That is, the
  654. * result is the argument closer to positive infinity. If the
  655. * arguments have the same value, the result is that same value. If
  656. * either value is <code>NaN</code>, then the result is <code>NaN</code>.
  657. * Unlike the the numerical comparison operators, this method considers
  658. * negative zero to be strictly smaller than positive zero. If one
  659. * argument is positive zero and the other negative zero, the result
  660. * is positive zero.
  661. *
  662. * @param a a <code>double</code> value.
  663. * @param b a <code>double</code> value.
  664. * @return the larger of <code>a</code> and <code>b</code>.
  665. */
  666. public static double max(double a, double b) {
  667. if (a != a) return a; // a is NaN
  668. if ((a == 0.0d) && (b == 0.0d)
  669. && (Double.doubleToLongBits(a) == negativeZeroDoubleBits)) {
  670. return b;
  671. }
  672. return (a >= b) ? a : b;
  673. }
  674. /**
  675. * Returns the smaller of two <code>int</code> values. That is, the
  676. * result the argument closer to the value of <code>Integer.MIN_VALUE</code>.
  677. * If the arguments have the same value, the result is that same value.
  678. *
  679. * @param a an <code>int</code> value.
  680. * @param b an <code>int</code> value.
  681. * @return the smaller of <code>a</code> and <code>b</code>.
  682. * @see java.lang.Long#MIN_VALUE
  683. */
  684. public static int min(int a, int b) {
  685. return (a <= b) ? a : b;
  686. }
  687. /**
  688. * Returns the smaller of two <code>long</code> values. That is, the
  689. * result is the argument closer to the value of
  690. * <code>Long.MIN_VALUE</code>. If the arguments have the same value,
  691. * the result is that same value.
  692. *
  693. * @param a a <code>long</code> value.
  694. * @param b a <code>long</code> value.
  695. * @return the smaller of <code>a</code> and <code>b</code>.
  696. * @see java.lang.Long#MIN_VALUE
  697. */
  698. public static long min(long a, long b) {
  699. return (a <= b) ? a : b;
  700. }
  701. /**
  702. * Returns the smaller of two <code>float</code> values. That is, the
  703. * result is the value closer to negative infinity. If the arguments
  704. * have the same value, the result is that same value. If either value
  705. * is <code>NaN</code>, then the result is <code>NaN</code>. Unlike the
  706. * the numerical comparison operators, this method considers negative zero
  707. * to be strictly smaller than positive zero. If one argument is
  708. * positive zero and the other is negative zero, the result is negative
  709. * zero.
  710. *
  711. * @param a a <code>float</code> value.
  712. * @param b a <code>float</code> value.
  713. * @return the smaller of <code>a</code> and <code>b.</code>
  714. */
  715. public static float min(float a, float b) {
  716. if (a != a) return a; // a is NaN
  717. if ((a == 0.0f) && (b == 0.0f)
  718. && (Float.floatToIntBits(b) == negativeZeroFloatBits)) {
  719. return b;
  720. }
  721. return (a <= b) ? a : b;
  722. }
  723. /**
  724. * Returns the smaller of two <code>double</code> values. That is, the
  725. * result is the value closer to negative infinity. If the arguments have
  726. * the same value, the result is that same value. If either value
  727. * is <code>NaN</code>, then the result is <code>NaN</code>. Unlike the
  728. * the numerical comparison operators, this method considers negative zero
  729. * to be strictly smaller than positive zero. If one argument is
  730. * positive zero and the other is negative zero, the result is negative
  731. * zero.
  732. *
  733. * @param a a <code>double</code> value.
  734. * @param b a <code>double</code> value.
  735. * @return the smaller of <code>a</code> and <code>b</code>.
  736. */
  737. public static double min(double a, double b) {
  738. if (a != a) return a; // a is NaN
  739. if ((a == 0.0d) && (b == 0.0d)
  740. && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
  741. return b;
  742. }
  743. return (a <= b) ? a : b;
  744. }
  745. }