001: /*
002: * @(#)StrictMath.java 1.18 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.lang;
029:
030: import java.util.Random;
031:
032: /**
033: * The class <code>StrictMath</code> contains methods for performing basic
034: * numeric operations such as the elementary exponential, logarithm,
035: * square root, and trigonometric functions.
036: * <p>
037: * To help ensure portability of Java programs, the definitions of
038: * many of the numeric functions in this package require that they
039: * produce the same results as certain published algorithms. These
040: * algorithms are available from the well-known network library
041: * <code>netlib</code> as the package "Freely Distributable
042: * Math Library" (<code>fdlibm</code>). These algorithms, which
043: * are written in the C programming language, are then to be
044: * understood as executed with all floating-point operations
045: * following the rules of Java floating-point arithmetic.
046: * <p>
047: * The network library may be found on the World Wide Web at:
048: * <blockquote><pre>
049: * <a href="http://metalab.unc.edu/">http://metalab.unc.edu/</a>
050: * </pre></blockquote>
051: * <p>
052: * The Java math library is defined with respect to the version of
053: * <code>fdlibm</code> dated January 4, 1995. Where
054: * <code>fdlibm</code> provides more than one definition for a
055: * function (such as <code>acos</code>), use the "IEEE 754 core
056: * function" version (residing in a file whose name begins with
057: * the letter <code>e</code>).
058: *
059: * @author unascribed
060: * @version 1.9, 02/02/00
061: * @since 1.3
062: */
063:
064: public final strictfp class StrictMath {
065:
066: /* Work-around for Symbian tool bug. No longer needed. */
067: private static native void init();
068:
069: static {
070: init();
071: }
072:
073: /**
074: * Don't let anyone instantiate this class.
075: */
076: private StrictMath() {
077: }
078:
079: /**
080: * The <code>double</code> value that is closer than any other to
081: * <i>e</i>, the base of the natural logarithms.
082: */
083: public static final double E = 2.7182818284590452354;
084:
085: /**
086: * The <code>double</code> value that is closer than any other to
087: * <i>pi</i>, the ratio of the circumference of a circle to its
088: * diameter.
089: */
090: public static final double PI = 3.14159265358979323846;
091:
092: /**
093: * Returns the trigonometric sine of an angle. Special cases:
094: * <ul><li>If the argument is NaN or an infinity, then the
095: * result is NaN.
096: * <li>If the argument is zero, then the result is a zero with the
097: * same sign as the argument.</ul>
098: *
099: * @param a an angle, in radians.
100: * @return the sine of the argument.
101: */
102: public static native double sin(double a);
103:
104: /**
105: * Returns the trigonometric cosine of an angle. Special cases:
106: * <ul><li>If the argument is NaN or an infinity, then the
107: * result is NaN.</ul>
108: *
109: * @param a an angle, in radians.
110: * @return the cosine of the argument.
111: */
112: public static native double cos(double a);
113:
114: /**
115: * Returns the trigonometric tangent of an angle. Special cases:
116: * <ul><li>If the argument is NaN or an infinity, then the result
117: * is NaN.
118: * <li>If the argument is zero, then the result is a zero with the
119: * same sign as the argument.</ul>
120: *
121: * @param a an angle, in radians.
122: * @return the tangent of the argument.
123: */
124: public static native double tan(double a);
125:
126: /**
127: * Returns the arc sine of an angle, in the range of -<i>pi</i>/2 through
128: * <i>pi</i>/2. Special cases:
129: * <ul><li>If the argument is NaN or its absolute value is greater
130: * than 1, then the result is NaN.
131: * <li>If the argument is zero, then the result is a zero with the
132: * same sign as the argument.</ul>
133: *
134: * @param a the value whose arc sine is to be returned.
135: * @return the arc sine of the argument.
136: */
137: public static native double asin(double a);
138:
139: /**
140: * Returns the arc cosine of an angle, in the range of 0.0 through
141: * <i>pi</i>. Special case:
142: * <ul><li>If the argument is NaN or its absolute value is greater
143: * than 1, then the result is NaN.</ul>
144: *
145: * @param a the value whose arc cosine is to be returned.
146: * @return the arc cosine of the argument.
147: */
148: public static native double acos(double a);
149:
150: /**
151: * Returns the arc tangent of an angle, in the range of -<i>pi</i>/2
152: * through <i>pi</i>/2. Special cases:
153: * <ul><li>If the argument is NaN, then the result is NaN.
154: * <li>If the argument is zero, then the result is a zero with the
155: * same sign as the argument.</ul>
156: *
157: * @param a the value whose arc tangent is to be returned.
158: * @return the arc tangent of the argument.
159: */
160: public static native double atan(double a);
161:
162: /**
163: * Converts an angle measured in degrees to an approximately
164: * equivalent angle measured in radians. The conversion from
165: * degrees to radians is generally inexact.
166: *
167: * @param angdeg an angle, in degrees
168: * @return the measurement of the angle <code>angdeg</code>
169: * in radians.
170: */
171: public static double toRadians(double angdeg) {
172: return angdeg / 180.0 * PI;
173: }
174:
175: /**
176: * Converts an angle measured in radians to an approximately
177: * equivalent angle measured in degrees. The conversion from
178: * radians to degrees is generally inexact; users should
179: * <i>not</i> expect <code>cos(toRadians(90.0))</code> to exactly
180: * equal <code>0.0</code>.
181: *
182: * @param angrad an angle, in radians
183: * @return the measurement of the angle <code>angrad</code>
184: * in degrees.
185: */
186: public static double toDegrees(double angrad) {
187: return angrad * 180.0 / PI;
188: }
189:
190: /**
191: * Returns Euler's number <i>e</i> raised to the power of a
192: * <code>double</code> value. Special cases:
193: * <ul><li>If the argument is NaN, the result is NaN.
194: * <li>If the argument is positive infinity, then the result is
195: * positive infinity.
196: * <li>If the argument is negative infinity, then the result is
197: * positive zero.</ul>
198: *
199: * @param a the exponent to raise <i>e</i> to.
200: * @return the value <i>e</i><sup><code>a</code></sup>,
201: * where <i>e</i> is the base of the natural logarithms.
202: */
203: public static native double exp(double a);
204:
205: /**
206: * Returns the natural logarithm (base <i>e</i>) of a <code>double</code>
207: * value. Special cases:
208: * <ul><li>If the argument is NaN or less than zero, then the result
209: * is NaN.
210: * <li>If the argument is positive infinity, then the result is
211: * positive infinity.
212: * <li>If the argument is positive zero or negative zero, then the
213: * result is negative infinity.</ul>
214: *
215: * @param a a number greater than <code>0.0</code>.
216: * @return the value ln <code>a</code>, the natural logarithm of
217: * <code>a</code>.
218: */
219: public static native double log(double a);
220:
221: /**
222: * Returns the correctly rounded positive square root of a
223: * <code>double</code> value.
224: * Special cases:
225: * <ul><li>If the argument is NaN or less than zero, then the result
226: * is NaN.
227: * <li>If the argument is positive infinity, then the result is positive
228: * infinity.
229: * <li>If the argument is positive zero or negative zero, then the
230: * result is the same as the argument.</ul>
231: * Otherwise, the result is the <code>double</code> value closest to
232: * the true mathematical square root of the argument value.
233: *
234: * @param a a value.
235: * <!--@return the value of √ <code>a</code>.-->
236: * @return the positive square root of <code>a</code>.
237: */
238: public static native double sqrt(double a);
239:
240: /**
241: * Computes the remainder operation on two arguments as prescribed
242: * by the IEEE 754 standard.
243: * The remainder value is mathematically equal to
244: * <code>f1 - f2</code> × <i>n</i>,
245: * where <i>n</i> is the mathematical integer closest to the exact
246: * mathematical value of the quotient <code>f1/f2</code>, and if two
247: * mathematical integers are equally close to <code>f1/f2</code>,
248: * then <i>n</i> is the integer that is even. If the remainder is
249: * zero, its sign is the same as the sign of the first argument.
250: * Special cases:
251: * <ul><li>If either argument is NaN, or the first argument is infinite,
252: * or the second argument is positive zero or negative zero, then the
253: * result is NaN.
254: * <li>If the first argument is finite and the second argument is
255: * infinite, then the result is the same as the first argument.</ul>
256: *
257: * @param f1 the dividend.
258: * @param f2 the divisor.
259: * @return the remainder when <code>f1</code> is divided by
260: * <code>f2</code>.
261: */
262: public static native double IEEEremainder(double f1, double f2);
263:
264: /**
265: * Returns the smallest (closest to negative infinity)
266: * <code>double</code> value that is not less than the argument and is
267: * equal to a mathematical integer. Special cases:
268: * <ul><li>If the argument value is already equal to a mathematical
269: * integer, then the result is the same as the argument.
270: * <li>If the argument is NaN or an infinity or positive zero or negative
271: * zero, then the result is the same as the argument.
272: * <li>If the argument value is less than zero but greater than -1.0,
273: * then the result is negative zero.</ul>
274: * Note that the value of <code>Math.ceil(x)</code> is exactly the
275: * value of <code>-Math.floor(-x)</code>.
276: *
277: * @param a a value.
278: * <!--@return the value ⌈ <code>a</code> ⌉.-->
279: * @return the smallest (closest to negative infinity)
280: * floating-point value that is not less than the argument
281: * and is equal to a mathematical integer.
282: */
283: public static native double ceil(double a);
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: * floating-point value that is not greater than the argument
298: * and is equal to a mathematical integer.
299: */
300: public static native double floor(double a);
301:
302: /**
303: * Returns the <code>double</code> value that is closest in value
304: * to the argument and is equal to a mathematical integer. If two
305: * <code>double</code> values that are mathematical integers are
306: * equally close to the value of the argument, the result is the
307: * integer value that is even. Special cases:
308: * <ul><li>If the argument value is already equal to a mathematical
309: * integer, then the result is the same as the argument.
310: * <li>If the argument is NaN or an infinity or positive zero or negative
311: * zero, then the result is the same as the argument.</ul>
312: *
313: * @param a a value.
314: * @return the closest floating-point value to <code>a</code> that is
315: * equal to a mathematical integer.
316: */
317: public static native double rint(double a);
318:
319: /**
320: * Converts rectangular coordinates (<code>x</code>, <code>y</code>)
321: * to polar (r, <i>theta</i>).
322: * This method computes the phase <i>theta</i> by computing an arc tangent
323: * of <code>y/x</code> in the range of -<i>pi</i> to <i>pi</i>. Special
324: * cases:
325: * <ul><li>If either argument is NaN, then the result is NaN.
326: * <li>If the first argument is positive zero and the second argument
327: * is positive, or the first argument is positive and finite and the
328: * second argument is positive infinity, then the result is positive
329: * zero.
330: * <li>If the first argument is negative zero and the second argument
331: * is positive, or the first argument is negative and finite and the
332: * second argument is positive infinity, then the result is negative zero.
333: * <li>If the first argument is positive zero and the second argument
334: * is negative, or the first argument is positive and finite and the
335: * second argument is negative infinity, then the result is the
336: * <code>double</code> value closest to <i>pi</i>.
337: * <li>If the first argument is negative zero and the second argument
338: * is negative, or the first argument is negative and finite and the
339: * second argument is negative infinity, then the result is the
340: * <code>double</code> value closest to -<i>pi</i>.
341: * <li>If the first argument is positive and the second argument is
342: * positive zero or negative zero, or the first argument is positive
343: * infinity and the second argument is finite, then the result is the
344: * <code>double</code> value closest to <i>pi</i>/2.
345: * <li>If the first argument is negative and the second argument is
346: * positive zero or negative zero, or the first argument is negative
347: * infinity and the second argument is finite, then the result is the
348: * <code>double</code> value closest to -<i>pi</i>/2.
349: * <li>If both arguments are positive infinity, then the result is the
350: * <code>double</code> value closest to <i>pi</i>/4.
351: * <li>If the first argument is positive infinity and the second argument
352: * is negative infinity, then the result is the <code>double</code>
353: * value closest to 3*<i>pi</i>/4.
354: * <li>If the first argument is negative infinity and the second argument
355: * is positive infinity, then the result is the <code>double</code> value
356: * closest to -<i>pi</i>/4.
357: * <li>If both arguments are negative infinity, then the result is the
358: * <code>double</code> value closest to -3*<i>pi</i>/4.</ul>
359: *
360: * @param y the ordinate coordinate
361: * @param x the abscissa coordinate
362: * @return the <i>theta</i> component of the point
363: * (<i>r</i>, <i>theta</i>)
364: * in polar coordinates that corresponds to the point
365: * (<i>x</i>, <i>y</i>) in Cartesian coordinates.
366: */
367: public static native double atan2(double y, double x);
368:
369: /**
370: * Returns the value of the first argument raised to the power of the
371: * second argument. Special cases:
372: *
373: * <ul><li>If the second argument is positive or negative zero, then the
374: * result is 1.0.
375: * <li>If the second argument is 1.0, then the result is the same as the
376: * first argument.
377: * <li>If the second argument is NaN, then the result is NaN.
378: * <li>If the first argument is NaN and the second argument is nonzero,
379: * then the result is NaN.
380: *
381: * <li>If
382: * <ul>
383: * <li>the absolute value of the first argument is greater than 1
384: * and the second argument is positive infinity, or
385: * <li>the absolute value of the first argument is less than 1 and
386: * the second argument is negative infinity,
387: * </ul>
388: * then the result is positive infinity.
389: *
390: * <li>If
391: * <ul>
392: * <li>the absolute value of the first argument is greater than 1 and
393: * the second argument is negative infinity, or
394: * <li>the absolute value of the
395: * first argument is less than 1 and the second argument is positive
396: * infinity,
397: * </ul>
398: * then the result is positive zero.
399: *
400: * <li>If the absolute value of the first argument equals 1 and the
401: * second argument is infinite, then the result is NaN.
402: *
403: * <li>If
404: * <ul>
405: * <li>the first argument is positive zero and the second argument
406: * is greater than zero, or
407: * <li>the first argument is positive infinity and the second
408: * argument is less than zero,
409: * </ul>
410: * then the result is positive zero.
411: *
412: * <li>If
413: * <ul>
414: * <li>the first argument is positive zero and the second argument
415: * is less than zero, or
416: * <li>the first argument is positive infinity and the second
417: * argument is greater than zero,
418: * </ul>
419: * then the result is positive infinity.
420: *
421: * <li>If
422: * <ul>
423: * <li>the first argument is negative zero and the second argument
424: * is greater than zero but not a finite odd integer, or
425: * <li>the first argument is negative infinity and the second
426: * argument is less than zero but not a finite odd integer,
427: * </ul>
428: * then the result is positive zero.
429: *
430: * <li>If
431: * <ul>
432: * <li>the first argument is negative zero and the second argument
433: * is a positive finite odd integer, or
434: * <li>the first argument is negative infinity and the second
435: * argument is a negative finite odd integer,
436: * </ul>
437: * then the result is negative zero.
438: *
439: * <li>If
440: * <ul>
441: * <li>the first argument is negative zero and the second argument
442: * is less than zero but not a finite odd integer, or
443: * <li>the first argument is negative infinity and the second
444: * argument is greater than zero but not a finite odd integer,
445: * </ul>
446: * then the result is positive infinity.
447: *
448: * <li>If
449: * <ul>
450: * <li>the first argument is negative zero and the second argument
451: * is a negative finite odd integer, or
452: * <li>the first argument is negative infinity and the second
453: * argument is a positive finite odd integer,
454: * </ul>
455: * then the result is negative infinity.
456: *
457: * <li>If the first argument is finite and less than zero
458: * <ul>
459: * <li> if the second argument is a finite even integer, the
460: * result is equal to the result of raising the absolute value of
461: * the first argument to the power of the second argument
462: *
463: * <li>if the second argument is a finite odd integer, the result
464: * is equal to the negative of the result of raising the absolute
465: * value of the first argument to the power of the second
466: * argument
467: *
468: * <li>if the second argument is finite and not an integer, then
469: * the result is NaN.
470: * </ul>
471: *
472: * <li>If both arguments are integers, then the result is exactly equal
473: * to the mathematical result of raising the first argument to the power
474: * of the second argument if that result can in fact be represented
475: * exactly as a <code>double</code> value.</ul>
476: *
477: * <p>(In the foregoing descriptions, a floating-point value is
478: * considered to be an integer if and only if it is finite and a
479: * fixed point of the method {@link #ceil <tt>ceil</tt>} or,
480: * equivalently, a fixed point of the method {@link #floor
481: * <tt>floor</tt>}. A value is a fixed point of a one-argument
482: * method if and only if the result of applying the method to the
483: * value is equal to the value.)
484: *
485: * @param a base.
486: * @param b the exponent.
487: * @return the value <code>a<sup>b</sup></code>.
488: */
489: public static native double pow(double a, double b);
490:
491: /**
492: * Returns the closest <code>int</code> to the argument. The
493: * result is rounded to an integer by adding 1/2, taking the
494: * floor of the result, and casting the result to type <code>int</code>.
495: * In other words, the result is equal to the value of the expression:
496: * <p><pre>(int)Math.floor(a + 0.5f)</pre>
497: * <p>
498: * Special cases:
499: * <ul><li>If the argument is NaN, the result is 0.
500: * <li>If the argument is negative infinity or any value less than or
501: * equal to the value of <code>Integer.MIN_VALUE</code>, the result is
502: * equal to the value of <code>Integer.MIN_VALUE</code>.
503: * <li>If the argument is positive infinity or any value greater than or
504: * equal to the value of <code>Integer.MAX_VALUE</code>, the result is
505: * equal to the value of <code>Integer.MAX_VALUE</code>.</ul>
506: *
507: * @param a a floating-point value to be rounded to an integer.
508: * @return the value of the argument rounded to the nearest
509: * <code>int</code> value.
510: * @see java.lang.Integer#MAX_VALUE
511: * @see java.lang.Integer#MIN_VALUE
512: */
513: public static int round(float a) {
514: return (int) floor(a + 0.5f);
515: }
516:
517: /**
518: * Returns the closest <code>long</code> to the argument. The result
519: * is rounded to an integer by adding 1/2, taking the floor of the
520: * result, and casting the result to type <code>long</code>. In other
521: * words, the result is equal to the value of the expression:
522: * <p><pre>(long)Math.floor(a + 0.5d)</pre>
523: * <p>
524: * Special cases:
525: * <ul><li>If the argument is NaN, the result is 0.
526: * <li>If the argument is negative infinity or any value less than or
527: * equal to the value of <code>Long.MIN_VALUE</code>, the result is
528: * equal to the value of <code>Long.MIN_VALUE</code>.
529: * <li>If the argument is positive infinity or any value greater than or
530: * equal to the value of <code>Long.MAX_VALUE</code>, the result is
531: * equal to the value of <code>Long.MAX_VALUE</code>.</ul>
532: *
533: * @param a a floating-point value to be rounded to a
534: * <code>long</code>.
535: * @return the value of the argument rounded to the nearest
536: * <code>long</code> value.
537: * @see java.lang.Long#MAX_VALUE
538: * @see java.lang.Long#MIN_VALUE
539: */
540: public static long round(double a) {
541: return (long) floor(a + 0.5d);
542: }
543:
544: private static Random randomNumberGenerator;
545:
546: private static synchronized void initRNG() {
547: if (randomNumberGenerator == null)
548: randomNumberGenerator = new Random();
549: }
550:
551: /**
552: * Returns a <code>double</code> value with a positive sign, greater
553: * than or equal to <code>0.0</code> and less than <code>1.0</code>.
554: * Returned values are chosen pseudorandomly with (approximately)
555: * uniform distribution from that range.
556: * <p>
557: * When this method is first called, it creates a single new
558: * pseudorandom-number generator, exactly as if by the expression
559: * <blockquote><pre>new java.util.Random</pre></blockquote>
560: * This new pseudorandom-number generator is used thereafter for all
561: * calls to this method and is used nowhere else.
562: * <p>
563: * This method is properly synchronized to allow correct use by more
564: * than one thread. However, if many threads need to generate
565: * pseudorandom numbers at a great rate, it may reduce contention for
566: * each thread to have its own pseudorandom number generator.
567: *
568: * @return a pseudorandom <code>double</code> greater than or equal
569: * to <code>0.0</code> and less than <code>1.0</code>.
570: * @see java.util.Random#nextDouble()
571: */
572: public static double random() {
573: if (randomNumberGenerator == null)
574: initRNG();
575: return randomNumberGenerator.nextDouble();
576: }
577:
578: /**
579: * Returns the absolute value of an <code>int</code> value..
580: * If the argument is not negative, the argument is returned.
581: * If the argument is negative, the negation of the argument is returned.
582: * <p>
583: * Note that if the argument is equal to the value of
584: * <code>Integer.MIN_VALUE</code>, the most negative representable
585: * <code>int</code> value, the result is that same value, which is
586: * negative.
587: *
588: * @param a the argument whose absolute value is to be determined.
589: * @return the absolute value of the argument.
590: * @see java.lang.Integer#MIN_VALUE
591: */
592: public static int abs(int a) {
593: return (a < 0) ? -a : a;
594: }
595:
596: /**
597: * Returns the absolute value of a <code>long</code> value.
598: * If the argument is not negative, the argument is returned.
599: * If the argument is negative, the negation of the argument is returned.
600: * <p>
601: * Note that if the argument is equal to the value of
602: * <code>Long.MIN_VALUE</code>, the most negative representable
603: * <code>long</code> value, the result is that same value, which is
604: * negative.
605: *
606: * @param a the argument whose absolute value is to be determined.
607: * @return the absolute value of the argument.
608: * @see java.lang.Long#MIN_VALUE
609: */
610: public static long abs(long a) {
611: return (a < 0) ? -a : a;
612: }
613:
614: /**
615: * Returns the absolute value of a <code>float</code> value.
616: * If the argument is not negative, the argument is returned.
617: * If the argument is negative, the negation of the argument is returned.
618: * Special cases:
619: * <ul><li>If the argument is positive zero or negative zero, the
620: * result is positive zero.
621: * <li>If the argument is infinite, the result is positive infinity.
622: * <li>If the argument is NaN, the result is NaN.</ul>
623: * In other words, the result is the same as the value of the expression:
624: * <p><pre>Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))</pre>
625: *
626: * @param a the argument whose absolute value is to be determined
627: * @return the absolute value of the argument.
628: */
629: public static float abs(float a) {
630: return (a <= 0.0F) ? 0.0F - a : a;
631: }
632:
633: /**
634: * Returns the absolute value of a <code>double</code> value.
635: * If the argument is not negative, the argument is returned.
636: * If the argument is negative, the negation of the argument is returned.
637: * Special cases:
638: * <ul><li>If the argument is positive zero or negative zero, the result
639: * is positive zero.
640: * <li>If the argument is infinite, the result is positive infinity.
641: * <li>If the argument is NaN, the result is NaN.</ul>
642: * In other words, the result is the same as the value of the expression:
643: * <p><code>Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)</code>
644: *
645: * @param a the argument whose absolute value is to be determined
646: * @return the absolute value of the argument.
647: */
648: public static double abs(double a) {
649: return (a <= 0.0D) ? 0.0D - a : a;
650: }
651:
652: /**
653: * Returns the greater of two <code>int</code> values. That is, the
654: * result is the argument closer to the value of
655: * <code>Integer.MAX_VALUE</code>. If the arguments have the same value,
656: * the result is that same value.
657: *
658: * @param a an argument.
659: * @param b another argument.
660: * @return the larger of <code>a</code> and <code>b</code>.
661: * @see java.lang.Long#MAX_VALUE
662: */
663: public static int max(int a, int b) {
664: return (a >= b) ? a : b;
665: }
666:
667: /**
668: * Returns the greater of two <code>long</code> values. That is, the
669: * result is the argument closer to the value of
670: * <code>Long.MAX_VALUE</code>. If the arguments have the same value,
671: * the result is that same value.
672: *
673: * @param a an argument.
674: * @param b another argument.
675: * @return the larger of <code>a</code> and <code>b</code>.
676: * @see java.lang.Long#MAX_VALUE
677: */
678: public static long max(long a, long b) {
679: return (a >= b) ? a : b;
680: }
681:
682: private static long negativeZeroFloatBits = Float
683: .floatToIntBits(-0.0f);
684: private static long negativeZeroDoubleBits = Double
685: .doubleToLongBits(-0.0d);
686:
687: /**
688: * Returns the greater of two <code>float</code> values. That is,
689: * the result is the argument closer to positive infinity. If the
690: * arguments have the same value, the result is that same
691: * value. If either value is NaN, then the result is NaN. Unlike
692: * the the numerical comparison operators, this method considers
693: * negative zero to be strictly smaller than positive zero. If one
694: * argument is positive zero and the other negative zero, the
695: * result is positive zero.
696: *
697: * @param a an argument.
698: * @param b another argument.
699: * @return the larger of <code>a</code> and <code>b</code>.
700: */
701: public static float max(float a, float b) {
702: if (a != a)
703: return a; // a is NaN
704: if ((a == 0.0f) && (b == 0.0f)
705: && (Float.floatToIntBits(a) == negativeZeroFloatBits)) {
706: return b;
707: }
708: return (a >= b) ? a : b;
709: }
710:
711: /**
712: * Returns the greater of two <code>double</code> values. That
713: * is, the result is the argument closer to positive infinity. If
714: * the arguments have the same value, the result is that same
715: * value. If either value is NaN, then the result is NaN. Unlike
716: * the the numerical comparison operators, this method considers
717: * negative zero to be strictly smaller than positive zero. If one
718: * argument is positive zero and the other negative zero, the
719: * result is positive zero.
720: *
721: * @param a an argument.
722: * @param b another argument.
723: * @return the larger of <code>a</code> and <code>b</code>.
724: */
725: public static double max(double a, double b) {
726: if (a != a)
727: return a; // a is NaN
728: if ((a == 0.0d)
729: && (b == 0.0d)
730: && (Double.doubleToLongBits(a) == negativeZeroDoubleBits)) {
731: return b;
732: }
733: return (a >= b) ? a : b;
734: }
735:
736: /**
737: * Returns the smaller of two <code>int</code> values. That is,
738: * the result the argument closer to the value of
739: * <code>Integer.MIN_VALUE</code>. If the arguments have the same
740: * value, the result is that same value.
741: *
742: * @param a an argument.
743: * @param b another argument.
744: * @return the smaller of <code>a</code> and <code>b</code>.
745: * @see java.lang.Long#MIN_VALUE
746: */
747: public static int min(int a, int b) {
748: return (a <= b) ? a : b;
749: }
750:
751: /**
752: * Returns the smaller of two <code>long</code> values. That is,
753: * the result is the argument closer to the value of
754: * <code>Long.MIN_VALUE</code>. If the arguments have the same
755: * value, the result is that same value.
756: *
757: * @param a an argument.
758: * @param b another argument.
759: * @return the smaller of <code>a</code> and <code>b</code>.
760: * @see java.lang.Long#MIN_VALUE
761: */
762: public static long min(long a, long b) {
763: return (a <= b) ? a : b;
764: }
765:
766: /**
767: * Returns the smaller of two <code>float</code> values. That is,
768: * the result is the value closer to negative infinity. If the
769: * arguments have the same value, the result is that same
770: * value. If either value is NaN, then the result is NaN. Unlike
771: * the the numerical comparison operators, this method considers
772: * negative zero to be strictly smaller than positive zero. If
773: * one argument is positive zero and the other is negative zero,
774: * the result is negative zero.
775: *
776: * @param a an argument.
777: * @param b another argument.
778: * @return the smaller of <code>a</code> and <code>b.</code>
779: */
780: public static float min(float a, float b) {
781: if (a != a)
782: return a; // a is NaN
783: if ((a == 0.0f) && (b == 0.0f)
784: && (Float.floatToIntBits(b) == negativeZeroFloatBits)) {
785: return b;
786: }
787: return (a <= b) ? a : b;
788: }
789:
790: /**
791: * Returns the smaller of two <code>double</code> values. That
792: * is, the result is the value closer to negative infinity. If the
793: * arguments have the same value, the result is that same
794: * value. If either value is NaN, then the result is NaN. Unlike
795: * the the numerical comparison operators, this method considers
796: * negative zero to be strictly smaller than positive zero. If one
797: * argument is positive zero and the other is negative zero, the
798: * result is negative zero.
799: *
800: * @param a an argument.
801: * @param b another argument.
802: * @return the smaller of <code>a</code> and <code>b</code>.
803: */
804: public static double min(double a, double b) {
805: if (a != a)
806: return a; // a is NaN
807: if ((a == 0.0d)
808: && (b == 0.0d)
809: && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
810: return b;
811: }
812: return (a <= b) ? a : b;
813: }
814:
815: }
|