2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
29 * The class {@code StrictMath} contains methods for performing basic
30 * numeric operations such as the elementary exponential, logarithm,
31 * square root, and trigonometric functions.
33 * <p>To help ensure portability of Java programs, the definitions of
34 * some of the numeric functions in this package require that they
35 * produce the same results as certain published algorithms. These
36 * algorithms are available from the well-known network library
37 * {@code netlib} as the package "Freely Distributable Math
39 * href="ftp://ftp.netlib.org/fdlibm.tar">{@code fdlibm}</a>. These
40 * algorithms, which are written in the C programming language, are
41 * then to be understood as executed with all floating-point
42 * operations following the rules of Java floating-point arithmetic.
44 * <p>The Java math library is defined with respect to
45 * {@code fdlibm} version 5.3. Where {@code fdlibm} provides
46 * more than one definition for a function (such as
47 * {@code acos}), use the "IEEE 754 core function" version
48 * (residing in a file whose name begins with the letter
49 * {@code e}). The methods which require {@code fdlibm}
50 * semantics are {@code sin}, {@code cos}, {@code tan},
51 * {@code asin}, {@code acos}, {@code atan},
52 * {@code exp}, {@code log}, {@code log10},
53 * {@code cbrt}, {@code atan2}, {@code pow},
54 * {@code sinh}, {@code cosh}, {@code tanh},
55 * {@code hypot}, {@code expm1}, and {@code log1p}.
58 * @author Joseph D. Darcy
62 public final class StrictMath {
65 * Don't let anyone instantiate this class.
67 private StrictMath() {}
70 * The {@code double} value that is closer than any other to
71 * <i>e</i>, the base of the natural logarithms.
73 public static final double E = 2.7182818284590452354;
76 * The {@code double} value that is closer than any other to
77 * <i>pi</i>, the ratio of the circumference of a circle to its
80 public static final double PI = 3.14159265358979323846;
83 * Returns the trigonometric sine of an angle. Special cases:
84 * <ul><li>If the argument is NaN or an infinity, then the
86 * <li>If the argument is zero, then the result is a zero with the
87 * same sign as the argument.</ul>
89 * @param a an angle, in radians.
90 * @return the sine of the argument.
92 public static native double sin(double a);
95 * Returns the trigonometric cosine of an angle. Special cases:
96 * <ul><li>If the argument is NaN or an infinity, then the
99 * @param a an angle, in radians.
100 * @return the cosine of the argument.
102 public static native double cos(double a);
105 * Returns the trigonometric tangent of an angle. Special cases:
106 * <ul><li>If the argument is NaN or an infinity, then the result
108 * <li>If the argument is zero, then the result is a zero with the
109 * same sign as the argument.</ul>
111 * @param a an angle, in radians.
112 * @return the tangent of the argument.
114 public static native double tan(double a);
117 * Returns the arc sine of a value; the returned angle is in the
118 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases:
119 * <ul><li>If the argument is NaN or its absolute value is greater
120 * than 1, then the result is NaN.
121 * <li>If the argument is zero, then the result is a zero with the
122 * same sign as the argument.</ul>
124 * @param a the value whose arc sine is to be returned.
125 * @return the arc sine of the argument.
127 public static native double asin(double a);
130 * Returns the arc cosine of a value; the returned angle is in the
131 * range 0.0 through <i>pi</i>. Special case:
132 * <ul><li>If the argument is NaN or its absolute value is greater
133 * than 1, then the result is NaN.</ul>
135 * @param a the value whose arc cosine is to be returned.
136 * @return the arc cosine of the argument.
138 public static native double acos(double a);
141 * Returns the arc tangent of a value; the returned angle is in the
142 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases:
143 * <ul><li>If the argument is NaN, then the result is NaN.
144 * <li>If the argument is zero, then the result is a zero with the
145 * same sign as the argument.</ul>
147 * @param a the value whose arc tangent is to be returned.
148 * @return the arc tangent of the argument.
150 public static native double atan(double a);
153 * Converts an angle measured in degrees to an approximately
154 * equivalent angle measured in radians. The conversion from
155 * degrees to radians is generally inexact.
157 * @param angdeg an angle, in degrees
158 * @return the measurement of the angle {@code angdeg}
161 public static strictfp double toRadians(double angdeg) {
162 return angdeg / 180.0 * PI;
166 * Converts an angle measured in radians to an approximately
167 * equivalent angle measured in degrees. The conversion from
168 * radians to degrees is generally inexact; users should
169 * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
172 * @param angrad an angle, in radians
173 * @return the measurement of the angle {@code angrad}
176 public static strictfp double toDegrees(double angrad) {
177 return angrad * 180.0 / PI;
181 * Returns Euler's number <i>e</i> raised to the power of a
182 * {@code double} value. Special cases:
183 * <ul><li>If the argument is NaN, the result is NaN.
184 * <li>If the argument is positive infinity, then the result is
186 * <li>If the argument is negative infinity, then the result is
187 * positive zero.</ul>
189 * @param a the exponent to raise <i>e</i> to.
190 * @return the value <i>e</i><sup>{@code a}</sup>,
191 * where <i>e</i> is the base of the natural logarithms.
193 public static native double exp(double a);
196 * Returns the natural logarithm (base <i>e</i>) of a {@code double}
197 * value. Special cases:
198 * <ul><li>If the argument is NaN or less than zero, then the result
200 * <li>If the argument is positive infinity, then the result is
202 * <li>If the argument is positive zero or negative zero, then the
203 * result is negative infinity.</ul>
206 * @return the value ln {@code a}, the natural logarithm of
209 public static native double log(double a);
213 * Returns the base 10 logarithm of a {@code double} value.
216 * <ul><li>If the argument is NaN or less than zero, then the result
218 * <li>If the argument is positive infinity, then the result is
220 * <li>If the argument is positive zero or negative zero, then the
221 * result is negative infinity.
222 * <li> If the argument is equal to 10<sup><i>n</i></sup> for
223 * integer <i>n</i>, then the result is <i>n</i>.
227 * @return the base 10 logarithm of {@code a}.
230 public static native double log10(double a);
233 * Returns the correctly rounded positive square root of a
234 * {@code double} value.
236 * <ul><li>If the argument is NaN or less than zero, then the result
238 * <li>If the argument is positive infinity, then the result is positive
240 * <li>If the argument is positive zero or negative zero, then the
241 * result is the same as the argument.</ul>
242 * Otherwise, the result is the {@code double} value closest to
243 * the true mathematical square root of the argument value.
246 * @return the positive square root of {@code a}.
248 public static native double sqrt(double a);
251 * Returns the cube root of a {@code double} value. For
252 * positive finite {@code x}, {@code cbrt(-x) ==
253 * -cbrt(x)}; that is, the cube root of a negative value is
254 * the negative of the cube root of that value's magnitude.
259 * <li>If the argument is NaN, then the result is NaN.
261 * <li>If the argument is infinite, then the result is an infinity
262 * with the same sign as the argument.
264 * <li>If the argument is zero, then the result is a zero with the
265 * same sign as the argument.
270 * @return the cube root of {@code a}.
273 public static native double cbrt(double a);
276 * Computes the remainder operation on two arguments as prescribed
277 * by the IEEE 754 standard.
278 * The remainder value is mathematically equal to
279 * <code>f1 - f2</code> × <i>n</i>,
280 * where <i>n</i> is the mathematical integer closest to the exact
281 * mathematical value of the quotient {@code f1/f2}, and if two
282 * mathematical integers are equally close to {@code f1/f2},
283 * then <i>n</i> is the integer that is even. If the remainder is
284 * zero, its sign is the same as the sign of the first argument.
286 * <ul><li>If either argument is NaN, or the first argument is infinite,
287 * or the second argument is positive zero or negative zero, then the
289 * <li>If the first argument is finite and the second argument is
290 * infinite, then the result is the same as the first argument.</ul>
292 * @param f1 the dividend.
293 * @param f2 the divisor.
294 * @return the remainder when {@code f1} is divided by
297 public static native double IEEEremainder(double f1, double f2);
300 * Returns the smallest (closest to negative infinity)
301 * {@code double} value that is greater than or equal to the
302 * argument and is equal to a mathematical integer. Special cases:
303 * <ul><li>If the argument value is already equal to a
304 * mathematical integer, then the result is the same as the
305 * argument. <li>If the argument is NaN or an infinity or
306 * positive zero or negative zero, then the result is the same as
307 * the argument. <li>If the argument value is less than zero but
308 * greater than -1.0, then the result is negative zero.</ul> Note
309 * that the value of {@code StrictMath.ceil(x)} is exactly the
310 * value of {@code -StrictMath.floor(-x)}.
313 * @return the smallest (closest to negative infinity)
314 * floating-point value that is greater than or equal to
315 * the argument and is equal to a mathematical integer.
317 public static double ceil(double a) {
318 return floorOrCeil(a, -0.0, 1.0, 1.0);
322 * Returns the largest (closest to positive infinity)
323 * {@code double} value that is less than or equal to the
324 * argument and is equal to a mathematical integer. Special cases:
325 * <ul><li>If the argument value is already equal to a
326 * mathematical integer, then the result is the same as the
327 * argument. <li>If the argument is NaN or an infinity or
328 * positive zero or negative zero, then the result is the same as
332 * @return the largest (closest to positive infinity)
333 * floating-point value that less than or equal to the argument
334 * and is equal to a mathematical integer.
336 public static double floor(double a) {
337 return floorOrCeil(a, -1.0, 0.0, -1.0);
341 * Internal method to share logic between floor and ceil.
343 * @param a the value to be floored or ceiled
344 * @param negativeBoundary result for values in (-1, 0)
345 * @param positiveBoundary result for values in (0, 1)
346 * @param increment value to add when the argument is non-integral
348 private static double floorOrCeil(double a,
349 double negativeBoundary,
350 double positiveBoundary,
352 int exponent = getExponent(a);
356 * Absolute value of argument is less than 1.
357 * floorOrceil(-0.0) => -0.0
358 * floorOrceil(+0.0) => +0.0
360 return ((a == 0.0) ? a :
361 ( (a < 0.0) ? negativeBoundary : positiveBoundary) );
362 } else if (exponent >= 52) {
364 * Infinity, NaN, or a value so large it must be integral.
368 // Else the argument is either an integral value already XOR it
369 // has to be rounded to one.
370 assert exponent >= 0 && exponent <= 51;
372 long doppel = Double.doubleToRawLongBits(a);
373 long mask = 0; // DoubleConsts.SIGNIF_BIT_MASK >> exponent;
375 if ( (mask & doppel) == 0L )
376 return a; // integral value
378 double result = Double.longBitsToDouble(doppel & (~mask));
380 result = result + sign;
386 * Returns the {@code double} value that is closest in value
387 * to the argument and is equal to a mathematical integer. If two
388 * {@code double} values that are mathematical integers are
389 * equally close to the value of the argument, the result is the
390 * integer value that is even. Special cases:
391 * <ul><li>If the argument value is already equal to a mathematical
392 * integer, then the result is the same as the argument.
393 * <li>If the argument is NaN or an infinity or positive zero or negative
394 * zero, then the result is the same as the argument.</ul>
397 * @return the closest floating-point value to {@code a} that is
398 * equal to a mathematical integer.
399 * @author Joseph D. Darcy
401 public static double rint(double a) {
402 throw new UnsupportedOperationException();
404 * If the absolute value of a is not less than 2^52, it
405 * is either a finite integer (the double format does not have
406 * enough significand bits for a number that large to have any
407 * fractional portion), an infinity, or a NaN. In any of
408 * these cases, rint of the argument is the argument.
410 * Otherwise, the sum (twoToThe52 + a ) will properly round
411 * away any fractional portion of a since ulp(twoToThe52) ==
412 * 1.0; subtracting out twoToThe52 from this sum will then be
413 * exact and leave the rounded integer portion of a.
415 * This method does *not* need to be declared strictfp to get
416 * fully reproducible results. Whether or not a method is
417 * declared strictfp can only make a difference in the
418 * returned result if some operation would overflow or
419 * underflow with strictfp semantics. The operation
420 * (twoToThe52 + a ) cannot overflow since large values of a
421 * are screened out; the add cannot underflow since twoToThe52
422 * is too large. The subtraction ((twoToThe52 + a ) -
423 * twoToThe52) will be exact as discussed above and thus
424 * cannot overflow or meaningfully underflow. Finally, the
425 * last multiply in the return statement is by plus or minus
426 * 1.0, which is exact too.
428 // double twoToThe52 = (double)(1L << 52); // 2^52
429 // double sign = FpUtils.rawCopySign(1.0, a); // preserve sign info
432 // if (a < twoToThe52) { // E_min <= ilogb(a) <= 51
433 // a = ((twoToThe52 + a ) - twoToThe52);
436 // return sign * a; // restore original sign
440 * Returns the angle <i>theta</i> from the conversion of rectangular
441 * coordinates ({@code x}, {@code y}) to polar
442 * coordinates (r, <i>theta</i>).
443 * This method computes the phase <i>theta</i> by computing an arc tangent
444 * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special
446 * <ul><li>If either argument is NaN, then the result is NaN.
447 * <li>If the first argument is positive zero and the second argument
448 * is positive, or the first argument is positive and finite and the
449 * second argument is positive infinity, then the result is positive
451 * <li>If the first argument is negative zero and the second argument
452 * is positive, or the first argument is negative and finite and the
453 * second argument is positive infinity, then the result is negative zero.
454 * <li>If the first argument is positive zero and the second argument
455 * is negative, or the first argument is positive and finite and the
456 * second argument is negative infinity, then the result is the
457 * {@code double} value closest to <i>pi</i>.
458 * <li>If the first argument is negative zero and the second argument
459 * is negative, or the first argument is negative and finite and the
460 * second argument is negative infinity, then the result is the
461 * {@code double} value closest to -<i>pi</i>.
462 * <li>If the first argument is positive and the second argument is
463 * positive zero or negative zero, or the first argument is positive
464 * infinity and the second argument is finite, then the result is the
465 * {@code double} value closest to <i>pi</i>/2.
466 * <li>If the first argument is negative and the second argument is
467 * positive zero or negative zero, or the first argument is negative
468 * infinity and the second argument is finite, then the result is the
469 * {@code double} value closest to -<i>pi</i>/2.
470 * <li>If both arguments are positive infinity, then the result is the
471 * {@code double} value closest to <i>pi</i>/4.
472 * <li>If the first argument is positive infinity and the second argument
473 * is negative infinity, then the result is the {@code double}
474 * value closest to 3*<i>pi</i>/4.
475 * <li>If the first argument is negative infinity and the second argument
476 * is positive infinity, then the result is the {@code double} value
477 * closest to -<i>pi</i>/4.
478 * <li>If both arguments are negative infinity, then the result is the
479 * {@code double} value closest to -3*<i>pi</i>/4.</ul>
481 * @param y the ordinate coordinate
482 * @param x the abscissa coordinate
483 * @return the <i>theta</i> component of the point
484 * (<i>r</i>, <i>theta</i>)
485 * in polar coordinates that corresponds to the point
486 * (<i>x</i>, <i>y</i>) in Cartesian coordinates.
488 public static native double atan2(double y, double x);
492 * Returns the value of the first argument raised to the power of the
493 * second argument. Special cases:
495 * <ul><li>If the second argument is positive or negative zero, then the
497 * <li>If the second argument is 1.0, then the result is the same as the
499 * <li>If the second argument is NaN, then the result is NaN.
500 * <li>If the first argument is NaN and the second argument is nonzero,
501 * then the result is NaN.
505 * <li>the absolute value of the first argument is greater than 1
506 * and the second argument is positive infinity, or
507 * <li>the absolute value of the first argument is less than 1 and
508 * the second argument is negative infinity,
510 * then the result is positive infinity.
514 * <li>the absolute value of the first argument is greater than 1 and
515 * the second argument is negative infinity, or
516 * <li>the absolute value of the
517 * first argument is less than 1 and the second argument is positive
520 * then the result is positive zero.
522 * <li>If the absolute value of the first argument equals 1 and the
523 * second argument is infinite, then the result is NaN.
527 * <li>the first argument is positive zero and the second argument
528 * is greater than zero, or
529 * <li>the first argument is positive infinity and the second
530 * argument is less than zero,
532 * then the result is positive zero.
536 * <li>the first argument is positive zero and the second argument
537 * is less than zero, or
538 * <li>the first argument is positive infinity and the second
539 * argument is greater than zero,
541 * then the result is positive infinity.
545 * <li>the first argument is negative zero and the second argument
546 * is greater than zero but not a finite odd integer, or
547 * <li>the first argument is negative infinity and the second
548 * argument is less than zero but not a finite odd integer,
550 * then the result is positive zero.
554 * <li>the first argument is negative zero and the second argument
555 * is a positive finite odd integer, or
556 * <li>the first argument is negative infinity and the second
557 * argument is a negative finite odd integer,
559 * then the result is negative zero.
563 * <li>the first argument is negative zero and the second argument
564 * is less than zero but not a finite odd integer, or
565 * <li>the first argument is negative infinity and the second
566 * argument is greater than zero but not a finite odd integer,
568 * then the result is positive infinity.
572 * <li>the first argument is negative zero and the second argument
573 * is a negative finite odd integer, or
574 * <li>the first argument is negative infinity and the second
575 * argument is a positive finite odd integer,
577 * then the result is negative infinity.
579 * <li>If the first argument is finite and less than zero
581 * <li> if the second argument is a finite even integer, the
582 * result is equal to the result of raising the absolute value of
583 * the first argument to the power of the second argument
585 * <li>if the second argument is a finite odd integer, the result
586 * is equal to the negative of the result of raising the absolute
587 * value of the first argument to the power of the second
590 * <li>if the second argument is finite and not an integer, then
594 * <li>If both arguments are integers, then the result is exactly equal
595 * to the mathematical result of raising the first argument to the power
596 * of the second argument if that result can in fact be represented
597 * exactly as a {@code double} value.</ul>
599 * <p>(In the foregoing descriptions, a floating-point value is
600 * considered to be an integer if and only if it is finite and a
601 * fixed point of the method {@link #ceil ceil} or,
602 * equivalently, a fixed point of the method {@link #floor
603 * floor}. A value is a fixed point of a one-argument
604 * method if and only if the result of applying the method to the
605 * value is equal to the value.)
608 * @param b the exponent.
609 * @return the value {@code a}<sup>{@code b}</sup>.
611 public static native double pow(double a, double b);
614 * Returns the closest {@code int} to the argument, with ties
618 * <ul><li>If the argument is NaN, the result is 0.
619 * <li>If the argument is negative infinity or any value less than or
620 * equal to the value of {@code Integer.MIN_VALUE}, the result is
621 * equal to the value of {@code Integer.MIN_VALUE}.
622 * <li>If the argument is positive infinity or any value greater than or
623 * equal to the value of {@code Integer.MAX_VALUE}, the result is
624 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
626 * @param a a floating-point value to be rounded to an integer.
627 * @return the value of the argument rounded to the nearest
629 * @see java.lang.Integer#MAX_VALUE
630 * @see java.lang.Integer#MIN_VALUE
632 public static int round(float a) {
633 return Math.round(a);
637 * Returns the closest {@code long} to the argument, with ties
641 * <ul><li>If the argument is NaN, the result is 0.
642 * <li>If the argument is negative infinity or any value less than or
643 * equal to the value of {@code Long.MIN_VALUE}, the result is
644 * equal to the value of {@code Long.MIN_VALUE}.
645 * <li>If the argument is positive infinity or any value greater than or
646 * equal to the value of {@code Long.MAX_VALUE}, the result is
647 * equal to the value of {@code Long.MAX_VALUE}.</ul>
649 * @param a a floating-point value to be rounded to a
651 * @return the value of the argument rounded to the nearest
652 * {@code long} value.
653 * @see java.lang.Long#MAX_VALUE
654 * @see java.lang.Long#MIN_VALUE
656 public static long round(double a) {
657 return Math.round(a);
661 * Returns a {@code double} value with a positive sign, greater
662 * than or equal to {@code 0.0} and less than {@code 1.0}.
663 * Returned values are chosen pseudorandomly with (approximately)
664 * uniform distribution from that range.
666 * <p>When this method is first called, it creates a single new
667 * pseudorandom-number generator, exactly as if by the expression
669 * <blockquote>{@code new java.util.Random()}</blockquote>
671 * This new pseudorandom-number generator is used thereafter for
672 * all calls to this method and is used nowhere else.
674 * <p>This method is properly synchronized to allow correct use by
675 * more than one thread. However, if many threads need to generate
676 * pseudorandom numbers at a great rate, it may reduce contention
677 * for each thread to have its own pseudorandom number generator.
679 * @return a pseudorandom {@code double} greater than or equal
680 * to {@code 0.0} and less than {@code 1.0}.
681 * @see Random#nextDouble()
683 public static double random() {
684 throw new UnsupportedOperationException();
688 * Returns the absolute value of an {@code int} value..
689 * If the argument is not negative, the argument is returned.
690 * If the argument is negative, the negation of the argument is returned.
692 * <p>Note that if the argument is equal to the value of
693 * {@link Integer#MIN_VALUE}, the most negative representable
694 * {@code int} value, the result is that same value, which is
697 * @param a the argument whose absolute value is to be determined.
698 * @return the absolute value of the argument.
700 public static int abs(int a) {
701 return (a < 0) ? -a : a;
705 * Returns the absolute value of a {@code long} value.
706 * If the argument is not negative, the argument is returned.
707 * If the argument is negative, the negation of the argument is returned.
709 * <p>Note that if the argument is equal to the value of
710 * {@link Long#MIN_VALUE}, the most negative representable
711 * {@code long} value, the result is that same value, which
714 * @param a the argument whose absolute value is to be determined.
715 * @return the absolute value of the argument.
717 public static long abs(long a) {
718 return (a < 0) ? -a : a;
722 * Returns the absolute value of a {@code float} value.
723 * If the argument is not negative, the argument is returned.
724 * If the argument is negative, the negation of the argument is returned.
726 * <ul><li>If the argument is positive zero or negative zero, the
727 * result is positive zero.
728 * <li>If the argument is infinite, the result is positive infinity.
729 * <li>If the argument is NaN, the result is NaN.</ul>
730 * In other words, the result is the same as the value of the expression:
731 * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))}
733 * @param a the argument whose absolute value is to be determined
734 * @return the absolute value of the argument.
736 public static float abs(float a) {
737 return (a <= 0.0F) ? 0.0F - a : a;
741 * Returns the absolute value of a {@code double} value.
742 * If the argument is not negative, the argument is returned.
743 * If the argument is negative, the negation of the argument is returned.
745 * <ul><li>If the argument is positive zero or negative zero, the result
747 * <li>If the argument is infinite, the result is positive infinity.
748 * <li>If the argument is NaN, the result is NaN.</ul>
749 * In other words, the result is the same as the value of the expression:
750 * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)}
752 * @param a the argument whose absolute value is to be determined
753 * @return the absolute value of the argument.
755 public static double abs(double a) {
756 return (a <= 0.0D) ? 0.0D - a : a;
760 * Returns the greater of two {@code int} values. That is, the
761 * result is the argument closer to the value of
762 * {@link Integer#MAX_VALUE}. If the arguments have the same value,
763 * the result is that same value.
765 * @param a an argument.
766 * @param b another argument.
767 * @return the larger of {@code a} and {@code b}.
769 public static int max(int a, int b) {
770 return (a >= b) ? a : b;
774 * Returns the greater of two {@code long} values. That is, the
775 * result is the argument closer to the value of
776 * {@link Long#MAX_VALUE}. If the arguments have the same value,
777 * the result is that same value.
779 * @param a an argument.
780 * @param b another argument.
781 * @return the larger of {@code a} and {@code b}.
783 public static long max(long a, long b) {
784 return (a >= b) ? a : b;
787 // Use raw bit-wise conversions on guaranteed non-NaN arguments.
788 private static long negativeZeroFloatBits = Float.floatToRawIntBits(-0.0f);
789 private static long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d);
792 * Returns the greater of two {@code float} values. That is,
793 * the result is the argument closer to positive infinity. If the
794 * arguments have the same value, the result is that same
795 * value. If either value is NaN, then the result is NaN. Unlike
796 * the numerical comparison operators, this method considers
797 * negative zero to be strictly smaller than positive zero. If one
798 * argument is positive zero and the other negative zero, the
799 * result is positive zero.
801 * @param a an argument.
802 * @param b another argument.
803 * @return the larger of {@code a} and {@code b}.
805 public static float max(float a, float b) {
807 return a; // a is NaN
810 (Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
811 // Raw conversion ok since NaN can't map to -0.0.
814 return (a >= b) ? a : b;
818 * Returns the greater of two {@code double} values. That
819 * is, the result is the argument closer to positive infinity. If
820 * the arguments have the same value, the result is that same
821 * value. If either value is NaN, then the result is NaN. Unlike
822 * the numerical comparison operators, this method considers
823 * negative zero to be strictly smaller than positive zero. If one
824 * argument is positive zero and the other negative zero, the
825 * result is positive zero.
827 * @param a an argument.
828 * @param b another argument.
829 * @return the larger of {@code a} and {@code b}.
831 public static double max(double a, double b) {
833 return a; // a is NaN
836 (Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
837 // Raw conversion ok since NaN can't map to -0.0.
840 return (a >= b) ? a : b;
844 * Returns the smaller of two {@code int} values. That is,
845 * the result the argument closer to the value of
846 * {@link Integer#MIN_VALUE}. If the arguments have the same
847 * value, the result is that same value.
849 * @param a an argument.
850 * @param b another argument.
851 * @return the smaller of {@code a} and {@code b}.
853 public static int min(int a, int b) {
854 return (a <= b) ? a : b;
858 * Returns the smaller of two {@code long} values. That is,
859 * the result is the argument closer to the value of
860 * {@link Long#MIN_VALUE}. If the arguments have the same
861 * value, the result is that same value.
863 * @param a an argument.
864 * @param b another argument.
865 * @return the smaller of {@code a} and {@code b}.
867 public static long min(long a, long b) {
868 return (a <= b) ? a : b;
872 * Returns the smaller of two {@code float} values. That is,
873 * the result is the value closer to negative infinity. If the
874 * arguments have the same value, the result is that same
875 * value. If either value is NaN, then the result is NaN. Unlike
876 * the numerical comparison operators, this method considers
877 * negative zero to be strictly smaller than positive zero. If
878 * one argument is positive zero and the other is negative zero,
879 * the result is negative zero.
881 * @param a an argument.
882 * @param b another argument.
883 * @return the smaller of {@code a} and {@code b.}
885 public static float min(float a, float b) {
887 return a; // a is NaN
890 (Float.floatToRawIntBits(b) == negativeZeroFloatBits)) {
891 // Raw conversion ok since NaN can't map to -0.0.
894 return (a <= b) ? a : b;
898 * Returns the smaller of two {@code double} values. That
899 * is, the result is the value closer to negative infinity. If the
900 * arguments have the same value, the result is that same
901 * value. If either value is NaN, then the result is NaN. Unlike
902 * the numerical comparison operators, this method considers
903 * negative zero to be strictly smaller than positive zero. If one
904 * argument is positive zero and the other is negative zero, the
905 * result is negative zero.
907 * @param a an argument.
908 * @param b another argument.
909 * @return the smaller of {@code a} and {@code b}.
911 public static double min(double a, double b) {
913 return a; // a is NaN
916 (Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) {
917 // Raw conversion ok since NaN can't map to -0.0.
920 return (a <= b) ? a : b;
924 * Returns the size of an ulp of the argument. An ulp of a
925 * {@code double} value is the positive distance between this
926 * floating-point value and the {@code double} value next
927 * larger in magnitude. Note that for non-NaN <i>x</i>,
928 * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
932 * <li> If the argument is NaN, then the result is NaN.
933 * <li> If the argument is positive or negative infinity, then the
934 * result is positive infinity.
935 * <li> If the argument is positive or negative zero, then the result is
936 * {@code Double.MIN_VALUE}.
937 * <li> If the argument is ±{@code Double.MAX_VALUE}, then
938 * the result is equal to 2<sup>971</sup>.
941 * @param d the floating-point value whose ulp is to be returned
942 * @return the size of an ulp of the argument
943 * @author Joseph D. Darcy
946 public static double ulp(double d) {
947 throw new UnsupportedOperationException();
951 * Returns the size of an ulp of the argument. An ulp of a
952 * {@code float} value is the positive distance between this
953 * floating-point value and the {@code float} value next
954 * larger in magnitude. Note that for non-NaN <i>x</i>,
955 * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
959 * <li> If the argument is NaN, then the result is NaN.
960 * <li> If the argument is positive or negative infinity, then the
961 * result is positive infinity.
962 * <li> If the argument is positive or negative zero, then the result is
963 * {@code Float.MIN_VALUE}.
964 * <li> If the argument is ±{@code Float.MAX_VALUE}, then
965 * the result is equal to 2<sup>104</sup>.
968 * @param f the floating-point value whose ulp is to be returned
969 * @return the size of an ulp of the argument
970 * @author Joseph D. Darcy
973 public static float ulp(float f) {
974 throw new UnsupportedOperationException();
978 * Returns the signum function of the argument; zero if the argument
979 * is zero, 1.0 if the argument is greater than zero, -1.0 if the
980 * argument is less than zero.
984 * <li> If the argument is NaN, then the result is NaN.
985 * <li> If the argument is positive zero or negative zero, then the
986 * result is the same as the argument.
989 * @param d the floating-point value whose signum is to be returned
990 * @return the signum function of the argument
991 * @author Joseph D. Darcy
994 public static double signum(double d) {
995 throw new UnsupportedOperationException();
999 * Returns the signum function of the argument; zero if the argument
1000 * is zero, 1.0f if the argument is greater than zero, -1.0f if the
1001 * argument is less than zero.
1005 * <li> If the argument is NaN, then the result is NaN.
1006 * <li> If the argument is positive zero or negative zero, then the
1007 * result is the same as the argument.
1010 * @param f the floating-point value whose signum is to be returned
1011 * @return the signum function of the argument
1012 * @author Joseph D. Darcy
1015 public static float signum(float f) {
1016 throw new UnsupportedOperationException();
1020 * Returns the hyperbolic sine of a {@code double} value.
1021 * The hyperbolic sine of <i>x</i> is defined to be
1022 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2
1023 * where <i>e</i> is {@linkplain Math#E Euler's number}.
1028 * <li>If the argument is NaN, then the result is NaN.
1030 * <li>If the argument is infinite, then the result is an infinity
1031 * with the same sign as the argument.
1033 * <li>If the argument is zero, then the result is a zero with the
1034 * same sign as the argument.
1038 * @param x The number whose hyperbolic sine is to be returned.
1039 * @return The hyperbolic sine of {@code x}.
1042 public static native double sinh(double x);
1045 * Returns the hyperbolic cosine of a {@code double} value.
1046 * The hyperbolic cosine of <i>x</i> is defined to be
1047 * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2
1048 * where <i>e</i> is {@linkplain Math#E Euler's number}.
1053 * <li>If the argument is NaN, then the result is NaN.
1055 * <li>If the argument is infinite, then the result is positive
1058 * <li>If the argument is zero, then the result is {@code 1.0}.
1062 * @param x The number whose hyperbolic cosine is to be returned.
1063 * @return The hyperbolic cosine of {@code x}.
1066 public static native double cosh(double x);
1069 * Returns the hyperbolic tangent of a {@code double} value.
1070 * The hyperbolic tangent of <i>x</i> is defined to be
1071 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>),
1072 * in other words, {@linkplain Math#sinh
1073 * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note
1074 * that the absolute value of the exact tanh is always less than
1080 * <li>If the argument is NaN, then the result is NaN.
1082 * <li>If the argument is zero, then the result is a zero with the
1083 * same sign as the argument.
1085 * <li>If the argument is positive infinity, then the result is
1088 * <li>If the argument is negative infinity, then the result is
1093 * @param x The number whose hyperbolic tangent is to be returned.
1094 * @return The hyperbolic tangent of {@code x}.
1097 public static native double tanh(double x);
1100 * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
1101 * without intermediate overflow or underflow.
1106 * <li> If either argument is infinite, then the result
1107 * is positive infinity.
1109 * <li> If either argument is NaN and neither argument is infinite,
1110 * then the result is NaN.
1116 * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
1117 * without intermediate overflow or underflow
1120 public static native double hypot(double x, double y);
1123 * Returns <i>e</i><sup>x</sup> -1. Note that for values of
1124 * <i>x</i> near 0, the exact sum of
1125 * {@code expm1(x)} + 1 is much closer to the true
1126 * result of <i>e</i><sup>x</sup> than {@code exp(x)}.
1130 * <li>If the argument is NaN, the result is NaN.
1132 * <li>If the argument is positive infinity, then the result is
1133 * positive infinity.
1135 * <li>If the argument is negative infinity, then the result is
1138 * <li>If the argument is zero, then the result is a zero with the
1139 * same sign as the argument.
1143 * @param x the exponent to raise <i>e</i> to in the computation of
1144 * <i>e</i><sup>{@code x}</sup> -1.
1145 * @return the value <i>e</i><sup>{@code x}</sup> - 1.
1148 public static native double expm1(double x);
1151 * Returns the natural logarithm of the sum of the argument and 1.
1152 * Note that for small values {@code x}, the result of
1153 * {@code log1p(x)} is much closer to the true result of ln(1
1154 * + {@code x}) than the floating-point evaluation of
1155 * {@code log(1.0+x)}.
1160 * <li>If the argument is NaN or less than -1, then the result is
1163 * <li>If the argument is positive infinity, then the result is
1164 * positive infinity.
1166 * <li>If the argument is negative one, then the result is
1167 * negative infinity.
1169 * <li>If the argument is zero, then the result is a zero with the
1170 * same sign as the argument.
1175 * @return the value ln({@code x} + 1), the natural
1176 * log of {@code x} + 1
1179 public static native double log1p(double x);
1182 * Returns the first floating-point argument with the sign of the
1183 * second floating-point argument. For this method, a NaN
1184 * {@code sign} argument is always treated as if it were
1187 * @param magnitude the parameter providing the magnitude of the result
1188 * @param sign the parameter providing the sign of the result
1189 * @return a value with the magnitude of {@code magnitude}
1190 * and the sign of {@code sign}.
1193 public static double copySign(double magnitude, double sign) {
1194 throw new UnsupportedOperationException();
1198 * Returns the first floating-point argument with the sign of the
1199 * second floating-point argument. For this method, a NaN
1200 * {@code sign} argument is always treated as if it were
1203 * @param magnitude the parameter providing the magnitude of the result
1204 * @param sign the parameter providing the sign of the result
1205 * @return a value with the magnitude of {@code magnitude}
1206 * and the sign of {@code sign}.
1209 public static float copySign(float magnitude, float sign) {
1210 throw new UnsupportedOperationException();
1213 * Returns the unbiased exponent used in the representation of a
1214 * {@code float}. Special cases:
1217 * <li>If the argument is NaN or infinite, then the result is
1218 * {@link Float#MAX_EXPONENT} + 1.
1219 * <li>If the argument is zero or subnormal, then the result is
1220 * {@link Float#MIN_EXPONENT} -1.
1222 * @param f a {@code float} value
1225 public static int getExponent(float f) {
1226 throw new UnsupportedOperationException();
1230 * Returns the unbiased exponent used in the representation of a
1231 * {@code double}. Special cases:
1234 * <li>If the argument is NaN or infinite, then the result is
1235 * {@link Double#MAX_EXPONENT} + 1.
1236 * <li>If the argument is zero or subnormal, then the result is
1237 * {@link Double#MIN_EXPONENT} -1.
1239 * @param d a {@code double} value
1242 public static int getExponent(double d) {
1243 throw new UnsupportedOperationException();
1247 * Returns the floating-point number adjacent to the first
1248 * argument in the direction of the second argument. If both
1249 * arguments compare as equal the second argument is returned.
1253 * <li> If either argument is a NaN, then NaN is returned.
1255 * <li> If both arguments are signed zeros, {@code direction}
1256 * is returned unchanged (as implied by the requirement of
1257 * returning the second argument if the arguments compare as
1260 * <li> If {@code start} is
1261 * ±{@link Double#MIN_VALUE} and {@code direction}
1262 * has a value such that the result should have a smaller
1263 * magnitude, then a zero with the same sign as {@code start}
1266 * <li> If {@code start} is infinite and
1267 * {@code direction} has a value such that the result should
1268 * have a smaller magnitude, {@link Double#MAX_VALUE} with the
1269 * same sign as {@code start} is returned.
1271 * <li> If {@code start} is equal to ±
1272 * {@link Double#MAX_VALUE} and {@code direction} has a
1273 * value such that the result should have a larger magnitude, an
1274 * infinity with same sign as {@code start} is returned.
1277 * @param start starting floating-point value
1278 * @param direction value indicating which of
1279 * {@code start}'s neighbors or {@code start} should
1281 * @return The floating-point number adjacent to {@code start} in the
1282 * direction of {@code direction}.
1285 public static double nextAfter(double start, double direction) {
1286 throw new UnsupportedOperationException();
1290 * Returns the floating-point number adjacent to the first
1291 * argument in the direction of the second argument. If both
1292 * arguments compare as equal a value equivalent to the second argument
1297 * <li> If either argument is a NaN, then NaN is returned.
1299 * <li> If both arguments are signed zeros, a value equivalent
1300 * to {@code direction} is returned.
1302 * <li> If {@code start} is
1303 * ±{@link Float#MIN_VALUE} and {@code direction}
1304 * has a value such that the result should have a smaller
1305 * magnitude, then a zero with the same sign as {@code start}
1308 * <li> If {@code start} is infinite and
1309 * {@code direction} has a value such that the result should
1310 * have a smaller magnitude, {@link Float#MAX_VALUE} with the
1311 * same sign as {@code start} is returned.
1313 * <li> If {@code start} is equal to ±
1314 * {@link Float#MAX_VALUE} and {@code direction} has a
1315 * value such that the result should have a larger magnitude, an
1316 * infinity with same sign as {@code start} is returned.
1319 * @param start starting floating-point value
1320 * @param direction value indicating which of
1321 * {@code start}'s neighbors or {@code start} should
1323 * @return The floating-point number adjacent to {@code start} in the
1324 * direction of {@code direction}.
1327 public static float nextAfter(float start, double direction) {
1328 throw new UnsupportedOperationException();
1332 * Returns the floating-point value adjacent to {@code d} in
1333 * the direction of positive infinity. This method is
1334 * semantically equivalent to {@code nextAfter(d,
1335 * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
1336 * implementation may run faster than its equivalent
1337 * {@code nextAfter} call.
1341 * <li> If the argument is NaN, the result is NaN.
1343 * <li> If the argument is positive infinity, the result is
1344 * positive infinity.
1346 * <li> If the argument is zero, the result is
1347 * {@link Double#MIN_VALUE}
1351 * @param d starting floating-point value
1352 * @return The adjacent floating-point value closer to positive
1356 public static double nextUp(double d) {
1357 throw new UnsupportedOperationException();
1361 * Returns the floating-point value adjacent to {@code f} in
1362 * the direction of positive infinity. This method is
1363 * semantically equivalent to {@code nextAfter(f,
1364 * Float.POSITIVE_INFINITY)}; however, a {@code nextUp}
1365 * implementation may run faster than its equivalent
1366 * {@code nextAfter} call.
1370 * <li> If the argument is NaN, the result is NaN.
1372 * <li> If the argument is positive infinity, the result is
1373 * positive infinity.
1375 * <li> If the argument is zero, the result is
1376 * {@link Float#MIN_VALUE}
1380 * @param f starting floating-point value
1381 * @return The adjacent floating-point value closer to positive
1385 public static float nextUp(float f) {
1386 throw new UnsupportedOperationException();
1391 * Return {@code d} ×
1392 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1393 * by a single correctly rounded floating-point multiply to a
1394 * member of the double value set. See the Java
1395 * Language Specification for a discussion of floating-point
1396 * value sets. If the exponent of the result is between {@link
1397 * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
1398 * answer is calculated exactly. If the exponent of the result
1399 * would be larger than {@code Double.MAX_EXPONENT}, an
1400 * infinity is returned. Note that if the result is subnormal,
1401 * precision may be lost; that is, when {@code scalb(x, n)}
1402 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1403 * <i>x</i>. When the result is non-NaN, the result has the same
1404 * sign as {@code d}.
1408 * <li> If the first argument is NaN, NaN is returned.
1409 * <li> If the first argument is infinite, then an infinity of the
1410 * same sign is returned.
1411 * <li> If the first argument is zero, then a zero of the same
1415 * @param d number to be scaled by a power of two.
1416 * @param scaleFactor power of 2 used to scale {@code d}
1417 * @return {@code d} × 2<sup>{@code scaleFactor}</sup>
1420 public static double scalb(double d, int scaleFactor) {
1421 throw new UnsupportedOperationException();
1425 * Return {@code f} ×
1426 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1427 * by a single correctly rounded floating-point multiply to a
1428 * member of the float value set. See the Java
1429 * Language Specification for a discussion of floating-point
1430 * value sets. If the exponent of the result is between {@link
1431 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
1432 * answer is calculated exactly. If the exponent of the result
1433 * would be larger than {@code Float.MAX_EXPONENT}, an
1434 * infinity is returned. Note that if the result is subnormal,
1435 * precision may be lost; that is, when {@code scalb(x, n)}
1436 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1437 * <i>x</i>. When the result is non-NaN, the result has the same
1438 * sign as {@code f}.
1442 * <li> If the first argument is NaN, NaN is returned.
1443 * <li> If the first argument is infinite, then an infinity of the
1444 * same sign is returned.
1445 * <li> If the first argument is zero, then a zero of the same
1449 * @param f number to be scaled by a power of two.
1450 * @param scaleFactor power of 2 used to scale {@code f}
1451 * @return {@code f} × 2<sup>{@code scaleFactor}</sup>
1454 public static float scalb(float f, int scaleFactor) {
1455 throw new UnsupportedOperationException();