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