2 * Copyright (c) 1994, 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
30 * The class {@code Math} contains methods for performing basic
31 * numeric operations such as the elementary exponential, logarithm,
32 * square root, and trigonometric functions.
34 * <p>Unlike some of the numeric methods of class
35 * {@code StrictMath}, all implementations of the equivalent
36 * functions of class {@code Math} are not defined to return the
37 * bit-for-bit same results. This relaxation permits
38 * better-performing implementations where strict reproducibility is
41 * <p>By default many of the {@code Math} methods simply call
42 * the equivalent method in {@code StrictMath} for their
43 * implementation. Code generators are encouraged to use
44 * platform-specific native libraries or microprocessor instructions,
45 * where available, to provide higher-performance implementations of
46 * {@code Math} methods. Such higher-performance
47 * implementations still must conform to the specification for
50 * <p>The quality of implementation specifications concern two
51 * properties, accuracy of the returned result and monotonicity of the
52 * method. Accuracy of the floating-point {@code Math} methods
53 * is measured in terms of <i>ulps</i>, units in the last place. For
54 * a given floating-point format, an ulp of a specific real number
55 * value is the distance between the two floating-point values
56 * bracketing that numerical value. When discussing the accuracy of a
57 * method as a whole rather than at a specific argument, the number of
58 * ulps cited is for the worst-case error at any argument. If a
59 * method always has an error less than 0.5 ulps, the method always
60 * returns the floating-point number nearest the exact result; such a
61 * method is <i>correctly rounded</i>. A correctly rounded method is
62 * generally the best a floating-point approximation can be; however,
63 * it is impractical for many floating-point methods to be correctly
64 * rounded. Instead, for the {@code Math} class, a larger error
65 * bound of 1 or 2 ulps is allowed for certain methods. Informally,
66 * with a 1 ulp error bound, when the exact result is a representable
67 * number, the exact result should be returned as the computed result;
68 * otherwise, either of the two floating-point values which bracket
69 * the exact result may be returned. For exact results large in
70 * magnitude, one of the endpoints of the bracket may be infinite.
71 * Besides accuracy at individual arguments, maintaining proper
72 * relations between the method at different arguments is also
73 * important. Therefore, most methods with more than 0.5 ulp errors
74 * are required to be <i>semi-monotonic</i>: whenever the mathematical
75 * function is non-decreasing, so is the floating-point approximation,
76 * likewise, whenever the mathematical function is non-increasing, so
77 * is the floating-point approximation. Not all approximations that
78 * have 1 ulp accuracy will automatically meet the monotonicity
82 * @author Joseph D. Darcy
86 public final class Math {
89 * Don't let anyone instantiate this class.
94 * The {@code double} value that is closer than any other to
95 * <i>e</i>, the base of the natural logarithms.
97 public static final double E = 2.7182818284590452354;
100 * The {@code double} value that is closer than any other to
101 * <i>pi</i>, the ratio of the circumference of a circle to its
104 public static final double PI = 3.14159265358979323846;
107 * Returns the trigonometric sine of an angle. Special cases:
108 * <ul><li>If the argument is NaN or an infinity, then the
110 * <li>If the argument is zero, then the result is a zero with the
111 * same sign as the argument.</ul>
113 * <p>The computed result must be within 1 ulp of the exact result.
114 * Results must be semi-monotonic.
116 * @param a an angle, in radians.
117 * @return the sine of the argument.
119 public static double sin(double a) {
120 return StrictMath.sin(a); // default impl. delegates to StrictMath
124 * Returns the trigonometric cosine of an angle. Special cases:
125 * <ul><li>If the argument is NaN or an infinity, then the
126 * result is NaN.</ul>
128 * <p>The computed result must be within 1 ulp of the exact result.
129 * Results must be semi-monotonic.
131 * @param a an angle, in radians.
132 * @return the cosine of the argument.
134 public static double cos(double a) {
135 return StrictMath.cos(a); // default impl. delegates to StrictMath
139 * Returns the trigonometric tangent of an angle. Special cases:
140 * <ul><li>If the argument is NaN or an infinity, then the result
142 * <li>If the argument is zero, then the result is a zero with the
143 * same sign as the argument.</ul>
145 * <p>The computed result must be within 1 ulp of the exact result.
146 * Results must be semi-monotonic.
148 * @param a an angle, in radians.
149 * @return the tangent of the argument.
151 public static double tan(double a) {
152 return StrictMath.tan(a); // default impl. delegates to StrictMath
156 * Returns the arc sine of a value; the returned angle is in the
157 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases:
158 * <ul><li>If the argument is NaN or its absolute value is greater
159 * than 1, then the result is NaN.
160 * <li>If the argument is zero, then the result is a zero with the
161 * same sign as the argument.</ul>
163 * <p>The computed result must be within 1 ulp of the exact result.
164 * Results must be semi-monotonic.
166 * @param a the value whose arc sine is to be returned.
167 * @return the arc sine of the argument.
169 public static double asin(double a) {
170 return StrictMath.asin(a); // default impl. delegates to StrictMath
174 * Returns the arc cosine of a value; the returned angle is in the
175 * range 0.0 through <i>pi</i>. Special case:
176 * <ul><li>If the argument is NaN or its absolute value is greater
177 * than 1, then the result is NaN.</ul>
179 * <p>The computed result must be within 1 ulp of the exact result.
180 * Results must be semi-monotonic.
182 * @param a the value whose arc cosine is to be returned.
183 * @return the arc cosine of the argument.
185 public static double acos(double a) {
186 return StrictMath.acos(a); // default impl. delegates to StrictMath
190 * Returns the arc tangent of a value; the returned angle is in the
191 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases:
192 * <ul><li>If the argument is NaN, then the result is NaN.
193 * <li>If the argument is zero, then the result is a zero with the
194 * same sign as the argument.</ul>
196 * <p>The computed result must be within 1 ulp of the exact result.
197 * Results must be semi-monotonic.
199 * @param a the value whose arc tangent is to be returned.
200 * @return the arc tangent of the argument.
202 public static double atan(double a) {
203 return StrictMath.atan(a); // default impl. delegates to StrictMath
207 * Converts an angle measured in degrees to an approximately
208 * equivalent angle measured in radians. The conversion from
209 * degrees to radians is generally inexact.
211 * @param angdeg an angle, in degrees
212 * @return the measurement of the angle {@code angdeg}
216 public static double toRadians(double angdeg) {
217 return angdeg / 180.0 * PI;
221 * Converts an angle measured in radians to an approximately
222 * equivalent angle measured in degrees. The conversion from
223 * radians to degrees is generally inexact; users should
224 * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
227 * @param angrad an angle, in radians
228 * @return the measurement of the angle {@code angrad}
232 public static double toDegrees(double angrad) {
233 return angrad * 180.0 / PI;
237 * Returns Euler's number <i>e</i> raised to the power of a
238 * {@code double} value. Special cases:
239 * <ul><li>If the argument is NaN, the result is NaN.
240 * <li>If the argument is positive infinity, then the result is
242 * <li>If the argument is negative infinity, then the result is
243 * positive zero.</ul>
245 * <p>The computed result must be within 1 ulp of the exact result.
246 * Results must be semi-monotonic.
248 * @param a the exponent to raise <i>e</i> to.
249 * @return the value <i>e</i><sup>{@code a}</sup>,
250 * where <i>e</i> is the base of the natural logarithms.
252 public static double exp(double a) {
253 return StrictMath.exp(a); // default impl. delegates to StrictMath
257 * Returns the natural logarithm (base <i>e</i>) of a {@code double}
258 * value. Special cases:
259 * <ul><li>If the argument is NaN or less than zero, then the result
261 * <li>If the argument is positive infinity, then the result is
263 * <li>If the argument is positive zero or negative zero, then the
264 * result is negative infinity.</ul>
266 * <p>The computed result must be within 1 ulp of the exact result.
267 * Results must be semi-monotonic.
270 * @return the value ln {@code a}, the natural logarithm of
273 public static double log(double a) {
274 return StrictMath.log(a); // default impl. delegates to StrictMath
278 * Returns the base 10 logarithm of a {@code double} value.
281 * <ul><li>If the argument is NaN or less than zero, then the result
283 * <li>If the argument is positive infinity, then the result is
285 * <li>If the argument is positive zero or negative zero, then the
286 * result is negative infinity.
287 * <li> If the argument is equal to 10<sup><i>n</i></sup> for
288 * integer <i>n</i>, then the result is <i>n</i>.
291 * <p>The computed result must be within 1 ulp of the exact result.
292 * Results must be semi-monotonic.
295 * @return the base 10 logarithm of {@code a}.
298 public static double log10(double a) {
299 return StrictMath.log10(a); // default impl. delegates to StrictMath
303 * Returns the correctly rounded positive square root of a
304 * {@code double} value.
306 * <ul><li>If the argument is NaN or less than zero, then the result
308 * <li>If the argument is positive infinity, then the result is positive
310 * <li>If the argument is positive zero or negative zero, then the
311 * result is the same as the argument.</ul>
312 * Otherwise, the result is the {@code double} value closest to
313 * the true mathematical square root of the argument value.
316 * @return the positive square root of {@code a}.
317 * If the argument is NaN or less than zero, the result is NaN.
319 public static double sqrt(double a) {
320 return StrictMath.sqrt(a); // default impl. delegates to StrictMath
321 // Note that hardware sqrt instructions
322 // frequently can be directly used by JITs
323 // and should be much faster than doing
324 // Math.sqrt in software.
329 * Returns the cube root of a {@code double} value. For
330 * positive finite {@code x}, {@code cbrt(-x) ==
331 * -cbrt(x)}; that is, the cube root of a negative value is
332 * the negative of the cube root of that value's magnitude.
338 * <li>If the argument is NaN, then the result is NaN.
340 * <li>If the argument is infinite, then the result is an infinity
341 * with the same sign as the argument.
343 * <li>If the argument is zero, then the result is a zero with the
344 * same sign as the argument.
348 * <p>The computed result must be within 1 ulp of the exact result.
351 * @return the cube root of {@code a}.
354 public static double cbrt(double a) {
355 return StrictMath.cbrt(a);
359 * Computes the remainder operation on two arguments as prescribed
360 * by the IEEE 754 standard.
361 * The remainder value is mathematically equal to
362 * <code>f1 - f2</code> × <i>n</i>,
363 * where <i>n</i> is the mathematical integer closest to the exact
364 * mathematical value of the quotient {@code f1/f2}, and if two
365 * mathematical integers are equally close to {@code f1/f2},
366 * then <i>n</i> is the integer that is even. If the remainder is
367 * zero, its sign is the same as the sign of the first argument.
369 * <ul><li>If either argument is NaN, or the first argument is infinite,
370 * or the second argument is positive zero or negative zero, then the
372 * <li>If the first argument is finite and the second argument is
373 * infinite, then the result is the same as the first argument.</ul>
375 * @param f1 the dividend.
376 * @param f2 the divisor.
377 * @return the remainder when {@code f1} is divided by
380 public static double IEEEremainder(double f1, double f2) {
381 return StrictMath.IEEEremainder(f1, f2); // delegate to StrictMath
385 * Returns the smallest (closest to negative infinity)
386 * {@code double} value that is greater than or equal to the
387 * argument and is equal to a mathematical integer. Special cases:
388 * <ul><li>If the argument value is already equal to a
389 * mathematical integer, then the result is the same as the
390 * argument. <li>If the argument is NaN or an infinity or
391 * positive zero or negative zero, then the result is the same as
392 * the argument. <li>If the argument value is less than zero but
393 * greater than -1.0, then the result is negative zero.</ul> Note
394 * that the value of {@code Math.ceil(x)} is exactly the
395 * value of {@code -Math.floor(-x)}.
399 * @return the smallest (closest to negative infinity)
400 * floating-point value that is greater than or equal to
401 * the argument and is equal to a mathematical integer.
403 public static double ceil(double a) {
404 return StrictMath.ceil(a); // default impl. delegates to StrictMath
408 * Returns the largest (closest to positive infinity)
409 * {@code double} value that is less than or equal to the
410 * argument and is equal to a mathematical integer. Special cases:
411 * <ul><li>If the argument value is already equal to a
412 * mathematical integer, then the result is the same as the
413 * argument. <li>If the argument is NaN or an infinity or
414 * positive zero or negative zero, then the result is the same as
418 * @return the largest (closest to positive infinity)
419 * floating-point value that less than or equal to the argument
420 * and is equal to a mathematical integer.
422 public static double floor(double a) {
423 return StrictMath.floor(a); // default impl. delegates to StrictMath
427 * Returns the {@code double} value that is closest in value
428 * to the argument and is equal to a mathematical integer. If two
429 * {@code double} values that are mathematical integers are
430 * equally close, the result is the integer value that is
431 * even. Special cases:
432 * <ul><li>If the argument value is already equal to a mathematical
433 * integer, then the result is the same as the argument.
434 * <li>If the argument is NaN or an infinity or positive zero or negative
435 * zero, then the result is the same as the argument.</ul>
437 * @param a a {@code double} value.
438 * @return the closest floating-point value to {@code a} that is
439 * equal to a mathematical integer.
441 public static double rint(double a) {
442 return StrictMath.rint(a); // default impl. delegates to StrictMath
446 * Returns the angle <i>theta</i> from the conversion of rectangular
447 * coordinates ({@code x}, {@code y}) to polar
448 * coordinates (r, <i>theta</i>).
449 * This method computes the phase <i>theta</i> by computing an arc tangent
450 * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special
452 * <ul><li>If either argument is NaN, then the result is NaN.
453 * <li>If the first argument is positive zero and the second argument
454 * is positive, or the first argument is positive and finite and the
455 * second argument is positive infinity, then the result is positive
457 * <li>If the first argument is negative zero and the second argument
458 * is positive, or the first argument is negative and finite and the
459 * second argument is positive infinity, then the result is negative zero.
460 * <li>If the first argument is positive zero and the second argument
461 * is negative, or the first argument is positive 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 negative zero and the second argument
465 * is negative, or the first argument is negative and finite and the
466 * second argument is negative infinity, then the result is the
467 * {@code double} value closest to -<i>pi</i>.
468 * <li>If the first argument is positive and the second argument is
469 * positive zero or negative zero, or the first argument is positive
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 the first argument is negative and the second argument is
473 * positive zero or negative zero, or the first argument is negative
474 * infinity and the second argument is finite, then the result is the
475 * {@code double} value closest to -<i>pi</i>/2.
476 * <li>If both arguments are positive infinity, then the result is the
477 * {@code double} value closest to <i>pi</i>/4.
478 * <li>If the first argument is positive infinity and the second argument
479 * is negative infinity, then the result is the {@code double}
480 * value closest to 3*<i>pi</i>/4.
481 * <li>If the first argument is negative infinity and the second argument
482 * is positive infinity, then the result is the {@code double} value
483 * closest to -<i>pi</i>/4.
484 * <li>If both arguments are negative infinity, then the result is the
485 * {@code double} value closest to -3*<i>pi</i>/4.</ul>
487 * <p>The computed result must be within 2 ulps of the exact result.
488 * Results must be semi-monotonic.
490 * @param y the ordinate coordinate
491 * @param x the abscissa coordinate
492 * @return the <i>theta</i> component of the point
493 * (<i>r</i>, <i>theta</i>)
494 * in polar coordinates that corresponds to the point
495 * (<i>x</i>, <i>y</i>) in Cartesian coordinates.
497 public static double atan2(double y, double x) {
498 return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
502 * Returns the value of the first argument raised to the power of the
503 * second argument. Special cases:
505 * <ul><li>If the second argument is positive or negative zero, then the
507 * <li>If the second argument is 1.0, then the result is the same as the
509 * <li>If the second argument is NaN, then the result is NaN.
510 * <li>If the first argument is NaN and the second argument is nonzero,
511 * then the result is NaN.
515 * <li>the absolute value of the first argument is greater than 1
516 * and the second argument is positive infinity, or
517 * <li>the absolute value of the first argument is less than 1 and
518 * the second argument is negative infinity,
520 * then the result is positive infinity.
524 * <li>the absolute value of the first argument is greater than 1 and
525 * the second argument is negative infinity, or
526 * <li>the absolute value of the
527 * first argument is less than 1 and the second argument is positive
530 * then the result is positive zero.
532 * <li>If the absolute value of the first argument equals 1 and the
533 * second argument is infinite, then the result is NaN.
537 * <li>the first argument is positive zero and the second argument
538 * is greater than zero, or
539 * <li>the first argument is positive infinity and the second
540 * argument is less than zero,
542 * then the result is positive zero.
546 * <li>the first argument is positive zero and the second argument
547 * is less than zero, or
548 * <li>the first argument is positive infinity and the second
549 * argument is greater than zero,
551 * then the result is positive infinity.
555 * <li>the first argument is negative zero and the second argument
556 * is greater than zero but not a finite odd integer, or
557 * <li>the first argument is negative infinity and the second
558 * argument is less than zero but not a finite odd integer,
560 * then the result is positive zero.
564 * <li>the first argument is negative zero and the second argument
565 * is a positive finite odd integer, or
566 * <li>the first argument is negative infinity and the second
567 * argument is a negative finite odd integer,
569 * then the result is negative zero.
573 * <li>the first argument is negative zero and the second argument
574 * is less than zero but not a finite odd integer, or
575 * <li>the first argument is negative infinity and the second
576 * argument is greater than zero but not a finite odd integer,
578 * then the result is positive infinity.
582 * <li>the first argument is negative zero and the second argument
583 * is a negative finite odd integer, or
584 * <li>the first argument is negative infinity and the second
585 * argument is a positive finite odd integer,
587 * then the result is negative infinity.
589 * <li>If the first argument is finite and less than zero
591 * <li> if the second argument is a finite even integer, the
592 * result is equal to the result of raising the absolute value of
593 * the first argument to the power of the second argument
595 * <li>if the second argument is a finite odd integer, the result
596 * is equal to the negative of the result of raising the absolute
597 * value of the first argument to the power of the second
600 * <li>if the second argument is finite and not an integer, then
604 * <li>If both arguments are integers, then the result is exactly equal
605 * to the mathematical result of raising the first argument to the power
606 * of the second argument if that result can in fact be represented
607 * exactly as a {@code double} value.</ul>
609 * <p>(In the foregoing descriptions, a floating-point value is
610 * considered to be an integer if and only if it is finite and a
611 * fixed point of the method {@link #ceil ceil} or,
612 * equivalently, a fixed point of the method {@link #floor
613 * floor}. A value is a fixed point of a one-argument
614 * method if and only if the result of applying the method to the
615 * value is equal to the value.)
617 * <p>The computed result must be within 1 ulp of the exact result.
618 * Results must be semi-monotonic.
621 * @param b the exponent.
622 * @return the value {@code a}<sup>{@code b}</sup>.
624 public static double pow(double a, double b) {
625 return StrictMath.pow(a, b); // default impl. delegates to StrictMath
629 * Returns the closest {@code int} to the argument, with ties
634 * <ul><li>If the argument is NaN, the result is 0.
635 * <li>If the argument is negative infinity or any value less than or
636 * equal to the value of {@code Integer.MIN_VALUE}, the result is
637 * equal to the value of {@code Integer.MIN_VALUE}.
638 * <li>If the argument is positive infinity or any value greater than or
639 * equal to the value of {@code Integer.MAX_VALUE}, the result is
640 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
642 * @param a a floating-point value to be rounded to an integer.
643 * @return the value of the argument rounded to the nearest
645 * @see java.lang.Integer#MAX_VALUE
646 * @see java.lang.Integer#MIN_VALUE
648 public static int round(float a) {
649 if (a != 0x1.fffffep-2f) // greatest float value less than 0.5
650 return (int)floor(a + 0.5f);
656 * Returns the closest {@code long} to the argument, with ties
660 * <ul><li>If the argument is NaN, the result is 0.
661 * <li>If the argument is negative infinity or any value less than or
662 * equal to the value of {@code Long.MIN_VALUE}, the result is
663 * equal to the value of {@code Long.MIN_VALUE}.
664 * <li>If the argument is positive infinity or any value greater than or
665 * equal to the value of {@code Long.MAX_VALUE}, the result is
666 * equal to the value of {@code Long.MAX_VALUE}.</ul>
668 * @param a a floating-point value to be rounded to a
670 * @return the value of the argument rounded to the nearest
671 * {@code long} value.
672 * @see java.lang.Long#MAX_VALUE
673 * @see java.lang.Long#MIN_VALUE
675 public static long round(double a) {
676 if (a != 0x1.fffffffffffffp-2) // greatest double value less than 0.5
677 return (long)floor(a + 0.5d);
682 // private static Random randomNumberGenerator;
684 // private static synchronized Random initRNG() {
685 // Random rnd = randomNumberGenerator;
686 // return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
690 * Returns a {@code double} value with a positive sign, greater
691 * than or equal to {@code 0.0} and less than {@code 1.0}.
692 * Returned values are chosen pseudorandomly with (approximately)
693 * uniform distribution from that range.
695 * <p>When this method is first called, it creates a single new
696 * pseudorandom-number generator, exactly as if by the expression
698 * <blockquote>{@code new java.util.Random()}</blockquote>
700 * This new pseudorandom-number generator is used thereafter for
701 * all calls to this method and is used nowhere else.
703 * <p>This method is properly synchronized to allow correct use by
704 * more than one thread. However, if many threads need to generate
705 * pseudorandom numbers at a great rate, it may reduce contention
706 * for each thread to have its own pseudorandom-number generator.
708 * @return a pseudorandom {@code double} greater than or equal
709 * to {@code 0.0} and less than {@code 1.0}.
710 * @see Random#nextDouble()
712 public static double random() {
713 throw new UnsupportedOperationException();
717 * Returns the absolute value of an {@code int} value.
718 * If the argument is not negative, the argument is returned.
719 * If the argument is negative, the negation of the argument is returned.
721 * <p>Note that if the argument is equal to the value of
722 * {@link Integer#MIN_VALUE}, the most negative representable
723 * {@code int} value, the result is that same value, which is
726 * @param a the argument whose absolute value is to be determined
727 * @return the absolute value of the argument.
729 public static int abs(int a) {
730 return (a < 0) ? -a : a;
734 * Returns the absolute value of a {@code long} value.
735 * If the argument is not negative, the argument is returned.
736 * If the argument is negative, the negation of the argument is returned.
738 * <p>Note that if the argument is equal to the value of
739 * {@link Long#MIN_VALUE}, the most negative representable
740 * {@code long} value, the result is that same value, which
743 * @param a the argument whose absolute value is to be determined
744 * @return the absolute value of the argument.
746 public static long abs(long a) {
747 return (a < 0) ? -a : a;
751 * Returns the absolute value of a {@code float} value.
752 * If the argument is not negative, the argument is returned.
753 * If the argument is negative, the negation of the argument is returned.
755 * <ul><li>If the argument is positive zero or negative zero, the
756 * result is positive zero.
757 * <li>If the argument is infinite, the result is positive infinity.
758 * <li>If the argument is NaN, the result is NaN.</ul>
759 * In other words, the result is the same as the value of the expression:
760 * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))}
762 * @param a the argument whose absolute value is to be determined
763 * @return the absolute value of the argument.
765 public static float abs(float a) {
766 return (a <= 0.0F) ? 0.0F - a : a;
770 * Returns the absolute value of a {@code double} value.
771 * If the argument is not negative, the argument is returned.
772 * If the argument is negative, the negation of the argument is returned.
774 * <ul><li>If the argument is positive zero or negative zero, the result
776 * <li>If the argument is infinite, the result is positive infinity.
777 * <li>If the argument is NaN, the result is NaN.</ul>
778 * In other words, the result is the same as the value of the expression:
779 * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)}
781 * @param a the argument whose absolute value is to be determined
782 * @return the absolute value of the argument.
784 public static double abs(double a) {
785 return (a <= 0.0D) ? 0.0D - a : a;
789 * Returns the greater of two {@code int} values. That is, the
790 * result is the argument closer to the value of
791 * {@link Integer#MAX_VALUE}. If the arguments have the same value,
792 * the result is that same value.
794 * @param a an argument.
795 * @param b another argument.
796 * @return the larger of {@code a} and {@code b}.
798 public static int max(int a, int b) {
799 return (a >= b) ? a : b;
803 * Returns the greater of two {@code long} values. That is, the
804 * result is the argument closer to the value of
805 * {@link Long#MAX_VALUE}. If the arguments have the same value,
806 * the result is that same value.
808 * @param a an argument.
809 * @param b another argument.
810 * @return the larger of {@code a} and {@code b}.
812 public static long max(long a, long b) {
813 return (a >= b) ? a : b;
816 private static long negativeZeroFloatBits = Float.floatToIntBits(-0.0f);
817 private static long negativeZeroDoubleBits = Double.doubleToLongBits(-0.0d);
820 * Returns the greater of two {@code float} values. That is,
821 * the result is the argument closer to positive infinity. If the
822 * arguments have the same value, the result is that same
823 * value. If either value is NaN, then the result is NaN. Unlike
824 * the numerical comparison operators, this method considers
825 * negative zero to be strictly smaller than positive zero. If one
826 * argument is positive zero and the other negative zero, the
827 * result is positive zero.
829 * @param a an argument.
830 * @param b another argument.
831 * @return the larger of {@code a} and {@code b}.
833 public static float max(float a, float b) {
834 if (a != a) return a; // a is NaN
835 if ((a == 0.0f) && (b == 0.0f)
836 && (Float.floatToIntBits(a) == negativeZeroFloatBits)) {
839 return (a >= b) ? a : b;
843 * Returns the greater of two {@code double} values. That
844 * is, the result is the argument closer to positive infinity. If
845 * the arguments have the same value, the result is that same
846 * value. If either value is NaN, then the result is NaN. Unlike
847 * the numerical comparison operators, this method considers
848 * negative zero to be strictly smaller than positive zero. If one
849 * argument is positive zero and the other negative zero, the
850 * result is positive zero.
852 * @param a an argument.
853 * @param b another argument.
854 * @return the larger of {@code a} and {@code b}.
856 public static double max(double a, double b) {
857 if (a != a) return a; // a is NaN
858 if ((a == 0.0d) && (b == 0.0d)
859 && (Double.doubleToLongBits(a) == negativeZeroDoubleBits)) {
862 return (a >= b) ? a : b;
866 * Returns the smaller of two {@code int} values. That is,
867 * the result the argument closer to the value of
868 * {@link Integer#MIN_VALUE}. If the arguments have the same
869 * value, the result is that same value.
871 * @param a an argument.
872 * @param b another argument.
873 * @return the smaller of {@code a} and {@code b}.
875 public static int min(int a, int b) {
876 return (a <= b) ? a : b;
880 * Returns the smaller of two {@code long} values. That is,
881 * the result is the argument closer to the value of
882 * {@link Long#MIN_VALUE}. If the arguments have the same
883 * value, the result is that same value.
885 * @param a an argument.
886 * @param b another argument.
887 * @return the smaller of {@code a} and {@code b}.
889 public static long min(long a, long b) {
890 return (a <= b) ? a : b;
894 * Returns the smaller of two {@code float} values. That is,
895 * the result is the value closer to negative infinity. If the
896 * arguments have the same value, the result is that same
897 * value. If either value is NaN, then the result is NaN. Unlike
898 * the numerical comparison operators, this method considers
899 * negative zero to be strictly smaller than positive zero. If
900 * one argument is positive zero and the other is negative zero,
901 * the result is negative zero.
903 * @param a an argument.
904 * @param b another argument.
905 * @return the smaller of {@code a} and {@code b}.
907 public static float min(float a, float b) {
908 if (a != a) return a; // a is NaN
909 if ((a == 0.0f) && (b == 0.0f)
910 && (Float.floatToIntBits(b) == negativeZeroFloatBits)) {
913 return (a <= b) ? a : b;
917 * Returns the smaller of two {@code double} values. That
918 * is, the result is the value closer to negative infinity. If the
919 * arguments have the same value, the result is that same
920 * value. If either value is NaN, then the result is NaN. Unlike
921 * the numerical comparison operators, this method considers
922 * negative zero to be strictly smaller than positive zero. If one
923 * argument is positive zero and the other is negative zero, the
924 * result is negative zero.
926 * @param a an argument.
927 * @param b another argument.
928 * @return the smaller of {@code a} and {@code b}.
930 public static double min(double a, double b) {
931 if (a != a) return a; // a is NaN
932 if ((a == 0.0d) && (b == 0.0d)
933 && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
936 return (a <= b) ? a : b;
940 * Returns the size of an ulp of the argument. An ulp of a
941 * {@code double} value is the positive distance between this
942 * floating-point value and the {@code double} value next
943 * larger in magnitude. Note that for non-NaN <i>x</i>,
944 * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
948 * <li> If the argument is NaN, then the result is NaN.
949 * <li> If the argument is positive or negative infinity, then the
950 * result is positive infinity.
951 * <li> If the argument is positive or negative zero, then the result is
952 * {@code Double.MIN_VALUE}.
953 * <li> If the argument is ±{@code Double.MAX_VALUE}, then
954 * the result is equal to 2<sup>971</sup>.
957 * @param d the floating-point value whose ulp is to be returned
958 * @return the size of an ulp of the argument
959 * @author Joseph D. Darcy
962 // public static double ulp(double d) {
963 // return sun.misc.FpUtils.ulp(d);
967 * Returns the size of an ulp of the argument. An ulp of a
968 * {@code float} value is the positive distance between this
969 * floating-point value and the {@code float} value next
970 * larger in magnitude. Note that for non-NaN <i>x</i>,
971 * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
975 * <li> If the argument is NaN, then the result is NaN.
976 * <li> If the argument is positive or negative infinity, then the
977 * result is positive infinity.
978 * <li> If the argument is positive or negative zero, then the result is
979 * {@code Float.MIN_VALUE}.
980 * <li> If the argument is ±{@code Float.MAX_VALUE}, then
981 * the result is equal to 2<sup>104</sup>.
984 * @param f the floating-point value whose ulp is to be returned
985 * @return the size of an ulp of the argument
986 * @author Joseph D. Darcy
989 // public static float ulp(float f) {
990 // return sun.misc.FpUtils.ulp(f);
994 * Returns the signum function of the argument; zero if the argument
995 * is zero, 1.0 if the argument is greater than zero, -1.0 if the
996 * argument is less than zero.
1000 * <li> If the argument is NaN, then the result is NaN.
1001 * <li> If the argument is positive zero or negative zero, then the
1002 * result is the same as the argument.
1005 * @param d the floating-point value whose signum is to be returned
1006 * @return the signum function of the argument
1007 * @author Joseph D. Darcy
1010 // public static double signum(double d) {
1011 // return sun.misc.FpUtils.signum(d);
1015 * Returns the signum function of the argument; zero if the argument
1016 * is zero, 1.0f if the argument is greater than zero, -1.0f if the
1017 * argument is less than zero.
1021 * <li> If the argument is NaN, then the result is NaN.
1022 * <li> If the argument is positive zero or negative zero, then the
1023 * result is the same as the argument.
1026 * @param f the floating-point value whose signum is to be returned
1027 * @return the signum function of the argument
1028 * @author Joseph D. Darcy
1031 // public static float signum(float f) {
1032 // return sun.misc.FpUtils.signum(f);
1036 * Returns the hyperbolic sine of a {@code double} value.
1037 * The hyperbolic sine of <i>x</i> is defined to be
1038 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2
1039 * where <i>e</i> is {@linkplain Math#E Euler's number}.
1044 * <li>If the argument is NaN, then the result is NaN.
1046 * <li>If the argument is infinite, then the result is an infinity
1047 * with the same sign as the argument.
1049 * <li>If the argument is zero, then the result is a zero with the
1050 * same sign as the argument.
1054 * <p>The computed result must be within 2.5 ulps of the exact result.
1056 * @param x The number whose hyperbolic sine is to be returned.
1057 * @return The hyperbolic sine of {@code x}.
1060 public static double sinh(double x) {
1061 return StrictMath.sinh(x);
1065 * Returns the hyperbolic cosine of a {@code double} value.
1066 * The hyperbolic cosine of <i>x</i> is defined to be
1067 * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2
1068 * where <i>e</i> is {@linkplain Math#E Euler's number}.
1073 * <li>If the argument is NaN, then the result is NaN.
1075 * <li>If the argument is infinite, then the result is positive
1078 * <li>If the argument is zero, then the result is {@code 1.0}.
1082 * <p>The computed result must be within 2.5 ulps of the exact result.
1084 * @param x The number whose hyperbolic cosine is to be returned.
1085 * @return The hyperbolic cosine of {@code x}.
1088 public static double cosh(double x) {
1089 return StrictMath.cosh(x);
1093 * Returns the hyperbolic tangent of a {@code double} value.
1094 * The hyperbolic tangent of <i>x</i> is defined to be
1095 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>),
1096 * in other words, {@linkplain Math#sinh
1097 * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note
1098 * that the absolute value of the exact tanh is always less than
1104 * <li>If the argument is NaN, then the result is NaN.
1106 * <li>If the argument is zero, then the result is a zero with the
1107 * same sign as the argument.
1109 * <li>If the argument is positive infinity, then the result is
1112 * <li>If the argument is negative infinity, then the result is
1117 * <p>The computed result must be within 2.5 ulps of the exact result.
1118 * The result of {@code tanh} for any finite input must have
1119 * an absolute value less than or equal to 1. Note that once the
1120 * exact result of tanh is within 1/2 of an ulp of the limit value
1121 * of ±1, correctly signed ±{@code 1.0} should
1124 * @param x The number whose hyperbolic tangent is to be returned.
1125 * @return The hyperbolic tangent of {@code x}.
1128 public static double tanh(double x) {
1129 return StrictMath.tanh(x);
1133 * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
1134 * without intermediate overflow or underflow.
1139 * <li> If either argument is infinite, then the result
1140 * is positive infinity.
1142 * <li> If either argument is NaN and neither argument is infinite,
1143 * then the result is NaN.
1147 * <p>The computed result must be within 1 ulp of the exact
1148 * result. If one parameter is held constant, the results must be
1149 * semi-monotonic in the other parameter.
1153 * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
1154 * without intermediate overflow or underflow
1157 public static double hypot(double x, double y) {
1158 return StrictMath.hypot(x, y);
1162 * Returns <i>e</i><sup>x</sup> -1. Note that for values of
1163 * <i>x</i> near 0, the exact sum of
1164 * {@code expm1(x)} + 1 is much closer to the true
1165 * result of <i>e</i><sup>x</sup> than {@code exp(x)}.
1169 * <li>If the argument is NaN, the result is NaN.
1171 * <li>If the argument is positive infinity, then the result is
1172 * positive infinity.
1174 * <li>If the argument is negative infinity, then the result is
1177 * <li>If the argument is zero, then the result is a zero with the
1178 * same sign as the argument.
1182 * <p>The computed result must be within 1 ulp of the exact result.
1183 * Results must be semi-monotonic. The result of
1184 * {@code expm1} for any finite input must be greater than or
1185 * equal to {@code -1.0}. Note that once the exact result of
1186 * <i>e</i><sup>{@code x}</sup> - 1 is within 1/2
1187 * ulp of the limit value -1, {@code -1.0} should be
1190 * @param x the exponent to raise <i>e</i> to in the computation of
1191 * <i>e</i><sup>{@code x}</sup> -1.
1192 * @return the value <i>e</i><sup>{@code x}</sup> - 1.
1195 public static double expm1(double x) {
1196 return StrictMath.expm1(x);
1200 * Returns the natural logarithm of the sum of the argument and 1.
1201 * Note that for small values {@code x}, the result of
1202 * {@code log1p(x)} is much closer to the true result of ln(1
1203 * + {@code x}) than the floating-point evaluation of
1204 * {@code log(1.0+x)}.
1210 * <li>If the argument is NaN or less than -1, then the result is
1213 * <li>If the argument is positive infinity, then the result is
1214 * positive infinity.
1216 * <li>If the argument is negative one, then the result is
1217 * negative infinity.
1219 * <li>If the argument is zero, then the result is a zero with the
1220 * same sign as the argument.
1224 * <p>The computed result must be within 1 ulp of the exact result.
1225 * Results must be semi-monotonic.
1228 * @return the value ln({@code x} + 1), the natural
1229 * log of {@code x} + 1
1232 public static double log1p(double x) {
1233 return StrictMath.log1p(x);
1237 * Returns the first floating-point argument with the sign of the
1238 * second floating-point argument. Note that unlike the {@link
1239 * StrictMath#copySign(double, double) StrictMath.copySign}
1240 * method, this method does not require NaN {@code sign}
1241 * arguments to be treated as positive values; implementations are
1242 * permitted to treat some NaN arguments as positive and other NaN
1243 * arguments as negative to allow greater performance.
1245 * @param magnitude the parameter providing the magnitude of the result
1246 * @param sign the parameter providing the sign of the result
1247 * @return a value with the magnitude of {@code magnitude}
1248 * and the sign of {@code sign}.
1251 // public static double copySign(double magnitude, double sign) {
1252 // return sun.misc.FpUtils.rawCopySign(magnitude, sign);
1256 * Returns the first floating-point argument with the sign of the
1257 * second floating-point argument. Note that unlike the {@link
1258 * StrictMath#copySign(float, float) StrictMath.copySign}
1259 * method, this method does not require NaN {@code sign}
1260 * arguments to be treated as positive values; implementations are
1261 * permitted to treat some NaN arguments as positive and other NaN
1262 * arguments as negative to allow greater performance.
1264 * @param magnitude the parameter providing the magnitude of the result
1265 * @param sign the parameter providing the sign of the result
1266 * @return a value with the magnitude of {@code magnitude}
1267 * and the sign of {@code sign}.
1270 // public static float copySign(float magnitude, float sign) {
1271 // return sun.misc.FpUtils.rawCopySign(magnitude, sign);
1275 * Returns the unbiased exponent used in the representation of a
1276 * {@code float}. Special cases:
1279 * <li>If the argument is NaN or infinite, then the result is
1280 * {@link Float#MAX_EXPONENT} + 1.
1281 * <li>If the argument is zero or subnormal, then the result is
1282 * {@link Float#MIN_EXPONENT} -1.
1284 * @param f a {@code float} value
1285 * @return the unbiased exponent of the argument
1288 // public static int getExponent(float f) {
1289 // return sun.misc.FpUtils.getExponent(f);
1293 * Returns the unbiased exponent used in the representation of a
1294 * {@code double}. Special cases:
1297 * <li>If the argument is NaN or infinite, then the result is
1298 * {@link Double#MAX_EXPONENT} + 1.
1299 * <li>If the argument is zero or subnormal, then the result is
1300 * {@link Double#MIN_EXPONENT} -1.
1302 * @param d a {@code double} value
1303 * @return the unbiased exponent of the argument
1306 // public static int getExponent(double d) {
1307 // return sun.misc.FpUtils.getExponent(d);
1311 * Returns the floating-point number adjacent to the first
1312 * argument in the direction of the second argument. If both
1313 * arguments compare as equal the second argument is returned.
1318 * <li> If either argument is a NaN, then NaN is returned.
1320 * <li> If both arguments are signed zeros, {@code direction}
1321 * is returned unchanged (as implied by the requirement of
1322 * returning the second argument if the arguments compare as
1325 * <li> If {@code start} is
1326 * ±{@link Double#MIN_VALUE} and {@code direction}
1327 * has a value such that the result should have a smaller
1328 * magnitude, then a zero with the same sign as {@code start}
1331 * <li> If {@code start} is infinite and
1332 * {@code direction} has a value such that the result should
1333 * have a smaller magnitude, {@link Double#MAX_VALUE} with the
1334 * same sign as {@code start} is returned.
1336 * <li> If {@code start} is equal to ±
1337 * {@link Double#MAX_VALUE} and {@code direction} has a
1338 * value such that the result should have a larger magnitude, an
1339 * infinity with same sign as {@code start} is returned.
1342 * @param start starting floating-point value
1343 * @param direction value indicating which of
1344 * {@code start}'s neighbors or {@code start} should
1346 * @return The floating-point number adjacent to {@code start} in the
1347 * direction of {@code direction}.
1350 // public static double nextAfter(double start, double direction) {
1351 // return sun.misc.FpUtils.nextAfter(start, direction);
1355 * Returns the floating-point number adjacent to the first
1356 * argument in the direction of the second argument. If both
1357 * arguments compare as equal a value equivalent to the second argument
1363 * <li> If either argument is a NaN, then NaN is returned.
1365 * <li> If both arguments are signed zeros, a value equivalent
1366 * to {@code direction} is returned.
1368 * <li> If {@code start} is
1369 * ±{@link Float#MIN_VALUE} and {@code direction}
1370 * has a value such that the result should have a smaller
1371 * magnitude, then a zero with the same sign as {@code start}
1374 * <li> If {@code start} is infinite and
1375 * {@code direction} has a value such that the result should
1376 * have a smaller magnitude, {@link Float#MAX_VALUE} with the
1377 * same sign as {@code start} is returned.
1379 * <li> If {@code start} is equal to ±
1380 * {@link Float#MAX_VALUE} and {@code direction} has a
1381 * value such that the result should have a larger magnitude, an
1382 * infinity with same sign as {@code start} is returned.
1385 * @param start starting floating-point value
1386 * @param direction value indicating which of
1387 * {@code start}'s neighbors or {@code start} should
1389 * @return The floating-point number adjacent to {@code start} in the
1390 * direction of {@code direction}.
1393 // public static float nextAfter(float start, double direction) {
1394 // return sun.misc.FpUtils.nextAfter(start, direction);
1398 * Returns the floating-point value adjacent to {@code d} in
1399 * the direction of positive infinity. This method is
1400 * semantically equivalent to {@code nextAfter(d,
1401 * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
1402 * implementation may run faster than its equivalent
1403 * {@code nextAfter} call.
1407 * <li> If the argument is NaN, the result is NaN.
1409 * <li> If the argument is positive infinity, the result is
1410 * positive infinity.
1412 * <li> If the argument is zero, the result is
1413 * {@link Double#MIN_VALUE}
1417 * @param d starting floating-point value
1418 * @return The adjacent floating-point value closer to positive
1422 // public static double nextUp(double d) {
1423 // return sun.misc.FpUtils.nextUp(d);
1427 * Returns the floating-point value adjacent to {@code f} in
1428 * the direction of positive infinity. This method is
1429 * semantically equivalent to {@code nextAfter(f,
1430 * Float.POSITIVE_INFINITY)}; however, a {@code nextUp}
1431 * implementation may run faster than its equivalent
1432 * {@code nextAfter} call.
1436 * <li> If the argument is NaN, the result is NaN.
1438 * <li> If the argument is positive infinity, the result is
1439 * positive infinity.
1441 * <li> If the argument is zero, the result is
1442 * {@link Float#MIN_VALUE}
1446 * @param f starting floating-point value
1447 * @return The adjacent floating-point value closer to positive
1451 // public static float nextUp(float f) {
1452 // return sun.misc.FpUtils.nextUp(f);
1457 * Return {@code d} ×
1458 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1459 * by a single correctly rounded floating-point multiply to a
1460 * member of the double value set. See the Java
1461 * Language Specification for a discussion of floating-point
1462 * value sets. If the exponent of the result is between {@link
1463 * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
1464 * answer is calculated exactly. If the exponent of the result
1465 * would be larger than {@code Double.MAX_EXPONENT}, an
1466 * infinity is returned. Note that if the result is subnormal,
1467 * precision may be lost; that is, when {@code scalb(x, n)}
1468 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1469 * <i>x</i>. When the result is non-NaN, the result has the same
1470 * sign as {@code d}.
1474 * <li> If the first argument is NaN, NaN is returned.
1475 * <li> If the first argument is infinite, then an infinity of the
1476 * same sign is returned.
1477 * <li> If the first argument is zero, then a zero of the same
1481 * @param d number to be scaled by a power of two.
1482 * @param scaleFactor power of 2 used to scale {@code d}
1483 * @return {@code d} × 2<sup>{@code scaleFactor}</sup>
1486 // public static double scalb(double d, int scaleFactor) {
1487 // return sun.misc.FpUtils.scalb(d, scaleFactor);
1491 * Return {@code f} ×
1492 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1493 * by a single correctly rounded floating-point multiply to a
1494 * member of the float value set. See the Java
1495 * Language Specification for a discussion of floating-point
1496 * value sets. If the exponent of the result is between {@link
1497 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
1498 * answer is calculated exactly. If the exponent of the result
1499 * would be larger than {@code Float.MAX_EXPONENT}, an
1500 * infinity is returned. Note that if the result is subnormal,
1501 * precision may be lost; that is, when {@code scalb(x, n)}
1502 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1503 * <i>x</i>. When the result is non-NaN, the result has the same
1504 * sign as {@code f}.
1508 * <li> If the first argument is NaN, NaN is returned.
1509 * <li> If the first argument is infinite, then an infinity of the
1510 * same sign is returned.
1511 * <li> If the first argument is zero, then a zero of the same
1515 * @param f number to be scaled by a power of two.
1516 * @param scaleFactor power of 2 used to scale {@code f}
1517 * @return {@code f} × 2<sup>{@code scaleFactor}</sup>
1520 // public static float scalb(float f, int scaleFactor) {
1521 // return sun.misc.FpUtils.scalb(f, scaleFactor);