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
27 import java.util.Random;
28 import sun.misc.FpUtils;
29 import sun.misc.DoubleConsts;
32 * The class {@code StrictMath} contains methods for performing basic
33 * numeric operations such as the elementary exponential, logarithm,
34 * square root, and trigonometric functions.
36 * <p>To help ensure portability of Java programs, the definitions of
37 * some of the numeric functions in this package require that they
38 * produce the same results as certain published algorithms. These
39 * algorithms are available from the well-known network library
40 * {@code netlib} as the package "Freely Distributable Math
42 * href="ftp://ftp.netlib.org/fdlibm.tar">{@code fdlibm}</a>. These
43 * algorithms, which are written in the C programming language, are
44 * then to be understood as executed with all floating-point
45 * operations following the rules of Java floating-point arithmetic.
47 * <p>The Java math library is defined with respect to
48 * {@code fdlibm} version 5.3. Where {@code fdlibm} provides
49 * more than one definition for a function (such as
50 * {@code acos}), use the "IEEE 754 core function" version
51 * (residing in a file whose name begins with the letter
52 * {@code e}). The methods which require {@code fdlibm}
53 * semantics are {@code sin}, {@code cos}, {@code tan},
54 * {@code asin}, {@code acos}, {@code atan},
55 * {@code exp}, {@code log}, {@code log10},
56 * {@code cbrt}, {@code atan2}, {@code pow},
57 * {@code sinh}, {@code cosh}, {@code tanh},
58 * {@code hypot}, {@code expm1}, and {@code log1p}.
61 * @author Joseph D. Darcy
65 public final class StrictMath {
68 * Don't let anyone instantiate this class.
70 private StrictMath() {}
73 * The {@code double} value that is closer than any other to
74 * <i>e</i>, the base of the natural logarithms.
76 public static final double E = 2.7182818284590452354;
79 * The {@code double} value that is closer than any other to
80 * <i>pi</i>, the ratio of the circumference of a circle to its
83 public static final double PI = 3.14159265358979323846;
86 * Returns the trigonometric sine of an angle. Special cases:
87 * <ul><li>If the argument is NaN or an infinity, then the
89 * <li>If the argument is zero, then the result is a zero with the
90 * same sign as the argument.</ul>
92 * @param a an angle, in radians.
93 * @return the sine of the argument.
95 public static native double sin(double a);
98 * Returns the trigonometric cosine of an angle. Special cases:
99 * <ul><li>If the argument is NaN or an infinity, then the
100 * result is NaN.</ul>
102 * @param a an angle, in radians.
103 * @return the cosine of the argument.
105 public static native double cos(double a);
108 * Returns the trigonometric tangent of an angle. Special cases:
109 * <ul><li>If the argument is NaN or an infinity, then the result
111 * <li>If the argument is zero, then the result is a zero with the
112 * same sign as the argument.</ul>
114 * @param a an angle, in radians.
115 * @return the tangent of the argument.
117 public static native double tan(double a);
120 * Returns the arc sine of a value; the returned angle is in the
121 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases:
122 * <ul><li>If the argument is NaN or its absolute value is greater
123 * than 1, then the result is NaN.
124 * <li>If the argument is zero, then the result is a zero with the
125 * same sign as the argument.</ul>
127 * @param a the value whose arc sine is to be returned.
128 * @return the arc sine of the argument.
130 public static native double asin(double a);
133 * Returns the arc cosine of a value; the returned angle is in the
134 * range 0.0 through <i>pi</i>. Special case:
135 * <ul><li>If the argument is NaN or its absolute value is greater
136 * than 1, then the result is NaN.</ul>
138 * @param a the value whose arc cosine is to be returned.
139 * @return the arc cosine of the argument.
141 public static native double acos(double a);
144 * Returns the arc tangent of a value; the returned angle is in the
145 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases:
146 * <ul><li>If the argument is NaN, then the result is NaN.
147 * <li>If the argument is zero, then the result is a zero with the
148 * same sign as the argument.</ul>
150 * @param a the value whose arc tangent is to be returned.
151 * @return the arc tangent of the argument.
153 public static native double atan(double a);
156 * Converts an angle measured in degrees to an approximately
157 * equivalent angle measured in radians. The conversion from
158 * degrees to radians is generally inexact.
160 * @param angdeg an angle, in degrees
161 * @return the measurement of the angle {@code angdeg}
164 public static strictfp double toRadians(double angdeg) {
165 return angdeg / 180.0 * PI;
169 * Converts an angle measured in radians to an approximately
170 * equivalent angle measured in degrees. The conversion from
171 * radians to degrees is generally inexact; users should
172 * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
175 * @param angrad an angle, in radians
176 * @return the measurement of the angle {@code angrad}
179 public static strictfp double toDegrees(double angrad) {
180 return angrad * 180.0 / PI;
184 * Returns Euler's number <i>e</i> raised to the power of a
185 * {@code double} value. Special cases:
186 * <ul><li>If the argument is NaN, the result is NaN.
187 * <li>If the argument is positive infinity, then the result is
189 * <li>If the argument is negative infinity, then the result is
190 * positive zero.</ul>
192 * @param a the exponent to raise <i>e</i> to.
193 * @return the value <i>e</i><sup>{@code a}</sup>,
194 * where <i>e</i> is the base of the natural logarithms.
196 public static native double exp(double a);
199 * Returns the natural logarithm (base <i>e</i>) of a {@code double}
200 * value. Special cases:
201 * <ul><li>If the argument is NaN or less than zero, then the result
203 * <li>If the argument is positive infinity, then the result is
205 * <li>If the argument is positive zero or negative zero, then the
206 * result is negative infinity.</ul>
209 * @return the value ln {@code a}, the natural logarithm of
212 public static native double log(double a);
216 * Returns the base 10 logarithm of a {@code double} value.
219 * <ul><li>If the argument is NaN or less than zero, then the result
221 * <li>If the argument is positive infinity, then the result is
223 * <li>If the argument is positive zero or negative zero, then the
224 * result is negative infinity.
225 * <li> If the argument is equal to 10<sup><i>n</i></sup> for
226 * integer <i>n</i>, then the result is <i>n</i>.
230 * @return the base 10 logarithm of {@code a}.
233 public static native double log10(double a);
236 * Returns the correctly rounded positive square root of a
237 * {@code double} value.
239 * <ul><li>If the argument is NaN or less than zero, then the result
241 * <li>If the argument is positive infinity, then the result is positive
243 * <li>If the argument is positive zero or negative zero, then the
244 * result is the same as the argument.</ul>
245 * Otherwise, the result is the {@code double} value closest to
246 * the true mathematical square root of the argument value.
249 * @return the positive square root of {@code a}.
251 public static native double sqrt(double a);
254 * Returns the cube root of a {@code double} value. For
255 * positive finite {@code x}, {@code cbrt(-x) ==
256 * -cbrt(x)}; that is, the cube root of a negative value is
257 * the negative of the cube root of that value's magnitude.
262 * <li>If the argument is NaN, then the result is NaN.
264 * <li>If the argument is infinite, then the result is an infinity
265 * with the same sign as the argument.
267 * <li>If the argument is zero, then the result is a zero with the
268 * same sign as the argument.
273 * @return the cube root of {@code a}.
276 public static native double cbrt(double a);
279 * Computes the remainder operation on two arguments as prescribed
280 * by the IEEE 754 standard.
281 * The remainder value is mathematically equal to
282 * <code>f1 - f2</code> × <i>n</i>,
283 * where <i>n</i> is the mathematical integer closest to the exact
284 * mathematical value of the quotient {@code f1/f2}, and if two
285 * mathematical integers are equally close to {@code f1/f2},
286 * then <i>n</i> is the integer that is even. If the remainder is
287 * zero, its sign is the same as the sign of the first argument.
289 * <ul><li>If either argument is NaN, or the first argument is infinite,
290 * or the second argument is positive zero or negative zero, then the
292 * <li>If the first argument is finite and the second argument is
293 * infinite, then the result is the same as the first argument.</ul>
295 * @param f1 the dividend.
296 * @param f2 the divisor.
297 * @return the remainder when {@code f1} is divided by
300 public static native double IEEEremainder(double f1, double f2);
303 * Returns the smallest (closest to negative infinity)
304 * {@code double} value that is greater than or equal to the
305 * argument and is equal to a mathematical integer. Special cases:
306 * <ul><li>If the argument value is already equal to a
307 * mathematical integer, then the result is the same as the
308 * argument. <li>If the argument is NaN or an infinity or
309 * positive zero or negative zero, then the result is the same as
310 * the argument. <li>If the argument value is less than zero but
311 * greater than -1.0, then the result is negative zero.</ul> Note
312 * that the value of {@code StrictMath.ceil(x)} is exactly the
313 * value of {@code -StrictMath.floor(-x)}.
316 * @return the smallest (closest to negative infinity)
317 * floating-point value that is greater than or equal to
318 * the argument and is equal to a mathematical integer.
320 public static double ceil(double a) {
321 return floorOrCeil(a, -0.0, 1.0, 1.0);
325 * Returns the largest (closest to positive infinity)
326 * {@code double} value that is less than or equal to the
327 * argument and is equal to a mathematical integer. Special cases:
328 * <ul><li>If the argument value is already equal to a
329 * mathematical integer, then the result is the same as the
330 * argument. <li>If the argument is NaN or an infinity or
331 * positive zero or negative zero, then the result is the same as
335 * @return the largest (closest to positive infinity)
336 * floating-point value that less than or equal to the argument
337 * and is equal to a mathematical integer.
339 public static double floor(double a) {
340 return floorOrCeil(a, -1.0, 0.0, -1.0);
344 * Internal method to share logic between floor and ceil.
346 * @param a the value to be floored or ceiled
347 * @param negativeBoundary result for values in (-1, 0)
348 * @param positiveBoundary result for values in (0, 1)
349 * @param increment value to add when the argument is non-integral
351 private static double floorOrCeil(double a,
352 double negativeBoundary,
353 double positiveBoundary,
355 int exponent = Math.getExponent(a);
359 * Absolute value of argument is less than 1.
360 * floorOrceil(-0.0) => -0.0
361 * floorOrceil(+0.0) => +0.0
363 return ((a == 0.0) ? a :
364 ( (a < 0.0) ? negativeBoundary : positiveBoundary) );
365 } else if (exponent >= 52) {
367 * Infinity, NaN, or a value so large it must be integral.
371 // Else the argument is either an integral value already XOR it
372 // has to be rounded to one.
373 assert exponent >= 0 && exponent <= 51;
375 long doppel = Double.doubleToRawLongBits(a);
376 long mask = DoubleConsts.SIGNIF_BIT_MASK >> exponent;
378 if ( (mask & doppel) == 0L )
379 return a; // integral value
381 double result = Double.longBitsToDouble(doppel & (~mask));
383 result = result + sign;
389 * Returns the {@code double} value that is closest in value
390 * to the argument and is equal to a mathematical integer. If two
391 * {@code double} values that are mathematical integers are
392 * equally close to the value of the argument, the result is the
393 * integer value that is even. Special cases:
394 * <ul><li>If the argument value is already equal to a mathematical
395 * integer, then the result is the same as the argument.
396 * <li>If the argument is NaN or an infinity or positive zero or negative
397 * zero, then the result is the same as the argument.</ul>
400 * @return the closest floating-point value to {@code a} that is
401 * equal to a mathematical integer.
402 * @author Joseph D. Darcy
404 public static double rint(double a) {
406 * If the absolute value of a is not less than 2^52, it
407 * is either a finite integer (the double format does not have
408 * enough significand bits for a number that large to have any
409 * fractional portion), an infinity, or a NaN. In any of
410 * these cases, rint of the argument is the argument.
412 * Otherwise, the sum (twoToThe52 + a ) will properly round
413 * away any fractional portion of a since ulp(twoToThe52) ==
414 * 1.0; subtracting out twoToThe52 from this sum will then be
415 * exact and leave the rounded integer portion of a.
417 * This method does *not* need to be declared strictfp to get
418 * fully reproducible results. Whether or not a method is
419 * declared strictfp can only make a difference in the
420 * returned result if some operation would overflow or
421 * underflow with strictfp semantics. The operation
422 * (twoToThe52 + a ) cannot overflow since large values of a
423 * are screened out; the add cannot underflow since twoToThe52
424 * is too large. The subtraction ((twoToThe52 + a ) -
425 * twoToThe52) will be exact as discussed above and thus
426 * cannot overflow or meaningfully underflow. Finally, the
427 * last multiply in the return statement is by plus or minus
428 * 1.0, which is exact too.
430 double twoToThe52 = (double)(1L << 52); // 2^52
431 double sign = FpUtils.rawCopySign(1.0, a); // preserve sign info
434 if (a < twoToThe52) { // E_min <= ilogb(a) <= 51
435 a = ((twoToThe52 + a ) - twoToThe52);
438 return sign * a; // restore original sign
442 * Returns the angle <i>theta</i> from the conversion of rectangular
443 * coordinates ({@code x}, {@code y}) to polar
444 * coordinates (r, <i>theta</i>).
445 * This method computes the phase <i>theta</i> by computing an arc tangent
446 * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special
448 * <ul><li>If either argument is NaN, then the result is NaN.
449 * <li>If the first argument is positive zero and the second argument
450 * is positive, or the first argument is positive and finite and the
451 * second argument is positive infinity, then the result is positive
453 * <li>If the first argument is negative zero and the second argument
454 * is positive, or the first argument is negative and finite and the
455 * second argument is positive infinity, then the result is negative zero.
456 * <li>If the first argument is positive zero and the second argument
457 * is negative, or the first argument is positive and finite and the
458 * second argument is negative infinity, then the result is the
459 * {@code double} value closest to <i>pi</i>.
460 * <li>If the first argument is negative zero and the second argument
461 * is negative, or the first argument is negative and finite and the
462 * second argument is negative infinity, then the result is the
463 * {@code double} value closest to -<i>pi</i>.
464 * <li>If the first argument is positive and the second argument is
465 * positive zero or negative zero, or the first argument is positive
466 * infinity and the second argument is finite, then the result is the
467 * {@code double} value closest to <i>pi</i>/2.
468 * <li>If the first argument is negative and the second argument is
469 * positive zero or negative zero, or the first argument is negative
470 * infinity and the second argument is finite, then the result is the
471 * {@code double} value closest to -<i>pi</i>/2.
472 * <li>If both arguments are positive infinity, then the result is the
473 * {@code double} value closest to <i>pi</i>/4.
474 * <li>If the first argument is positive infinity and the second argument
475 * is negative infinity, then the result is the {@code double}
476 * value closest to 3*<i>pi</i>/4.
477 * <li>If the first argument is negative infinity and the second argument
478 * is positive infinity, then the result is the {@code double} value
479 * closest to -<i>pi</i>/4.
480 * <li>If both arguments are negative infinity, then the result is the
481 * {@code double} value closest to -3*<i>pi</i>/4.</ul>
483 * @param y the ordinate coordinate
484 * @param x the abscissa coordinate
485 * @return the <i>theta</i> component of the point
486 * (<i>r</i>, <i>theta</i>)
487 * in polar coordinates that corresponds to the point
488 * (<i>x</i>, <i>y</i>) in Cartesian coordinates.
490 public static native double atan2(double y, double x);
494 * Returns the value of the first argument raised to the power of the
495 * second argument. Special cases:
497 * <ul><li>If the second argument is positive or negative zero, then the
499 * <li>If the second argument is 1.0, then the result is the same as the
501 * <li>If the second argument is NaN, then the result is NaN.
502 * <li>If the first argument is NaN and the second argument is nonzero,
503 * then the result is NaN.
507 * <li>the absolute value of the first argument is greater than 1
508 * and the second argument is positive infinity, or
509 * <li>the absolute value of the first argument is less than 1 and
510 * the second argument is negative infinity,
512 * then the result is positive infinity.
516 * <li>the absolute value of the first argument is greater than 1 and
517 * the second argument is negative infinity, or
518 * <li>the absolute value of the
519 * first argument is less than 1 and the second argument is positive
522 * then the result is positive zero.
524 * <li>If the absolute value of the first argument equals 1 and the
525 * second argument is infinite, then the result is NaN.
529 * <li>the first argument is positive zero and the second argument
530 * is greater than zero, or
531 * <li>the first argument is positive infinity and the second
532 * argument is less than zero,
534 * then the result is positive zero.
538 * <li>the first argument is positive zero and the second argument
539 * is less than zero, or
540 * <li>the first argument is positive infinity and the second
541 * argument is greater than zero,
543 * then the result is positive infinity.
547 * <li>the first argument is negative zero and the second argument
548 * is greater than zero but not a finite odd integer, or
549 * <li>the first argument is negative infinity and the second
550 * argument is less than zero but not a finite odd integer,
552 * then the result is positive zero.
556 * <li>the first argument is negative zero and the second argument
557 * is a positive finite odd integer, or
558 * <li>the first argument is negative infinity and the second
559 * argument is a negative finite odd integer,
561 * then the result is negative zero.
565 * <li>the first argument is negative zero and the second argument
566 * is less than zero but not a finite odd integer, or
567 * <li>the first argument is negative infinity and the second
568 * argument is greater than zero but not a finite odd integer,
570 * then the result is positive infinity.
574 * <li>the first argument is negative zero and the second argument
575 * is a negative finite odd integer, or
576 * <li>the first argument is negative infinity and the second
577 * argument is a positive finite odd integer,
579 * then the result is negative infinity.
581 * <li>If the first argument is finite and less than zero
583 * <li> if the second argument is a finite even integer, the
584 * result is equal to the result of raising the absolute value of
585 * the first argument to the power of the second argument
587 * <li>if the second argument is a finite odd integer, the result
588 * is equal to the negative of the result of raising the absolute
589 * value of the first argument to the power of the second
592 * <li>if the second argument is finite and not an integer, then
596 * <li>If both arguments are integers, then the result is exactly equal
597 * to the mathematical result of raising the first argument to the power
598 * of the second argument if that result can in fact be represented
599 * exactly as a {@code double} value.</ul>
601 * <p>(In the foregoing descriptions, a floating-point value is
602 * considered to be an integer if and only if it is finite and a
603 * fixed point of the method {@link #ceil ceil} or,
604 * equivalently, a fixed point of the method {@link #floor
605 * floor}. A value is a fixed point of a one-argument
606 * method if and only if the result of applying the method to the
607 * value is equal to the value.)
610 * @param b the exponent.
611 * @return the value {@code a}<sup>{@code b}</sup>.
613 public static native double pow(double a, double b);
616 * Returns the closest {@code int} to the argument, with ties
620 * <ul><li>If the argument is NaN, the result is 0.
621 * <li>If the argument is negative infinity or any value less than or
622 * equal to the value of {@code Integer.MIN_VALUE}, the result is
623 * equal to the value of {@code Integer.MIN_VALUE}.
624 * <li>If the argument is positive infinity or any value greater than or
625 * equal to the value of {@code Integer.MAX_VALUE}, the result is
626 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
628 * @param a a floating-point value to be rounded to an integer.
629 * @return the value of the argument rounded to the nearest
631 * @see java.lang.Integer#MAX_VALUE
632 * @see java.lang.Integer#MIN_VALUE
634 public static int round(float a) {
635 return Math.round(a);
639 * Returns the closest {@code long} to the argument, with ties
643 * <ul><li>If the argument is NaN, the result is 0.
644 * <li>If the argument is negative infinity or any value less than or
645 * equal to the value of {@code Long.MIN_VALUE}, the result is
646 * equal to the value of {@code Long.MIN_VALUE}.
647 * <li>If the argument is positive infinity or any value greater than or
648 * equal to the value of {@code Long.MAX_VALUE}, the result is
649 * equal to the value of {@code Long.MAX_VALUE}.</ul>
651 * @param a a floating-point value to be rounded to a
653 * @return the value of the argument rounded to the nearest
654 * {@code long} value.
655 * @see java.lang.Long#MAX_VALUE
656 * @see java.lang.Long#MIN_VALUE
658 public static long round(double a) {
659 return Math.round(a);
662 private static Random randomNumberGenerator;
664 private static synchronized Random initRNG() {
665 Random rnd = randomNumberGenerator;
666 return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
670 * Returns a {@code double} value with a positive sign, greater
671 * than or equal to {@code 0.0} and less than {@code 1.0}.
672 * Returned values are chosen pseudorandomly with (approximately)
673 * uniform distribution from that range.
675 * <p>When this method is first called, it creates a single new
676 * pseudorandom-number generator, exactly as if by the expression
678 * <blockquote>{@code new java.util.Random()}</blockquote>
680 * This new pseudorandom-number generator is used thereafter for
681 * all calls to this method and is used nowhere else.
683 * <p>This method is properly synchronized to allow correct use by
684 * more than one thread. However, if many threads need to generate
685 * pseudorandom numbers at a great rate, it may reduce contention
686 * for each thread to have its own pseudorandom number generator.
688 * @return a pseudorandom {@code double} greater than or equal
689 * to {@code 0.0} and less than {@code 1.0}.
690 * @see Random#nextDouble()
692 public static double random() {
693 Random rnd = randomNumberGenerator;
694 if (rnd == null) rnd = initRNG();
695 return rnd.nextDouble();
699 * Returns the absolute value of an {@code int} value..
700 * If the argument is not negative, the argument is returned.
701 * If the argument is negative, the negation of the argument is returned.
703 * <p>Note that if the argument is equal to the value of
704 * {@link Integer#MIN_VALUE}, the most negative representable
705 * {@code int} value, the result is that same value, which is
708 * @param a the argument whose absolute value is to be determined.
709 * @return the absolute value of the argument.
711 public static int abs(int a) {
712 return (a < 0) ? -a : a;
716 * Returns the absolute value of a {@code long} value.
717 * If the argument is not negative, the argument is returned.
718 * If the argument is negative, the negation of the argument is returned.
720 * <p>Note that if the argument is equal to the value of
721 * {@link Long#MIN_VALUE}, the most negative representable
722 * {@code long} value, the result is that same value, which
725 * @param a the argument whose absolute value is to be determined.
726 * @return the absolute value of the argument.
728 public static long abs(long a) {
729 return (a < 0) ? -a : a;
733 * Returns the absolute value of a {@code float} value.
734 * If the argument is not negative, the argument is returned.
735 * If the argument is negative, the negation of the argument is returned.
737 * <ul><li>If the argument is positive zero or negative zero, the
738 * result is positive zero.
739 * <li>If the argument is infinite, the result is positive infinity.
740 * <li>If the argument is NaN, the result is NaN.</ul>
741 * In other words, the result is the same as the value of the expression:
742 * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))}
744 * @param a the argument whose absolute value is to be determined
745 * @return the absolute value of the argument.
747 public static float abs(float a) {
748 return (a <= 0.0F) ? 0.0F - a : a;
752 * Returns the absolute value of a {@code double} value.
753 * If the argument is not negative, the argument is returned.
754 * If the argument is negative, the negation of the argument is returned.
756 * <ul><li>If the argument is positive zero or negative zero, the result
758 * <li>If the argument is infinite, the result is positive infinity.
759 * <li>If the argument is NaN, the result is NaN.</ul>
760 * In other words, the result is the same as the value of the expression:
761 * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)}
763 * @param a the argument whose absolute value is to be determined
764 * @return the absolute value of the argument.
766 public static double abs(double a) {
767 return (a <= 0.0D) ? 0.0D - a : a;
771 * Returns the greater of two {@code int} values. That is, the
772 * result is the argument closer to the value of
773 * {@link Integer#MAX_VALUE}. If the arguments have the same value,
774 * the result is that same value.
776 * @param a an argument.
777 * @param b another argument.
778 * @return the larger of {@code a} and {@code b}.
780 public static int max(int a, int b) {
781 return (a >= b) ? a : b;
785 * Returns the greater of two {@code long} values. That is, the
786 * result is the argument closer to the value of
787 * {@link Long#MAX_VALUE}. If the arguments have the same value,
788 * the result is that same value.
790 * @param a an argument.
791 * @param b another argument.
792 * @return the larger of {@code a} and {@code b}.
794 public static long max(long a, long b) {
795 return (a >= b) ? a : b;
798 // Use raw bit-wise conversions on guaranteed non-NaN arguments.
799 private static long negativeZeroFloatBits = Float.floatToRawIntBits(-0.0f);
800 private static long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d);
803 * Returns the greater of two {@code float} values. That is,
804 * the result is the argument closer to positive infinity. If the
805 * arguments have the same value, the result is that same
806 * value. If either value is NaN, then the result is NaN. Unlike
807 * the numerical comparison operators, this method considers
808 * negative zero to be strictly smaller than positive zero. If one
809 * argument is positive zero and the other negative zero, the
810 * result is positive zero.
812 * @param a an argument.
813 * @param b another argument.
814 * @return the larger of {@code a} and {@code b}.
816 public static float max(float a, float b) {
818 return a; // a is NaN
821 (Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
822 // Raw conversion ok since NaN can't map to -0.0.
825 return (a >= b) ? a : b;
829 * Returns the greater of two {@code double} values. That
830 * is, the result is the argument closer to positive infinity. If
831 * the arguments have the same value, the result is that same
832 * value. If either value is NaN, then the result is NaN. Unlike
833 * the numerical comparison operators, this method considers
834 * negative zero to be strictly smaller than positive zero. If one
835 * argument is positive zero and the other negative zero, the
836 * result is positive zero.
838 * @param a an argument.
839 * @param b another argument.
840 * @return the larger of {@code a} and {@code b}.
842 public static double max(double a, double b) {
844 return a; // a is NaN
847 (Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
848 // Raw conversion ok since NaN can't map to -0.0.
851 return (a >= b) ? a : b;
855 * Returns the smaller of two {@code int} values. That is,
856 * the result the argument closer to the value of
857 * {@link Integer#MIN_VALUE}. If the arguments have the same
858 * value, the result is that same value.
860 * @param a an argument.
861 * @param b another argument.
862 * @return the smaller of {@code a} and {@code b}.
864 public static int min(int a, int b) {
865 return (a <= b) ? a : b;
869 * Returns the smaller of two {@code long} values. That is,
870 * the result is the argument closer to the value of
871 * {@link Long#MIN_VALUE}. If the arguments have the same
872 * value, the result is that same value.
874 * @param a an argument.
875 * @param b another argument.
876 * @return the smaller of {@code a} and {@code b}.
878 public static long min(long a, long b) {
879 return (a <= b) ? a : b;
883 * Returns the smaller of two {@code float} values. That is,
884 * the result is the value closer to negative infinity. If the
885 * arguments have the same value, the result is that same
886 * value. If either value is NaN, then the result is NaN. Unlike
887 * the numerical comparison operators, this method considers
888 * negative zero to be strictly smaller than positive zero. If
889 * one argument is positive zero and the other is negative zero,
890 * the result is negative zero.
892 * @param a an argument.
893 * @param b another argument.
894 * @return the smaller of {@code a} and {@code b.}
896 public static float min(float a, float b) {
898 return a; // a is NaN
901 (Float.floatToRawIntBits(b) == negativeZeroFloatBits)) {
902 // Raw conversion ok since NaN can't map to -0.0.
905 return (a <= b) ? a : b;
909 * Returns the smaller of two {@code double} values. That
910 * is, the result is the value closer to negative infinity. If the
911 * arguments have the same value, the result is that same
912 * value. If either value is NaN, then the result is NaN. Unlike
913 * the numerical comparison operators, this method considers
914 * negative zero to be strictly smaller than positive zero. If one
915 * argument is positive zero and the other is negative zero, the
916 * result is negative zero.
918 * @param a an argument.
919 * @param b another argument.
920 * @return the smaller of {@code a} and {@code b}.
922 public static double min(double a, double b) {
924 return a; // a is NaN
927 (Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) {
928 // Raw conversion ok since NaN can't map to -0.0.
931 return (a <= b) ? a : b;
935 * Returns the size of an ulp of the argument. An ulp of a
936 * {@code double} value is the positive distance between this
937 * floating-point value and the {@code double} value next
938 * larger in magnitude. Note that for non-NaN <i>x</i>,
939 * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
943 * <li> If the argument is NaN, then the result is NaN.
944 * <li> If the argument is positive or negative infinity, then the
945 * result is positive infinity.
946 * <li> If the argument is positive or negative zero, then the result is
947 * {@code Double.MIN_VALUE}.
948 * <li> If the argument is ±{@code Double.MAX_VALUE}, then
949 * the result is equal to 2<sup>971</sup>.
952 * @param d the floating-point value whose ulp is to be returned
953 * @return the size of an ulp of the argument
954 * @author Joseph D. Darcy
957 public static double ulp(double d) {
958 return sun.misc.FpUtils.ulp(d);
962 * Returns the size of an ulp of the argument. An ulp of a
963 * {@code float} value is the positive distance between this
964 * floating-point value and the {@code float} value next
965 * larger in magnitude. Note that for non-NaN <i>x</i>,
966 * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
970 * <li> If the argument is NaN, then the result is NaN.
971 * <li> If the argument is positive or negative infinity, then the
972 * result is positive infinity.
973 * <li> If the argument is positive or negative zero, then the result is
974 * {@code Float.MIN_VALUE}.
975 * <li> If the argument is ±{@code Float.MAX_VALUE}, then
976 * the result is equal to 2<sup>104</sup>.
979 * @param f the floating-point value whose ulp is to be returned
980 * @return the size of an ulp of the argument
981 * @author Joseph D. Darcy
984 public static float ulp(float f) {
985 return sun.misc.FpUtils.ulp(f);
989 * Returns the signum function of the argument; zero if the argument
990 * is zero, 1.0 if the argument is greater than zero, -1.0 if the
991 * argument is less than zero.
995 * <li> If the argument is NaN, then the result is NaN.
996 * <li> If the argument is positive zero or negative zero, then the
997 * result is the same as the argument.
1000 * @param d the floating-point value whose signum is to be returned
1001 * @return the signum function of the argument
1002 * @author Joseph D. Darcy
1005 public static double signum(double d) {
1006 return sun.misc.FpUtils.signum(d);
1010 * Returns the signum function of the argument; zero if the argument
1011 * is zero, 1.0f if the argument is greater than zero, -1.0f if the
1012 * argument is less than zero.
1016 * <li> If the argument is NaN, then the result is NaN.
1017 * <li> If the argument is positive zero or negative zero, then the
1018 * result is the same as the argument.
1021 * @param f the floating-point value whose signum is to be returned
1022 * @return the signum function of the argument
1023 * @author Joseph D. Darcy
1026 public static float signum(float f) {
1027 return sun.misc.FpUtils.signum(f);
1031 * Returns the hyperbolic sine of a {@code double} value.
1032 * The hyperbolic sine of <i>x</i> is defined to be
1033 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2
1034 * where <i>e</i> is {@linkplain Math#E Euler's number}.
1039 * <li>If the argument is NaN, then the result is NaN.
1041 * <li>If the argument is infinite, then the result is an infinity
1042 * with the same sign as the argument.
1044 * <li>If the argument is zero, then the result is a zero with the
1045 * same sign as the argument.
1049 * @param x The number whose hyperbolic sine is to be returned.
1050 * @return The hyperbolic sine of {@code x}.
1053 public static native double sinh(double x);
1056 * Returns the hyperbolic cosine of a {@code double} value.
1057 * The hyperbolic cosine of <i>x</i> is defined to be
1058 * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2
1059 * where <i>e</i> is {@linkplain Math#E Euler's number}.
1064 * <li>If the argument is NaN, then the result is NaN.
1066 * <li>If the argument is infinite, then the result is positive
1069 * <li>If the argument is zero, then the result is {@code 1.0}.
1073 * @param x The number whose hyperbolic cosine is to be returned.
1074 * @return The hyperbolic cosine of {@code x}.
1077 public static native double cosh(double x);
1080 * Returns the hyperbolic tangent of a {@code double} value.
1081 * The hyperbolic tangent of <i>x</i> is defined to be
1082 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>),
1083 * in other words, {@linkplain Math#sinh
1084 * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note
1085 * that the absolute value of the exact tanh is always less than
1091 * <li>If the argument is NaN, then the result is NaN.
1093 * <li>If the argument is zero, then the result is a zero with the
1094 * same sign as the argument.
1096 * <li>If the argument is positive infinity, then the result is
1099 * <li>If the argument is negative infinity, then the result is
1104 * @param x The number whose hyperbolic tangent is to be returned.
1105 * @return The hyperbolic tangent of {@code x}.
1108 public static native double tanh(double x);
1111 * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
1112 * without intermediate overflow or underflow.
1117 * <li> If either argument is infinite, then the result
1118 * is positive infinity.
1120 * <li> If either argument is NaN and neither argument is infinite,
1121 * then the result is NaN.
1127 * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
1128 * without intermediate overflow or underflow
1131 public static native double hypot(double x, double y);
1134 * Returns <i>e</i><sup>x</sup> -1. Note that for values of
1135 * <i>x</i> near 0, the exact sum of
1136 * {@code expm1(x)} + 1 is much closer to the true
1137 * result of <i>e</i><sup>x</sup> than {@code exp(x)}.
1141 * <li>If the argument is NaN, the result is NaN.
1143 * <li>If the argument is positive infinity, then the result is
1144 * positive infinity.
1146 * <li>If the argument is negative infinity, then the result is
1149 * <li>If the argument is zero, then the result is a zero with the
1150 * same sign as the argument.
1154 * @param x the exponent to raise <i>e</i> to in the computation of
1155 * <i>e</i><sup>{@code x}</sup> -1.
1156 * @return the value <i>e</i><sup>{@code x}</sup> - 1.
1159 public static native double expm1(double x);
1162 * Returns the natural logarithm of the sum of the argument and 1.
1163 * Note that for small values {@code x}, the result of
1164 * {@code log1p(x)} is much closer to the true result of ln(1
1165 * + {@code x}) than the floating-point evaluation of
1166 * {@code log(1.0+x)}.
1171 * <li>If the argument is NaN or less than -1, then the result is
1174 * <li>If the argument is positive infinity, then the result is
1175 * positive infinity.
1177 * <li>If the argument is negative one, then the result is
1178 * negative infinity.
1180 * <li>If the argument is zero, then the result is a zero with the
1181 * same sign as the argument.
1186 * @return the value ln({@code x} + 1), the natural
1187 * log of {@code x} + 1
1190 public static native double log1p(double x);
1193 * Returns the first floating-point argument with the sign of the
1194 * second floating-point argument. For this method, a NaN
1195 * {@code sign} argument is always treated as if it were
1198 * @param magnitude the parameter providing the magnitude of the result
1199 * @param sign the parameter providing the sign of the result
1200 * @return a value with the magnitude of {@code magnitude}
1201 * and the sign of {@code sign}.
1204 public static double copySign(double magnitude, double sign) {
1205 return sun.misc.FpUtils.copySign(magnitude, sign);
1209 * Returns the first floating-point argument with the sign of the
1210 * second floating-point argument. For this method, a NaN
1211 * {@code sign} argument is always treated as if it were
1214 * @param magnitude the parameter providing the magnitude of the result
1215 * @param sign the parameter providing the sign of the result
1216 * @return a value with the magnitude of {@code magnitude}
1217 * and the sign of {@code sign}.
1220 public static float copySign(float magnitude, float sign) {
1221 return sun.misc.FpUtils.copySign(magnitude, sign);
1224 * Returns the unbiased exponent used in the representation of a
1225 * {@code float}. Special cases:
1228 * <li>If the argument is NaN or infinite, then the result is
1229 * {@link Float#MAX_EXPONENT} + 1.
1230 * <li>If the argument is zero or subnormal, then the result is
1231 * {@link Float#MIN_EXPONENT} -1.
1233 * @param f a {@code float} value
1236 public static int getExponent(float f) {
1237 return sun.misc.FpUtils.getExponent(f);
1241 * Returns the unbiased exponent used in the representation of a
1242 * {@code double}. Special cases:
1245 * <li>If the argument is NaN or infinite, then the result is
1246 * {@link Double#MAX_EXPONENT} + 1.
1247 * <li>If the argument is zero or subnormal, then the result is
1248 * {@link Double#MIN_EXPONENT} -1.
1250 * @param d a {@code double} value
1253 public static int getExponent(double d) {
1254 return sun.misc.FpUtils.getExponent(d);
1258 * Returns the floating-point number adjacent to the first
1259 * argument in the direction of the second argument. If both
1260 * arguments compare as equal the second argument is returned.
1264 * <li> If either argument is a NaN, then NaN is returned.
1266 * <li> If both arguments are signed zeros, {@code direction}
1267 * is returned unchanged (as implied by the requirement of
1268 * returning the second argument if the arguments compare as
1271 * <li> If {@code start} is
1272 * ±{@link Double#MIN_VALUE} and {@code direction}
1273 * has a value such that the result should have a smaller
1274 * magnitude, then a zero with the same sign as {@code start}
1277 * <li> If {@code start} is infinite and
1278 * {@code direction} has a value such that the result should
1279 * have a smaller magnitude, {@link Double#MAX_VALUE} with the
1280 * same sign as {@code start} is returned.
1282 * <li> If {@code start} is equal to ±
1283 * {@link Double#MAX_VALUE} and {@code direction} has a
1284 * value such that the result should have a larger magnitude, an
1285 * infinity with same sign as {@code start} is returned.
1288 * @param start starting floating-point value
1289 * @param direction value indicating which of
1290 * {@code start}'s neighbors or {@code start} should
1292 * @return The floating-point number adjacent to {@code start} in the
1293 * direction of {@code direction}.
1296 public static double nextAfter(double start, double direction) {
1297 return sun.misc.FpUtils.nextAfter(start, direction);
1301 * Returns the floating-point number adjacent to the first
1302 * argument in the direction of the second argument. If both
1303 * arguments compare as equal a value equivalent to the second argument
1308 * <li> If either argument is a NaN, then NaN is returned.
1310 * <li> If both arguments are signed zeros, a value equivalent
1311 * to {@code direction} is returned.
1313 * <li> If {@code start} is
1314 * ±{@link Float#MIN_VALUE} and {@code direction}
1315 * has a value such that the result should have a smaller
1316 * magnitude, then a zero with the same sign as {@code start}
1319 * <li> If {@code start} is infinite and
1320 * {@code direction} has a value such that the result should
1321 * have a smaller magnitude, {@link Float#MAX_VALUE} with the
1322 * same sign as {@code start} is returned.
1324 * <li> If {@code start} is equal to ±
1325 * {@link Float#MAX_VALUE} and {@code direction} has a
1326 * value such that the result should have a larger magnitude, an
1327 * infinity with same sign as {@code start} is returned.
1330 * @param start starting floating-point value
1331 * @param direction value indicating which of
1332 * {@code start}'s neighbors or {@code start} should
1334 * @return The floating-point number adjacent to {@code start} in the
1335 * direction of {@code direction}.
1338 public static float nextAfter(float start, double direction) {
1339 return sun.misc.FpUtils.nextAfter(start, direction);
1343 * Returns the floating-point value adjacent to {@code d} in
1344 * the direction of positive infinity. This method is
1345 * semantically equivalent to {@code nextAfter(d,
1346 * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
1347 * implementation may run faster than its equivalent
1348 * {@code nextAfter} call.
1352 * <li> If the argument is NaN, the result is NaN.
1354 * <li> If the argument is positive infinity, the result is
1355 * positive infinity.
1357 * <li> If the argument is zero, the result is
1358 * {@link Double#MIN_VALUE}
1362 * @param d starting floating-point value
1363 * @return The adjacent floating-point value closer to positive
1367 public static double nextUp(double d) {
1368 return sun.misc.FpUtils.nextUp(d);
1372 * Returns the floating-point value adjacent to {@code f} in
1373 * the direction of positive infinity. This method is
1374 * semantically equivalent to {@code nextAfter(f,
1375 * Float.POSITIVE_INFINITY)}; however, a {@code nextUp}
1376 * implementation may run faster than its equivalent
1377 * {@code nextAfter} call.
1381 * <li> If the argument is NaN, the result is NaN.
1383 * <li> If the argument is positive infinity, the result is
1384 * positive infinity.
1386 * <li> If the argument is zero, the result is
1387 * {@link Float#MIN_VALUE}
1391 * @param f starting floating-point value
1392 * @return The adjacent floating-point value closer to positive
1396 public static float nextUp(float f) {
1397 return sun.misc.FpUtils.nextUp(f);
1402 * Return {@code d} ×
1403 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1404 * by a single correctly rounded floating-point multiply to a
1405 * member of the double value set. See the Java
1406 * Language Specification for a discussion of floating-point
1407 * value sets. If the exponent of the result is between {@link
1408 * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
1409 * answer is calculated exactly. If the exponent of the result
1410 * would be larger than {@code Double.MAX_EXPONENT}, an
1411 * infinity is returned. Note that if the result is subnormal,
1412 * precision may be lost; that is, when {@code scalb(x, n)}
1413 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1414 * <i>x</i>. When the result is non-NaN, the result has the same
1415 * sign as {@code d}.
1419 * <li> If the first argument is NaN, NaN is returned.
1420 * <li> If the first argument is infinite, then an infinity of the
1421 * same sign is returned.
1422 * <li> If the first argument is zero, then a zero of the same
1426 * @param d number to be scaled by a power of two.
1427 * @param scaleFactor power of 2 used to scale {@code d}
1428 * @return {@code d} × 2<sup>{@code scaleFactor}</sup>
1431 public static double scalb(double d, int scaleFactor) {
1432 return sun.misc.FpUtils.scalb(d, scaleFactor);
1436 * Return {@code f} ×
1437 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1438 * by a single correctly rounded floating-point multiply to a
1439 * member of the float value set. See the Java
1440 * Language Specification for a discussion of floating-point
1441 * value sets. If the exponent of the result is between {@link
1442 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
1443 * answer is calculated exactly. If the exponent of the result
1444 * would be larger than {@code Float.MAX_EXPONENT}, an
1445 * infinity is returned. Note that if the result is subnormal,
1446 * precision may be lost; that is, when {@code scalb(x, n)}
1447 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1448 * <i>x</i>. When the result is non-NaN, the result has the same
1449 * sign as {@code f}.
1453 * <li> If the first argument is NaN, NaN is returned.
1454 * <li> If the first argument is infinite, then an infinity of the
1455 * same sign is returned.
1456 * <li> If the first argument is zero, then a zero of the same
1460 * @param f number to be scaled by a power of two.
1461 * @param scaleFactor power of 2 used to scale {@code f}
1462 * @return {@code f} × 2<sup>{@code scaleFactor}</sup>
1465 public static float scalb(float f, int scaleFactor) {
1466 return sun.misc.FpUtils.scalb(f, scaleFactor);