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
28 import org.apidesign.bck2brwsr.core.JavaScriptBody;
32 * The class {@code Math} contains methods for performing basic
33 * numeric operations such as the elementary exponential, logarithm,
34 * square root, and trigonometric functions.
36 * <p>Unlike some of the numeric methods of class
37 * {@code StrictMath}, all implementations of the equivalent
38 * functions of class {@code Math} are not defined to return the
39 * bit-for-bit same results. This relaxation permits
40 * better-performing implementations where strict reproducibility is
43 * <p>By default many of the {@code Math} methods simply call
44 * the equivalent method in {@code StrictMath} for their
45 * implementation. Code generators are encouraged to use
46 * platform-specific native libraries or microprocessor instructions,
47 * where available, to provide higher-performance implementations of
48 * {@code Math} methods. Such higher-performance
49 * implementations still must conform to the specification for
52 * <p>The quality of implementation specifications concern two
53 * properties, accuracy of the returned result and monotonicity of the
54 * method. Accuracy of the floating-point {@code Math} methods
55 * is measured in terms of <i>ulps</i>, units in the last place. For
56 * a given floating-point format, an ulp of a specific real number
57 * value is the distance between the two floating-point values
58 * bracketing that numerical value. When discussing the accuracy of a
59 * method as a whole rather than at a specific argument, the number of
60 * ulps cited is for the worst-case error at any argument. If a
61 * method always has an error less than 0.5 ulps, the method always
62 * returns the floating-point number nearest the exact result; such a
63 * method is <i>correctly rounded</i>. A correctly rounded method is
64 * generally the best a floating-point approximation can be; however,
65 * it is impractical for many floating-point methods to be correctly
66 * rounded. Instead, for the {@code Math} class, a larger error
67 * bound of 1 or 2 ulps is allowed for certain methods. Informally,
68 * with a 1 ulp error bound, when the exact result is a representable
69 * number, the exact result should be returned as the computed result;
70 * otherwise, either of the two floating-point values which bracket
71 * the exact result may be returned. For exact results large in
72 * magnitude, one of the endpoints of the bracket may be infinite.
73 * Besides accuracy at individual arguments, maintaining proper
74 * relations between the method at different arguments is also
75 * important. Therefore, most methods with more than 0.5 ulp errors
76 * are required to be <i>semi-monotonic</i>: whenever the mathematical
77 * function is non-decreasing, so is the floating-point approximation,
78 * likewise, whenever the mathematical function is non-increasing, so
79 * is the floating-point approximation. Not all approximations that
80 * have 1 ulp accuracy will automatically meet the monotonicity
84 * @author Joseph D. Darcy
88 public final class Math {
91 * Don't let anyone instantiate this class.
96 * The {@code double} value that is closer than any other to
97 * <i>e</i>, the base of the natural logarithms.
99 public static final double E = 2.7182818284590452354;
102 * The {@code double} value that is closer than any other to
103 * <i>pi</i>, the ratio of the circumference of a circle to its
106 public static final double PI = 3.14159265358979323846;
109 * Returns the trigonometric sine of an angle. Special cases:
110 * <ul><li>If the argument is NaN or an infinity, then the
112 * <li>If the argument is zero, then the result is a zero with the
113 * same sign as the argument.</ul>
115 * <p>The computed result must be within 1 ulp of the exact result.
116 * Results must be semi-monotonic.
118 * @param a an angle, in radians.
119 * @return the sine of the argument.
121 public static double sin(double a) {
122 return StrictMath.sin(a); // default impl. delegates to StrictMath
126 * Returns the trigonometric cosine of an angle. Special cases:
127 * <ul><li>If the argument is NaN or an infinity, then the
128 * result is NaN.</ul>
130 * <p>The computed result must be within 1 ulp of the exact result.
131 * Results must be semi-monotonic.
133 * @param a an angle, in radians.
134 * @return the cosine of the argument.
136 public static double cos(double a) {
137 return StrictMath.cos(a); // default impl. delegates to StrictMath
141 * Returns the trigonometric tangent of an angle. Special cases:
142 * <ul><li>If the argument is NaN or an infinity, then the result
144 * <li>If the argument is zero, then the result is a zero with the
145 * same sign as the argument.</ul>
147 * <p>The computed result must be within 1 ulp of the exact result.
148 * Results must be semi-monotonic.
150 * @param a an angle, in radians.
151 * @return the tangent of the argument.
153 public static double tan(double a) {
154 return StrictMath.tan(a); // default impl. delegates to StrictMath
158 * Returns the arc sine of a value; the returned angle is in the
159 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases:
160 * <ul><li>If the argument is NaN or its absolute value is greater
161 * than 1, then the result is NaN.
162 * <li>If the argument is zero, then the result is a zero with the
163 * same sign as the argument.</ul>
165 * <p>The computed result must be within 1 ulp of the exact result.
166 * Results must be semi-monotonic.
168 * @param a the value whose arc sine is to be returned.
169 * @return the arc sine of the argument.
171 public static double asin(double a) {
172 return StrictMath.asin(a); // default impl. delegates to StrictMath
176 * Returns the arc cosine of a value; the returned angle is in the
177 * range 0.0 through <i>pi</i>. Special case:
178 * <ul><li>If the argument is NaN or its absolute value is greater
179 * than 1, then the result is NaN.</ul>
181 * <p>The computed result must be within 1 ulp of the exact result.
182 * Results must be semi-monotonic.
184 * @param a the value whose arc cosine is to be returned.
185 * @return the arc cosine of the argument.
187 public static double acos(double a) {
188 return StrictMath.acos(a); // default impl. delegates to StrictMath
192 * Returns the arc tangent of a value; the returned angle is in the
193 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases:
194 * <ul><li>If the argument is NaN, then the result is NaN.
195 * <li>If the argument is zero, then the result is a zero with the
196 * same sign as the argument.</ul>
198 * <p>The computed result must be within 1 ulp of the exact result.
199 * Results must be semi-monotonic.
201 * @param a the value whose arc tangent is to be returned.
202 * @return the arc tangent of the argument.
204 public static double atan(double a) {
205 return StrictMath.atan(a); // default impl. delegates to StrictMath
209 * Converts an angle measured in degrees to an approximately
210 * equivalent angle measured in radians. The conversion from
211 * degrees to radians is generally inexact.
213 * @param angdeg an angle, in degrees
214 * @return the measurement of the angle {@code angdeg}
218 public static double toRadians(double angdeg) {
219 return angdeg / 180.0 * PI;
223 * Converts an angle measured in radians to an approximately
224 * equivalent angle measured in degrees. The conversion from
225 * radians to degrees is generally inexact; users should
226 * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
229 * @param angrad an angle, in radians
230 * @return the measurement of the angle {@code angrad}
234 public static double toDegrees(double angrad) {
235 return angrad * 180.0 / PI;
239 * Returns Euler's number <i>e</i> raised to the power of a
240 * {@code double} value. Special cases:
241 * <ul><li>If the argument is NaN, the result is NaN.
242 * <li>If the argument is positive infinity, then the result is
244 * <li>If the argument is negative infinity, then the result is
245 * positive zero.</ul>
247 * <p>The computed result must be within 1 ulp of the exact result.
248 * Results must be semi-monotonic.
250 * @param a the exponent to raise <i>e</i> to.
251 * @return the value <i>e</i><sup>{@code a}</sup>,
252 * where <i>e</i> is the base of the natural logarithms.
254 public static double exp(double a) {
255 return StrictMath.exp(a); // default impl. delegates to StrictMath
259 * Returns the natural logarithm (base <i>e</i>) of a {@code double}
260 * value. Special cases:
261 * <ul><li>If the argument is NaN or less than zero, then the result
263 * <li>If the argument is positive infinity, then the result is
265 * <li>If the argument is positive zero or negative zero, then the
266 * result is negative infinity.</ul>
268 * <p>The computed result must be within 1 ulp of the exact result.
269 * Results must be semi-monotonic.
272 * @return the value ln {@code a}, the natural logarithm of
275 public static double log(double a) {
276 return StrictMath.log(a); // default impl. delegates to StrictMath
280 * Returns the base 10 logarithm of a {@code double} value.
283 * <ul><li>If the argument is NaN or less than zero, then the result
285 * <li>If the argument is positive infinity, then the result is
287 * <li>If the argument is positive zero or negative zero, then the
288 * result is negative infinity.
289 * <li> If the argument is equal to 10<sup><i>n</i></sup> for
290 * integer <i>n</i>, then the result is <i>n</i>.
293 * <p>The computed result must be within 1 ulp of the exact result.
294 * Results must be semi-monotonic.
297 * @return the base 10 logarithm of {@code a}.
300 public static double log10(double a) {
301 return StrictMath.log10(a); // default impl. delegates to StrictMath
305 * Returns the correctly rounded positive square root of a
306 * {@code double} value.
308 * <ul><li>If the argument is NaN or less than zero, then the result
310 * <li>If the argument is positive infinity, then the result is positive
312 * <li>If the argument is positive zero or negative zero, then the
313 * result is the same as the argument.</ul>
314 * Otherwise, the result is the {@code double} value closest to
315 * the true mathematical square root of the argument value.
318 * @return the positive square root of {@code a}.
319 * If the argument is NaN or less than zero, the result is NaN.
321 public static double sqrt(double a) {
322 return StrictMath.sqrt(a); // default impl. delegates to StrictMath
323 // Note that hardware sqrt instructions
324 // frequently can be directly used by JITs
325 // and should be much faster than doing
326 // Math.sqrt in software.
331 * Returns the cube root of a {@code double} value. For
332 * positive finite {@code x}, {@code cbrt(-x) ==
333 * -cbrt(x)}; that is, the cube root of a negative value is
334 * the negative of the cube root of that value's magnitude.
340 * <li>If the argument is NaN, then the result is NaN.
342 * <li>If the argument is infinite, then the result is an infinity
343 * with the same sign as the argument.
345 * <li>If the argument is zero, then the result is a zero with the
346 * same sign as the argument.
350 * <p>The computed result must be within 1 ulp of the exact result.
353 * @return the cube root of {@code a}.
356 public static double cbrt(double a) {
357 return StrictMath.cbrt(a);
361 * Computes the remainder operation on two arguments as prescribed
362 * by the IEEE 754 standard.
363 * The remainder value is mathematically equal to
364 * <code>f1 - f2</code> × <i>n</i>,
365 * where <i>n</i> is the mathematical integer closest to the exact
366 * mathematical value of the quotient {@code f1/f2}, and if two
367 * mathematical integers are equally close to {@code f1/f2},
368 * then <i>n</i> is the integer that is even. If the remainder is
369 * zero, its sign is the same as the sign of the first argument.
371 * <ul><li>If either argument is NaN, or the first argument is infinite,
372 * or the second argument is positive zero or negative zero, then the
374 * <li>If the first argument is finite and the second argument is
375 * infinite, then the result is the same as the first argument.</ul>
377 * @param f1 the dividend.
378 * @param f2 the divisor.
379 * @return the remainder when {@code f1} is divided by
382 public static double IEEEremainder(double f1, double f2) {
383 return StrictMath.IEEEremainder(f1, f2); // delegate to StrictMath
387 * Returns the smallest (closest to negative infinity)
388 * {@code double} value that is greater than or equal to the
389 * argument and is equal to a mathematical integer. Special cases:
390 * <ul><li>If the argument value is already equal to a
391 * mathematical integer, then the result is the same as the
392 * argument. <li>If the argument is NaN or an infinity or
393 * positive zero or negative zero, then the result is the same as
394 * the argument. <li>If the argument value is less than zero but
395 * greater than -1.0, then the result is negative zero.</ul> Note
396 * that the value of {@code Math.ceil(x)} is exactly the
397 * value of {@code -Math.floor(-x)}.
401 * @return the smallest (closest to negative infinity)
402 * floating-point value that is greater than or equal to
403 * the argument and is equal to a mathematical integer.
405 public static double ceil(double a) {
406 return StrictMath.ceil(a); // default impl. delegates to StrictMath
410 * Returns the largest (closest to positive infinity)
411 * {@code double} value that is less than or equal to the
412 * argument and is equal to a mathematical integer. Special cases:
413 * <ul><li>If the argument value is already equal to a
414 * mathematical integer, then the result is the same as the
415 * argument. <li>If the argument is NaN or an infinity or
416 * positive zero or negative zero, then the result is the same as
420 * @return the largest (closest to positive infinity)
421 * floating-point value that less than or equal to the argument
422 * and is equal to a mathematical integer.
424 public static double floor(double a) {
425 return StrictMath.floor(a); // default impl. delegates to StrictMath
429 * Returns the {@code double} value that is closest in value
430 * to the argument and is equal to a mathematical integer. If two
431 * {@code double} values that are mathematical integers are
432 * equally close, the result is the integer value that is
433 * even. Special cases:
434 * <ul><li>If the argument value is already equal to a mathematical
435 * integer, then the result is the same as the argument.
436 * <li>If the argument is NaN or an infinity or positive zero or negative
437 * zero, then the result is the same as the argument.</ul>
439 * @param a a {@code double} value.
440 * @return the closest floating-point value to {@code a} that is
441 * equal to a mathematical integer.
443 public static double rint(double a) {
444 return StrictMath.rint(a); // default impl. delegates to StrictMath
448 * Returns the angle <i>theta</i> from the conversion of rectangular
449 * coordinates ({@code x}, {@code y}) to polar
450 * coordinates (r, <i>theta</i>).
451 * This method computes the phase <i>theta</i> by computing an arc tangent
452 * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special
454 * <ul><li>If either argument is NaN, then the result is NaN.
455 * <li>If the first argument is positive zero and the second argument
456 * is positive, or the first argument is positive and finite and the
457 * second argument is positive infinity, then the result is positive
459 * <li>If the first argument is negative zero and the second argument
460 * is positive, or the first argument is negative and finite and the
461 * second argument is positive infinity, then the result is negative zero.
462 * <li>If the first argument is positive zero and the second argument
463 * is negative, or the first argument is positive and finite and the
464 * second argument is negative infinity, then the result is the
465 * {@code double} value closest to <i>pi</i>.
466 * <li>If the first argument is negative zero and the second argument
467 * is negative, or the first argument is negative and finite and the
468 * second argument is negative infinity, then the result is the
469 * {@code double} value closest to -<i>pi</i>.
470 * <li>If the first argument is positive and the second argument is
471 * positive zero or negative zero, or the first argument is positive
472 * infinity and the second argument is finite, then the result is the
473 * {@code double} value closest to <i>pi</i>/2.
474 * <li>If the first argument is negative and the second argument is
475 * positive zero or negative zero, or the first argument is negative
476 * infinity and the second argument is finite, then the result is the
477 * {@code double} value closest to -<i>pi</i>/2.
478 * <li>If both arguments are positive infinity, then the result is the
479 * {@code double} value closest to <i>pi</i>/4.
480 * <li>If the first argument is positive infinity and the second argument
481 * is negative infinity, then the result is the {@code double}
482 * value closest to 3*<i>pi</i>/4.
483 * <li>If the first argument is negative infinity and the second argument
484 * is positive infinity, then the result is the {@code double} value
485 * closest to -<i>pi</i>/4.
486 * <li>If both arguments are negative infinity, then the result is the
487 * {@code double} value closest to -3*<i>pi</i>/4.</ul>
489 * <p>The computed result must be within 2 ulps of the exact result.
490 * Results must be semi-monotonic.
492 * @param y the ordinate coordinate
493 * @param x the abscissa coordinate
494 * @return the <i>theta</i> component of the point
495 * (<i>r</i>, <i>theta</i>)
496 * in polar coordinates that corresponds to the point
497 * (<i>x</i>, <i>y</i>) in Cartesian coordinates.
499 public static double atan2(double y, double x) {
500 return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
504 * Returns the value of the first argument raised to the power of the
505 * second argument. Special cases:
507 * <ul><li>If the second argument is positive or negative zero, then the
509 * <li>If the second argument is 1.0, then the result is the same as the
511 * <li>If the second argument is NaN, then the result is NaN.
512 * <li>If the first argument is NaN and the second argument is nonzero,
513 * then the result is NaN.
517 * <li>the absolute value of the first argument is greater than 1
518 * and the second argument is positive infinity, or
519 * <li>the absolute value of the first argument is less than 1 and
520 * the second argument is negative infinity,
522 * then the result is positive infinity.
526 * <li>the absolute value of the first argument is greater than 1 and
527 * the second argument is negative infinity, or
528 * <li>the absolute value of the
529 * first argument is less than 1 and the second argument is positive
532 * then the result is positive zero.
534 * <li>If the absolute value of the first argument equals 1 and the
535 * second argument is infinite, then the result is NaN.
539 * <li>the first argument is positive zero and the second argument
540 * is greater than zero, or
541 * <li>the first argument is positive infinity and the second
542 * argument is less than zero,
544 * then the result is positive zero.
548 * <li>the first argument is positive zero and the second argument
549 * is less than zero, or
550 * <li>the first argument is positive infinity and the second
551 * argument is greater than zero,
553 * then the result is positive infinity.
557 * <li>the first argument is negative zero and the second argument
558 * is greater than zero but not a finite odd integer, or
559 * <li>the first argument is negative infinity and the second
560 * argument is less than zero but not a finite odd integer,
562 * then the result is positive zero.
566 * <li>the first argument is negative zero and the second argument
567 * is a positive finite odd integer, or
568 * <li>the first argument is negative infinity and the second
569 * argument is a negative finite odd integer,
571 * then the result is negative zero.
575 * <li>the first argument is negative zero and the second argument
576 * is less than zero but not a finite odd integer, or
577 * <li>the first argument is negative infinity and the second
578 * argument is greater than zero but not a finite odd integer,
580 * then the result is positive infinity.
584 * <li>the first argument is negative zero and the second argument
585 * is a negative finite odd integer, or
586 * <li>the first argument is negative infinity and the second
587 * argument is a positive finite odd integer,
589 * then the result is negative infinity.
591 * <li>If the first argument is finite and less than zero
593 * <li> if the second argument is a finite even integer, the
594 * result is equal to the result of raising the absolute value of
595 * the first argument to the power of the second argument
597 * <li>if the second argument is a finite odd integer, the result
598 * is equal to the negative of the result of raising the absolute
599 * value of the first argument to the power of the second
602 * <li>if the second argument is finite and not an integer, then
606 * <li>If both arguments are integers, then the result is exactly equal
607 * to the mathematical result of raising the first argument to the power
608 * of the second argument if that result can in fact be represented
609 * exactly as a {@code double} value.</ul>
611 * <p>(In the foregoing descriptions, a floating-point value is
612 * considered to be an integer if and only if it is finite and a
613 * fixed point of the method {@link #ceil ceil} or,
614 * equivalently, a fixed point of the method {@link #floor
615 * floor}. A value is a fixed point of a one-argument
616 * method if and only if the result of applying the method to the
617 * value is equal to the value.)
619 * <p>The computed result must be within 1 ulp of the exact result.
620 * Results must be semi-monotonic.
623 * @param b the exponent.
624 * @return the value {@code a}<sup>{@code b}</sup>.
626 public static double pow(double a, double b) {
627 return StrictMath.pow(a, b); // default impl. delegates to StrictMath
631 * Returns the closest {@code int} to the argument, with ties
636 * <ul><li>If the argument is NaN, the result is 0.
637 * <li>If the argument is negative infinity or any value less than or
638 * equal to the value of {@code Integer.MIN_VALUE}, the result is
639 * equal to the value of {@code Integer.MIN_VALUE}.
640 * <li>If the argument is positive infinity or any value greater than or
641 * equal to the value of {@code Integer.MAX_VALUE}, the result is
642 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
644 * @param a a floating-point value to be rounded to an integer.
645 * @return the value of the argument rounded to the nearest
647 * @see java.lang.Integer#MAX_VALUE
648 * @see java.lang.Integer#MIN_VALUE
650 public static int round(float a) {
651 if (a != 0x1.fffffep-2f) // greatest float value less than 0.5
652 return (int)floor(a + 0.5f);
658 * Returns the closest {@code long} to the argument, with ties
662 * <ul><li>If the argument is NaN, the result is 0.
663 * <li>If the argument is negative infinity or any value less than or
664 * equal to the value of {@code Long.MIN_VALUE}, the result is
665 * equal to the value of {@code Long.MIN_VALUE}.
666 * <li>If the argument is positive infinity or any value greater than or
667 * equal to the value of {@code Long.MAX_VALUE}, the result is
668 * equal to the value of {@code Long.MAX_VALUE}.</ul>
670 * @param a a floating-point value to be rounded to a
672 * @return the value of the argument rounded to the nearest
673 * {@code long} value.
674 * @see java.lang.Long#MAX_VALUE
675 * @see java.lang.Long#MIN_VALUE
677 public static long round(double a) {
678 if (a != 0x1.fffffffffffffp-2) // greatest double value less than 0.5
679 return (long)floor(a + 0.5d);
684 // private static Random randomNumberGenerator;
686 // private static synchronized Random initRNG() {
687 // Random rnd = randomNumberGenerator;
688 // return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
692 * Returns a {@code double} value with a positive sign, greater
693 * than or equal to {@code 0.0} and less than {@code 1.0}.
694 * Returned values are chosen pseudorandomly with (approximately)
695 * uniform distribution from that range.
697 * <p>When this method is first called, it creates a single new
698 * pseudorandom-number generator, exactly as if by the expression
700 * <blockquote>{@code new java.util.Random()}</blockquote>
702 * This new pseudorandom-number generator is used thereafter for
703 * all calls to this method and is used nowhere else.
705 * <p>This method is properly synchronized to allow correct use by
706 * more than one thread. However, if many threads need to generate
707 * pseudorandom numbers at a great rate, it may reduce contention
708 * for each thread to have its own pseudorandom-number generator.
710 * @return a pseudorandom {@code double} greater than or equal
711 * to {@code 0.0} and less than {@code 1.0}.
712 * @see Random#nextDouble()
714 public static double random() {
715 throw new UnsupportedOperationException();
719 * Returns the absolute value of an {@code int} value.
720 * If the argument is not negative, the argument is returned.
721 * If the argument is negative, the negation of the argument is returned.
723 * <p>Note that if the argument is equal to the value of
724 * {@link Integer#MIN_VALUE}, the most negative representable
725 * {@code int} value, the result is that same value, which is
728 * @param a the argument whose absolute value is to be determined
729 * @return the absolute value of the argument.
731 public static int abs(int a) {
732 return (a < 0) ? -a : a;
736 * Returns the absolute value of a {@code long} value.
737 * If the argument is not negative, the argument is returned.
738 * If the argument is negative, the negation of the argument is returned.
740 * <p>Note that if the argument is equal to the value of
741 * {@link Long#MIN_VALUE}, the most negative representable
742 * {@code long} value, the result is that same value, which
745 * @param a the argument whose absolute value is to be determined
746 * @return the absolute value of the argument.
748 public static long abs(long a) {
749 return (a < 0) ? -a : a;
753 * Returns the absolute value of a {@code float} value.
754 * If the argument is not negative, the argument is returned.
755 * If the argument is negative, the negation of the argument is returned.
757 * <ul><li>If the argument is positive zero or negative zero, the
758 * result is positive zero.
759 * <li>If the argument is infinite, the result is positive infinity.
760 * <li>If the argument is NaN, the result is NaN.</ul>
761 * In other words, the result is the same as the value of the expression:
762 * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))}
764 * @param a the argument whose absolute value is to be determined
765 * @return the absolute value of the argument.
767 public static float abs(float a) {
768 return (a <= 0.0F) ? 0.0F - a : a;
772 * Returns the absolute value of a {@code double} value.
773 * If the argument is not negative, the argument is returned.
774 * If the argument is negative, the negation of the argument is returned.
776 * <ul><li>If the argument is positive zero or negative zero, the result
778 * <li>If the argument is infinite, the result is positive infinity.
779 * <li>If the argument is NaN, the result is NaN.</ul>
780 * In other words, the result is the same as the value of the expression:
781 * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)}
783 * @param a the argument whose absolute value is to be determined
784 * @return the absolute value of the argument.
786 public static double abs(double a) {
787 return (a <= 0.0D) ? 0.0D - a : a;
791 * Returns the greater of two {@code int} values. That is, the
792 * result is the argument closer to the value of
793 * {@link Integer#MAX_VALUE}. If the arguments have the same value,
794 * the result is that same value.
796 * @param a an argument.
797 * @param b another argument.
798 * @return the larger of {@code a} and {@code b}.
800 public static int max(int a, int b) {
801 return (a >= b) ? a : b;
805 * Returns the greater of two {@code long} values. That is, the
806 * result is the argument closer to the value of
807 * {@link Long#MAX_VALUE}. If the arguments have the same value,
808 * the result is that same value.
810 * @param a an argument.
811 * @param b another argument.
812 * @return the larger of {@code a} and {@code b}.
814 public static long max(long a, long b) {
815 return (a >= b) ? a : b;
819 * Returns the greater of two {@code float} values. That is,
820 * the result is the argument closer to positive infinity. If the
821 * arguments have the same value, the result is that same
822 * value. If either value is NaN, then the result is NaN. Unlike
823 * the numerical comparison operators, this method considers
824 * negative zero to be strictly smaller than positive zero. If one
825 * argument is positive zero and the other negative zero, the
826 * result is positive zero.
828 * @param a an argument.
829 * @param b another argument.
830 * @return the larger of {@code a} and {@code b}.
832 @JavaScriptBody(args={"a", "b"},
833 body="return Math.max(a,b);"
835 public static float max(float a, float b) {
836 throw new UnsupportedOperationException();
840 * Returns the greater of two {@code double} values. That
841 * is, the result is the argument closer to positive infinity. If
842 * the arguments have the same value, the result is that same
843 * value. If either value is NaN, then the result is NaN. Unlike
844 * the numerical comparison operators, this method considers
845 * negative zero to be strictly smaller than positive zero. If one
846 * argument is positive zero and the other negative zero, the
847 * result is positive zero.
849 * @param a an argument.
850 * @param b another argument.
851 * @return the larger of {@code a} and {@code b}.
853 @JavaScriptBody(args={"a", "b"},
854 body="return Math.max(a,b);"
856 public static double max(double a, double b) {
857 throw new UnsupportedOperationException();
861 * Returns the smaller of two {@code int} values. That is,
862 * the result the argument closer to the value of
863 * {@link Integer#MIN_VALUE}. If the arguments have the same
864 * value, the result is that same value.
866 * @param a an argument.
867 * @param b another argument.
868 * @return the smaller of {@code a} and {@code b}.
870 public static int min(int a, int b) {
871 return (a <= b) ? a : b;
875 * Returns the smaller of two {@code long} values. That is,
876 * the result is the argument closer to the value of
877 * {@link Long#MIN_VALUE}. If the arguments have the same
878 * value, the result is that same value.
880 * @param a an argument.
881 * @param b another argument.
882 * @return the smaller of {@code a} and {@code b}.
884 public static long min(long a, long b) {
885 return (a <= b) ? a : b;
889 * Returns the smaller of two {@code float} values. That is,
890 * the result is the value closer to negative infinity. If the
891 * arguments have the same value, the result is that same
892 * value. If either value is NaN, then the result is NaN. Unlike
893 * the numerical comparison operators, this method considers
894 * negative zero to be strictly smaller than positive zero. If
895 * one argument is positive zero and the other is negative zero,
896 * the result is negative zero.
898 * @param a an argument.
899 * @param b another argument.
900 * @return the smaller of {@code a} and {@code b}.
902 @JavaScriptBody(args={"a", "b"},
903 body="return Math.min(a,b);"
905 public static float min(float a, float b) {
906 throw new UnsupportedOperationException();
910 * Returns the smaller of two {@code double} values. That
911 * is, the result is the value closer to negative infinity. If the
912 * arguments have the same value, the result is that same
913 * value. If either value is NaN, then the result is NaN. Unlike
914 * the numerical comparison operators, this method considers
915 * negative zero to be strictly smaller than positive zero. If one
916 * argument is positive zero and the other is negative zero, the
917 * result is negative zero.
919 * @param a an argument.
920 * @param b another argument.
921 * @return the smaller of {@code a} and {@code b}.
923 @JavaScriptBody(args={"a", "b"},
924 body="return Math.min(a,b);"
926 public static double min(double a, double b) {
927 throw new UnsupportedOperationException();
931 * Returns the size of an ulp of the argument. An ulp of a
932 * {@code double} value is the positive distance between this
933 * floating-point value and the {@code double} value next
934 * larger in magnitude. Note that for non-NaN <i>x</i>,
935 * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
939 * <li> If the argument is NaN, then the result is NaN.
940 * <li> If the argument is positive or negative infinity, then the
941 * result is positive infinity.
942 * <li> If the argument is positive or negative zero, then the result is
943 * {@code Double.MIN_VALUE}.
944 * <li> If the argument is ±{@code Double.MAX_VALUE}, then
945 * the result is equal to 2<sup>971</sup>.
948 * @param d the floating-point value whose ulp is to be returned
949 * @return the size of an ulp of the argument
950 * @author Joseph D. Darcy
953 // public static double ulp(double d) {
954 // return sun.misc.FpUtils.ulp(d);
958 * Returns the size of an ulp of the argument. An ulp of a
959 * {@code float} value is the positive distance between this
960 * floating-point value and the {@code float} value next
961 * larger in magnitude. Note that for non-NaN <i>x</i>,
962 * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
966 * <li> If the argument is NaN, then the result is NaN.
967 * <li> If the argument is positive or negative infinity, then the
968 * result is positive infinity.
969 * <li> If the argument is positive or negative zero, then the result is
970 * {@code Float.MIN_VALUE}.
971 * <li> If the argument is ±{@code Float.MAX_VALUE}, then
972 * the result is equal to 2<sup>104</sup>.
975 * @param f the floating-point value whose ulp is to be returned
976 * @return the size of an ulp of the argument
977 * @author Joseph D. Darcy
980 // public static float ulp(float f) {
981 // return sun.misc.FpUtils.ulp(f);
985 * Returns the signum function of the argument; zero if the argument
986 * is zero, 1.0 if the argument is greater than zero, -1.0 if the
987 * argument is less than zero.
991 * <li> If the argument is NaN, then the result is NaN.
992 * <li> If the argument is positive zero or negative zero, then the
993 * result is the same as the argument.
996 * @param d the floating-point value whose signum is to be returned
997 * @return the signum function of the argument
998 * @author Joseph D. Darcy
1001 // public static double signum(double d) {
1002 // return sun.misc.FpUtils.signum(d);
1006 * Returns the signum function of the argument; zero if the argument
1007 * is zero, 1.0f if the argument is greater than zero, -1.0f if the
1008 * argument is less than zero.
1012 * <li> If the argument is NaN, then the result is NaN.
1013 * <li> If the argument is positive zero or negative zero, then the
1014 * result is the same as the argument.
1017 * @param f the floating-point value whose signum is to be returned
1018 * @return the signum function of the argument
1019 * @author Joseph D. Darcy
1022 // public static float signum(float f) {
1023 // return sun.misc.FpUtils.signum(f);
1027 * Returns the hyperbolic sine of a {@code double} value.
1028 * The hyperbolic sine of <i>x</i> is defined to be
1029 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2
1030 * where <i>e</i> is {@linkplain Math#E Euler's number}.
1035 * <li>If the argument is NaN, then the result is NaN.
1037 * <li>If the argument is infinite, then the result is an infinity
1038 * with the same sign as the argument.
1040 * <li>If the argument is zero, then the result is a zero with the
1041 * same sign as the argument.
1045 * <p>The computed result must be within 2.5 ulps of the exact result.
1047 * @param x The number whose hyperbolic sine is to be returned.
1048 * @return The hyperbolic sine of {@code x}.
1051 public static double sinh(double x) {
1052 return StrictMath.sinh(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 * <p>The computed result must be within 2.5 ulps of the exact result.
1075 * @param x The number whose hyperbolic cosine is to be returned.
1076 * @return The hyperbolic cosine of {@code x}.
1079 public static double cosh(double x) {
1080 return StrictMath.cosh(x);
1084 * Returns the hyperbolic tangent of a {@code double} value.
1085 * The hyperbolic tangent of <i>x</i> is defined to be
1086 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>),
1087 * in other words, {@linkplain Math#sinh
1088 * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note
1089 * that the absolute value of the exact tanh is always less than
1095 * <li>If the argument is NaN, then the result is NaN.
1097 * <li>If the argument is zero, then the result is a zero with the
1098 * same sign as the argument.
1100 * <li>If the argument is positive infinity, then the result is
1103 * <li>If the argument is negative infinity, then the result is
1108 * <p>The computed result must be within 2.5 ulps of the exact result.
1109 * The result of {@code tanh} for any finite input must have
1110 * an absolute value less than or equal to 1. Note that once the
1111 * exact result of tanh is within 1/2 of an ulp of the limit value
1112 * of ±1, correctly signed ±{@code 1.0} should
1115 * @param x The number whose hyperbolic tangent is to be returned.
1116 * @return The hyperbolic tangent of {@code x}.
1119 public static double tanh(double x) {
1120 return StrictMath.tanh(x);
1124 * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
1125 * without intermediate overflow or underflow.
1130 * <li> If either argument is infinite, then the result
1131 * is positive infinity.
1133 * <li> If either argument is NaN and neither argument is infinite,
1134 * then the result is NaN.
1138 * <p>The computed result must be within 1 ulp of the exact
1139 * result. If one parameter is held constant, the results must be
1140 * semi-monotonic in the other parameter.
1144 * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
1145 * without intermediate overflow or underflow
1148 public static double hypot(double x, double y) {
1149 return StrictMath.hypot(x, y);
1153 * Returns <i>e</i><sup>x</sup> -1. Note that for values of
1154 * <i>x</i> near 0, the exact sum of
1155 * {@code expm1(x)} + 1 is much closer to the true
1156 * result of <i>e</i><sup>x</sup> than {@code exp(x)}.
1160 * <li>If the argument is NaN, the result is NaN.
1162 * <li>If the argument is positive infinity, then the result is
1163 * positive infinity.
1165 * <li>If the argument is negative infinity, then the result is
1168 * <li>If the argument is zero, then the result is a zero with the
1169 * same sign as the argument.
1173 * <p>The computed result must be within 1 ulp of the exact result.
1174 * Results must be semi-monotonic. The result of
1175 * {@code expm1} for any finite input must be greater than or
1176 * equal to {@code -1.0}. Note that once the exact result of
1177 * <i>e</i><sup>{@code x}</sup> - 1 is within 1/2
1178 * ulp of the limit value -1, {@code -1.0} should be
1181 * @param x the exponent to raise <i>e</i> to in the computation of
1182 * <i>e</i><sup>{@code x}</sup> -1.
1183 * @return the value <i>e</i><sup>{@code x}</sup> - 1.
1186 public static double expm1(double x) {
1187 return StrictMath.expm1(x);
1191 * Returns the natural logarithm of the sum of the argument and 1.
1192 * Note that for small values {@code x}, the result of
1193 * {@code log1p(x)} is much closer to the true result of ln(1
1194 * + {@code x}) than the floating-point evaluation of
1195 * {@code log(1.0+x)}.
1201 * <li>If the argument is NaN or less than -1, then the result is
1204 * <li>If the argument is positive infinity, then the result is
1205 * positive infinity.
1207 * <li>If the argument is negative one, then the result is
1208 * negative infinity.
1210 * <li>If the argument is zero, then the result is a zero with the
1211 * same sign as the argument.
1215 * <p>The computed result must be within 1 ulp of the exact result.
1216 * Results must be semi-monotonic.
1219 * @return the value ln({@code x} + 1), the natural
1220 * log of {@code x} + 1
1223 public static double log1p(double x) {
1224 return StrictMath.log1p(x);
1228 * Returns the first floating-point argument with the sign of the
1229 * second floating-point argument. Note that unlike the {@link
1230 * StrictMath#copySign(double, double) StrictMath.copySign}
1231 * method, this method does not require NaN {@code sign}
1232 * arguments to be treated as positive values; implementations are
1233 * permitted to treat some NaN arguments as positive and other NaN
1234 * arguments as negative to allow greater performance.
1236 * @param magnitude the parameter providing the magnitude of the result
1237 * @param sign the parameter providing the sign of the result
1238 * @return a value with the magnitude of {@code magnitude}
1239 * and the sign of {@code sign}.
1242 // public static double copySign(double magnitude, double sign) {
1243 // return sun.misc.FpUtils.rawCopySign(magnitude, sign);
1247 * Returns the first floating-point argument with the sign of the
1248 * second floating-point argument. Note that unlike the {@link
1249 * StrictMath#copySign(float, float) StrictMath.copySign}
1250 * method, this method does not require NaN {@code sign}
1251 * arguments to be treated as positive values; implementations are
1252 * permitted to treat some NaN arguments as positive and other NaN
1253 * arguments as negative to allow greater performance.
1255 * @param magnitude the parameter providing the magnitude of the result
1256 * @param sign the parameter providing the sign of the result
1257 * @return a value with the magnitude of {@code magnitude}
1258 * and the sign of {@code sign}.
1261 // public static float copySign(float magnitude, float sign) {
1262 // return sun.misc.FpUtils.rawCopySign(magnitude, sign);
1266 * Returns the unbiased exponent used in the representation of a
1267 * {@code float}. Special cases:
1270 * <li>If the argument is NaN or infinite, then the result is
1271 * {@link Float#MAX_EXPONENT} + 1.
1272 * <li>If the argument is zero or subnormal, then the result is
1273 * {@link Float#MIN_EXPONENT} -1.
1275 * @param f a {@code float} value
1276 * @return the unbiased exponent of the argument
1279 // public static int getExponent(float f) {
1280 // return sun.misc.FpUtils.getExponent(f);
1284 * Returns the unbiased exponent used in the representation of a
1285 * {@code double}. Special cases:
1288 * <li>If the argument is NaN or infinite, then the result is
1289 * {@link Double#MAX_EXPONENT} + 1.
1290 * <li>If the argument is zero or subnormal, then the result is
1291 * {@link Double#MIN_EXPONENT} -1.
1293 * @param d a {@code double} value
1294 * @return the unbiased exponent of the argument
1297 // public static int getExponent(double d) {
1298 // return sun.misc.FpUtils.getExponent(d);
1302 * Returns the floating-point number adjacent to the first
1303 * argument in the direction of the second argument. If both
1304 * arguments compare as equal the second argument is returned.
1309 * <li> If either argument is a NaN, then NaN is returned.
1311 * <li> If both arguments are signed zeros, {@code direction}
1312 * is returned unchanged (as implied by the requirement of
1313 * returning the second argument if the arguments compare as
1316 * <li> If {@code start} is
1317 * ±{@link Double#MIN_VALUE} and {@code direction}
1318 * has a value such that the result should have a smaller
1319 * magnitude, then a zero with the same sign as {@code start}
1322 * <li> If {@code start} is infinite and
1323 * {@code direction} has a value such that the result should
1324 * have a smaller magnitude, {@link Double#MAX_VALUE} with the
1325 * same sign as {@code start} is returned.
1327 * <li> If {@code start} is equal to ±
1328 * {@link Double#MAX_VALUE} and {@code direction} has a
1329 * value such that the result should have a larger magnitude, an
1330 * infinity with same sign as {@code start} is returned.
1333 * @param start starting floating-point value
1334 * @param direction value indicating which of
1335 * {@code start}'s neighbors or {@code start} should
1337 * @return The floating-point number adjacent to {@code start} in the
1338 * direction of {@code direction}.
1341 // public static double nextAfter(double start, double direction) {
1342 // return sun.misc.FpUtils.nextAfter(start, direction);
1346 * Returns the floating-point number adjacent to the first
1347 * argument in the direction of the second argument. If both
1348 * arguments compare as equal a value equivalent to the second argument
1354 * <li> If either argument is a NaN, then NaN is returned.
1356 * <li> If both arguments are signed zeros, a value equivalent
1357 * to {@code direction} is returned.
1359 * <li> If {@code start} is
1360 * ±{@link Float#MIN_VALUE} and {@code direction}
1361 * has a value such that the result should have a smaller
1362 * magnitude, then a zero with the same sign as {@code start}
1365 * <li> If {@code start} is infinite and
1366 * {@code direction} has a value such that the result should
1367 * have a smaller magnitude, {@link Float#MAX_VALUE} with the
1368 * same sign as {@code start} is returned.
1370 * <li> If {@code start} is equal to ±
1371 * {@link Float#MAX_VALUE} and {@code direction} has a
1372 * value such that the result should have a larger magnitude, an
1373 * infinity with same sign as {@code start} is returned.
1376 * @param start starting floating-point value
1377 * @param direction value indicating which of
1378 * {@code start}'s neighbors or {@code start} should
1380 * @return The floating-point number adjacent to {@code start} in the
1381 * direction of {@code direction}.
1384 // public static float nextAfter(float start, double direction) {
1385 // return sun.misc.FpUtils.nextAfter(start, direction);
1389 * Returns the floating-point value adjacent to {@code d} in
1390 * the direction of positive infinity. This method is
1391 * semantically equivalent to {@code nextAfter(d,
1392 * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
1393 * implementation may run faster than its equivalent
1394 * {@code nextAfter} call.
1398 * <li> If the argument is NaN, the result is NaN.
1400 * <li> If the argument is positive infinity, the result is
1401 * positive infinity.
1403 * <li> If the argument is zero, the result is
1404 * {@link Double#MIN_VALUE}
1408 * @param d starting floating-point value
1409 * @return The adjacent floating-point value closer to positive
1413 // public static double nextUp(double d) {
1414 // return sun.misc.FpUtils.nextUp(d);
1418 * Returns the floating-point value adjacent to {@code f} in
1419 * the direction of positive infinity. This method is
1420 * semantically equivalent to {@code nextAfter(f,
1421 * Float.POSITIVE_INFINITY)}; however, a {@code nextUp}
1422 * implementation may run faster than its equivalent
1423 * {@code nextAfter} call.
1427 * <li> If the argument is NaN, the result is NaN.
1429 * <li> If the argument is positive infinity, the result is
1430 * positive infinity.
1432 * <li> If the argument is zero, the result is
1433 * {@link Float#MIN_VALUE}
1437 * @param f starting floating-point value
1438 * @return The adjacent floating-point value closer to positive
1442 // public static float nextUp(float f) {
1443 // return sun.misc.FpUtils.nextUp(f);
1448 * Return {@code d} ×
1449 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1450 * by a single correctly rounded floating-point multiply to a
1451 * member of the double value set. See the Java
1452 * Language Specification for a discussion of floating-point
1453 * value sets. If the exponent of the result is between {@link
1454 * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
1455 * answer is calculated exactly. If the exponent of the result
1456 * would be larger than {@code Double.MAX_EXPONENT}, an
1457 * infinity is returned. Note that if the result is subnormal,
1458 * precision may be lost; that is, when {@code scalb(x, n)}
1459 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1460 * <i>x</i>. When the result is non-NaN, the result has the same
1461 * sign as {@code d}.
1465 * <li> If the first argument is NaN, NaN is returned.
1466 * <li> If the first argument is infinite, then an infinity of the
1467 * same sign is returned.
1468 * <li> If the first argument is zero, then a zero of the same
1472 * @param d number to be scaled by a power of two.
1473 * @param scaleFactor power of 2 used to scale {@code d}
1474 * @return {@code d} × 2<sup>{@code scaleFactor}</sup>
1477 // public static double scalb(double d, int scaleFactor) {
1478 // return sun.misc.FpUtils.scalb(d, scaleFactor);
1482 * Return {@code f} ×
1483 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1484 * by a single correctly rounded floating-point multiply to a
1485 * member of the float value set. See the Java
1486 * Language Specification for a discussion of floating-point
1487 * value sets. If the exponent of the result is between {@link
1488 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
1489 * answer is calculated exactly. If the exponent of the result
1490 * would be larger than {@code Float.MAX_EXPONENT}, an
1491 * infinity is returned. Note that if the result is subnormal,
1492 * precision may be lost; that is, when {@code scalb(x, n)}
1493 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1494 * <i>x</i>. When the result is non-NaN, the result has the same
1495 * sign as {@code f}.
1499 * <li> If the first argument is NaN, NaN is returned.
1500 * <li> If the first argument is infinite, then an infinity of the
1501 * same sign is returned.
1502 * <li> If the first argument is zero, then a zero of the same
1506 * @param f number to be scaled by a power of two.
1507 * @param scaleFactor power of 2 used to scale {@code f}
1508 * @return {@code f} × 2<sup>{@code scaleFactor}</sup>
1511 // public static float scalb(float f, int scaleFactor) {
1512 // return sun.misc.FpUtils.scalb(f, scaleFactor);