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