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.ExtraJavaScript;
29 import org.apidesign.bck2brwsr.core.JavaScriptBody;
33 * The class {@code Math} contains methods for performing basic
34 * numeric operations such as the elementary exponential, logarithm,
35 * square root, and trigonometric functions.
37 * <p>Unlike some of the numeric methods of class
38 * {@code StrictMath}, all implementations of the equivalent
39 * functions of class {@code Math} are not defined to return the
40 * bit-for-bit same results. This relaxation permits
41 * better-performing implementations where strict reproducibility is
44 * <p>By default many of the {@code Math} methods simply call
45 * the equivalent method in {@code StrictMath} for their
46 * implementation. Code generators are encouraged to use
47 * platform-specific native libraries or microprocessor instructions,
48 * where available, to provide higher-performance implementations of
49 * {@code Math} methods. Such higher-performance
50 * implementations still must conform to the specification for
53 * <p>The quality of implementation specifications concern two
54 * properties, accuracy of the returned result and monotonicity of the
55 * method. Accuracy of the floating-point {@code Math} methods
56 * is measured in terms of <i>ulps</i>, units in the last place. For
57 * a given floating-point format, an ulp of a specific real number
58 * value is the distance between the two floating-point values
59 * bracketing that numerical value. When discussing the accuracy of a
60 * method as a whole rather than at a specific argument, the number of
61 * ulps cited is for the worst-case error at any argument. If a
62 * method always has an error less than 0.5 ulps, the method always
63 * returns the floating-point number nearest the exact result; such a
64 * method is <i>correctly rounded</i>. A correctly rounded method is
65 * generally the best a floating-point approximation can be; however,
66 * it is impractical for many floating-point methods to be correctly
67 * rounded. Instead, for the {@code Math} class, a larger error
68 * bound of 1 or 2 ulps is allowed for certain methods. Informally,
69 * with a 1 ulp error bound, when the exact result is a representable
70 * number, the exact result should be returned as the computed result;
71 * otherwise, either of the two floating-point values which bracket
72 * the exact result may be returned. For exact results large in
73 * magnitude, one of the endpoints of the bracket may be infinite.
74 * Besides accuracy at individual arguments, maintaining proper
75 * relations between the method at different arguments is also
76 * important. Therefore, most methods with more than 0.5 ulp errors
77 * are required to be <i>semi-monotonic</i>: whenever the mathematical
78 * function is non-decreasing, so is the floating-point approximation,
79 * likewise, whenever the mathematical function is non-increasing, so
80 * is the floating-point approximation. Not all approximations that
81 * have 1 ulp accuracy will automatically meet the monotonicity
85 * @author Joseph D. Darcy
90 resource="/org/apidesign/vm4brwsr/emul/lang/java_lang_Math.js",
93 public final class Math {
96 * Don't let anyone instantiate this class.
101 * The {@code double} value that is closer than any other to
102 * <i>e</i>, the base of the natural logarithms.
104 public static final double E = 2.7182818284590452354;
107 * The {@code double} value that is closer than any other to
108 * <i>pi</i>, the ratio of the circumference of a circle to its
111 public static final double PI = 3.14159265358979323846;
114 * Returns the trigonometric sine of an angle. Special cases:
115 * <ul><li>If the argument is NaN or an infinity, then the
117 * <li>If the argument is zero, then the result is a zero with the
118 * same sign as the argument.</ul>
120 * <p>The computed result must be within 1 ulp of the exact result.
121 * Results must be semi-monotonic.
123 * @param a an angle, in radians.
124 * @return the sine of the argument.
126 @JavaScriptBody(args="a", body="return Math.sin(a);")
127 public static double sin(double a) {
128 throw new UnsupportedOperationException();
132 * Returns the trigonometric cosine of an angle. Special cases:
133 * <ul><li>If the argument is NaN or an infinity, then the
134 * result is NaN.</ul>
136 * <p>The computed result must be within 1 ulp of the exact result.
137 * Results must be semi-monotonic.
139 * @param a an angle, in radians.
140 * @return the cosine of the argument.
142 @JavaScriptBody(args="a", body="return Math.cos(a);")
143 public static double cos(double a) {
144 throw new UnsupportedOperationException();
148 * Returns the trigonometric tangent of an angle. Special cases:
149 * <ul><li>If the argument is NaN or an infinity, then the result
151 * <li>If the argument is zero, then the result is a zero with the
152 * same sign as the argument.</ul>
154 * <p>The computed result must be within 1 ulp of the exact result.
155 * Results must be semi-monotonic.
157 * @param a an angle, in radians.
158 * @return the tangent of the argument.
160 @JavaScriptBody(args="a", body="return Math.tan(a);")
161 public static double tan(double a) {
162 throw new UnsupportedOperationException();
166 * Returns the arc sine of a value; the returned angle is in the
167 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases:
168 * <ul><li>If the argument is NaN or its absolute value is greater
169 * than 1, then the result is NaN.
170 * <li>If the argument is zero, then the result is a zero with the
171 * same sign as the argument.</ul>
173 * <p>The computed result must be within 1 ulp of the exact result.
174 * Results must be semi-monotonic.
176 * @param a the value whose arc sine is to be returned.
177 * @return the arc sine of the argument.
179 @JavaScriptBody(args="a", body="return Math.asin(a);")
180 public static double asin(double a) {
181 throw new UnsupportedOperationException();
185 * Returns the arc cosine of a value; the returned angle is in the
186 * range 0.0 through <i>pi</i>. Special case:
187 * <ul><li>If the argument is NaN or its absolute value is greater
188 * than 1, then the result is NaN.</ul>
190 * <p>The computed result must be within 1 ulp of the exact result.
191 * Results must be semi-monotonic.
193 * @param a the value whose arc cosine is to be returned.
194 * @return the arc cosine of the argument.
196 @JavaScriptBody(args="a", body="return Math.acos(a);")
197 public static double acos(double a) {
198 throw new UnsupportedOperationException();
202 * Returns the arc tangent of a value; the returned angle is in the
203 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases:
204 * <ul><li>If the argument is NaN, then the result is NaN.
205 * <li>If the argument is zero, then the result is a zero with the
206 * same sign as the argument.</ul>
208 * <p>The computed result must be within 1 ulp of the exact result.
209 * Results must be semi-monotonic.
211 * @param a the value whose arc tangent is to be returned.
212 * @return the arc tangent of the argument.
214 @JavaScriptBody(args="a", body="return Math.atan(a);")
215 public static double atan(double a) {
216 throw new UnsupportedOperationException();
220 * Converts an angle measured in degrees to an approximately
221 * equivalent angle measured in radians. The conversion from
222 * degrees to radians is generally inexact.
224 * @param angdeg an angle, in degrees
225 * @return the measurement of the angle {@code angdeg}
229 public static double toRadians(double angdeg) {
230 return angdeg / 180.0 * PI;
234 * Converts an angle measured in radians to an approximately
235 * equivalent angle measured in degrees. The conversion from
236 * radians to degrees is generally inexact; users should
237 * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
240 * @param angrad an angle, in radians
241 * @return the measurement of the angle {@code angrad}
245 public static double toDegrees(double angrad) {
246 return angrad * 180.0 / PI;
250 * Returns Euler's number <i>e</i> raised to the power of a
251 * {@code double} value. Special cases:
252 * <ul><li>If the argument is NaN, the result is NaN.
253 * <li>If the argument is positive infinity, then the result is
255 * <li>If the argument is negative infinity, then the result is
256 * positive zero.</ul>
258 * <p>The computed result must be within 1 ulp of the exact result.
259 * Results must be semi-monotonic.
261 * @param a the exponent to raise <i>e</i> to.
262 * @return the value <i>e</i><sup>{@code a}</sup>,
263 * where <i>e</i> is the base of the natural logarithms.
265 @JavaScriptBody(args="a", body="return Math.exp(a);")
266 public static double exp(double a) {
267 throw new UnsupportedOperationException();
271 * Returns the natural logarithm (base <i>e</i>) of a {@code double}
272 * value. Special cases:
273 * <ul><li>If the argument is NaN or less than zero, then the result
275 * <li>If the argument is positive infinity, then the result is
277 * <li>If the argument is positive zero or negative zero, then the
278 * result is negative infinity.</ul>
280 * <p>The computed result must be within 1 ulp of the exact result.
281 * Results must be semi-monotonic.
284 * @return the value ln {@code a}, the natural logarithm of
287 @JavaScriptBody(args="a", body="return Math.log(a);")
288 public static double log(double a) {
289 throw new UnsupportedOperationException();
293 * Returns the base 10 logarithm of a {@code double} value.
296 * <ul><li>If the argument is NaN or less than zero, then the result
298 * <li>If the argument is positive infinity, then the result is
300 * <li>If the argument is positive zero or negative zero, then the
301 * result is negative infinity.
302 * <li> If the argument is equal to 10<sup><i>n</i></sup> for
303 * integer <i>n</i>, then the result is <i>n</i>.
306 * <p>The computed result must be within 1 ulp of the exact result.
307 * Results must be semi-monotonic.
310 * @return the base 10 logarithm of {@code a}.
313 @JavaScriptBody(args="a", body="return Math.log(a) / Math.LN10;")
314 public static double log10(double a) {
315 throw new UnsupportedOperationException();
319 * Returns the correctly rounded positive square root of a
320 * {@code double} value.
322 * <ul><li>If the argument is NaN or less than zero, then the result
324 * <li>If the argument is positive infinity, then the result is positive
326 * <li>If the argument is positive zero or negative zero, then the
327 * result is the same as the argument.</ul>
328 * Otherwise, the result is the {@code double} value closest to
329 * the true mathematical square root of the argument value.
332 * @return the positive square root of {@code a}.
333 * If the argument is NaN or less than zero, the result is NaN.
335 @JavaScriptBody(args="a", body="return Math.sqrt(a);")
336 public static double sqrt(double a) {
337 throw new UnsupportedOperationException();
341 * Returns the smallest (closest to negative infinity)
342 * {@code double} value that is greater than or equal to the
343 * argument and is equal to a mathematical integer. Special cases:
344 * <ul><li>If the argument value is already equal to a
345 * mathematical integer, then the result is the same as the
346 * argument. <li>If the argument is NaN or an infinity or
347 * positive zero or negative zero, then the result is the same as
348 * the argument. <li>If the argument value is less than zero but
349 * greater than -1.0, then the result is negative zero.</ul> Note
350 * that the value of {@code Math.ceil(x)} is exactly the
351 * value of {@code -Math.floor(-x)}.
355 * @return the smallest (closest to negative infinity)
356 * floating-point value that is greater than or equal to
357 * the argument and is equal to a mathematical integer.
359 @JavaScriptBody(args="a", body="return Math.ceil(a);")
360 public static double ceil(double a) {
361 throw new UnsupportedOperationException();
365 * Returns the largest (closest to positive infinity)
366 * {@code double} value that is less than or equal to the
367 * argument and is equal to a mathematical integer. Special cases:
368 * <ul><li>If the argument value is already equal to a
369 * mathematical integer, then the result is the same as the
370 * argument. <li>If the argument is NaN or an infinity or
371 * positive zero or negative zero, then the result is the same as
375 * @return the largest (closest to positive infinity)
376 * floating-point value that less than or equal to the argument
377 * and is equal to a mathematical integer.
379 @JavaScriptBody(args="a", body="return Math.floor(a);")
380 public static double floor(double a) {
381 throw new UnsupportedOperationException();
384 * Computes the remainder operation on two arguments as prescribed
385 * by the IEEE 754 standard.
386 * The remainder value is mathematically equal to
387 * <code>f1 - f2</code> × <i>n</i>,
388 * where <i>n</i> is the mathematical integer closest to the exact
389 * mathematical value of the quotient {@code f1/f2}, and if two
390 * mathematical integers are equally close to {@code f1/f2},
391 * then <i>n</i> is the integer that is even. If the remainder is
392 * zero, its sign is the same as the sign of the first argument.
394 * <ul><li>If either argument is NaN, or the first argument is infinite,
395 * or the second argument is positive zero or negative zero, then the
397 * <li>If the first argument is finite and the second argument is
398 * infinite, then the result is the same as the first argument.</ul>
400 * @param f1 the dividend.
401 * @param f2 the divisor.
402 * @return the remainder when {@code f1} is divided by
405 public static double IEEEremainder(double f1, double f2) {
406 return f1 - (f2 * Math.round(f1 / f2));
410 * Returns the {@code double} value that is closest in value
411 * to the argument and is equal to a mathematical integer. If two
412 * {@code double} values that are mathematical integers are
413 * equally close, the result is the integer value that is
414 * even. Special cases:
415 * <ul><li>If the argument value is already equal to a mathematical
416 * integer, then the result is the same as the argument.
417 * <li>If the argument is NaN or an infinity or positive zero or negative
418 * zero, then the result is the same as the argument.</ul>
420 * @param a a {@code double} value.
421 * @return the closest floating-point value to {@code a} that is
422 * equal to a mathematical integer.
424 public static double rint(double a) {
425 double ceil = ceil(a);
426 double floor = floor(a);
428 double dc = ceil - a;
429 double df = a - floor;
433 } else if (dc > df) {
437 int tenC = (int) (ceil % 10.0);
447 * Returns the angle <i>theta</i> from the conversion of rectangular
448 * coordinates ({@code x}, {@code y}) to polar
449 * coordinates (r, <i>theta</i>).
450 * This method computes the phase <i>theta</i> by computing an arc tangent
451 * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special
453 * <ul><li>If either argument is NaN, then the result is NaN.
454 * <li>If the first argument is positive zero and the second argument
455 * is positive, or the first argument is positive and finite and the
456 * second argument is positive infinity, then the result is positive
458 * <li>If the first argument is negative zero and the second argument
459 * is positive, or the first argument is negative and finite and the
460 * second argument is positive infinity, then the result is negative zero.
461 * <li>If the first argument is positive zero and the second argument
462 * is negative, or the first argument is positive and finite and the
463 * second argument is negative infinity, then the result is the
464 * {@code double} value closest to <i>pi</i>.
465 * <li>If the first argument is negative zero and the second argument
466 * is negative, or the first argument is negative and finite and the
467 * second argument is negative infinity, then the result is the
468 * {@code double} value closest to -<i>pi</i>.
469 * <li>If the first argument is positive and the second argument is
470 * positive zero or negative zero, or the first argument is positive
471 * infinity and the second argument is finite, then the result is the
472 * {@code double} value closest to <i>pi</i>/2.
473 * <li>If the first argument is negative and the second argument is
474 * positive zero or negative zero, or the first argument is negative
475 * infinity and the second argument is finite, then the result is the
476 * {@code double} value closest to -<i>pi</i>/2.
477 * <li>If both arguments are positive infinity, then the result is the
478 * {@code double} value closest to <i>pi</i>/4.
479 * <li>If the first argument is positive infinity and the second argument
480 * is negative infinity, then the result is the {@code double}
481 * value closest to 3*<i>pi</i>/4.
482 * <li>If the first argument is negative infinity and the second argument
483 * is positive infinity, then the result is the {@code double} value
484 * closest to -<i>pi</i>/4.
485 * <li>If both arguments are negative infinity, then the result is the
486 * {@code double} value closest to -3*<i>pi</i>/4.</ul>
488 * <p>The computed result must be within 2 ulps of the exact result.
489 * Results must be semi-monotonic.
491 * @param y the ordinate coordinate
492 * @param x the abscissa coordinate
493 * @return the <i>theta</i> component of the point
494 * (<i>r</i>, <i>theta</i>)
495 * in polar coordinates that corresponds to the point
496 * (<i>x</i>, <i>y</i>) in Cartesian coordinates.
498 @JavaScriptBody(args={"y", "x"}, body="return Math.atan2(y, x);")
499 public static double atan2(double y, double x) {
500 throw new UnsupportedOperationException();
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 @JavaScriptBody(args={"a", "b"}, body="return Math.pow(a, b);")
627 public static double pow(double a, double b) {
628 throw new UnsupportedOperationException();
632 * Returns the closest {@code int} to the argument, with ties
637 * <ul><li>If the argument is NaN, the result is 0.
638 * <li>If the argument is negative infinity or any value less than or
639 * equal to the value of {@code Integer.MIN_VALUE}, the result is
640 * equal to the value of {@code Integer.MIN_VALUE}.
641 * <li>If the argument is positive infinity or any value greater than or
642 * equal to the value of {@code Integer.MAX_VALUE}, the result is
643 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
645 * @param a a floating-point value to be rounded to an integer.
646 * @return the value of the argument rounded to the nearest
648 * @see java.lang.Integer#MAX_VALUE
649 * @see java.lang.Integer#MIN_VALUE
651 public static int round(float a) {
652 return (int)roundDbl(a);
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 return (long)roundDbl(a);
679 @JavaScriptBody(args="a", body="return Math.round(a);")
680 private static native double roundDbl(double d);
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 @JavaScriptBody(args={}, body="return Math.random();")
713 public static double random() {
714 throw new UnsupportedOperationException();
718 * Returns the absolute value of an {@code int} value.
719 * If the argument is not negative, the argument is returned.
720 * If the argument is negative, the negation of the argument is returned.
722 * <p>Note that if the argument is equal to the value of
723 * {@link Integer#MIN_VALUE}, the most negative representable
724 * {@code int} value, the result is that same value, which is
727 * @param a the argument whose absolute value is to be determined
728 * @return the absolute value of the argument.
730 public static int abs(int a) {
731 return (a < 0) ? -a : a;
735 * Returns the absolute value of a {@code long} value.
736 * If the argument is not negative, the argument is returned.
737 * If the argument is negative, the negation of the argument is returned.
739 * <p>Note that if the argument is equal to the value of
740 * {@link Long#MIN_VALUE}, the most negative representable
741 * {@code long} value, the result is that same value, which
744 * @param a the argument whose absolute value is to be determined
745 * @return the absolute value of the argument.
747 public static long abs(long a) {
748 return (a < 0) ? -a : a;
752 * Returns the absolute value of a {@code float} value.
753 * If the argument is not negative, the argument is returned.
754 * If the argument is negative, the negation of the argument is returned.
756 * <ul><li>If the argument is positive zero or negative zero, the
757 * result is positive zero.
758 * <li>If the argument is infinite, the result is positive infinity.
759 * <li>If the argument is NaN, the result is NaN.</ul>
760 * In other words, the result is the same as the value of the expression:
761 * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))}
763 * @param a the argument whose absolute value is to be determined
764 * @return the absolute value of the argument.
766 public static float abs(float a) {
767 return (a <= 0.0F) ? 0.0F - a : a;
771 * Returns the absolute value of a {@code double} value.
772 * If the argument is not negative, the argument is returned.
773 * If the argument is negative, the negation of the argument is returned.
775 * <ul><li>If the argument is positive zero or negative zero, the result
777 * <li>If the argument is infinite, the result is positive infinity.
778 * <li>If the argument is NaN, the result is NaN.</ul>
779 * In other words, the result is the same as the value of the expression:
780 * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)}
782 * @param a the argument whose absolute value is to be determined
783 * @return the absolute value of the argument.
785 public static double abs(double a) {
786 return (a <= 0.0D) ? 0.0D - a : a;
790 * Returns the greater of two {@code int} values. That is, the
791 * result is the argument closer to the value of
792 * {@link Integer#MAX_VALUE}. If the arguments have the same value,
793 * the result is that same value.
795 * @param a an argument.
796 * @param b another argument.
797 * @return the larger of {@code a} and {@code b}.
799 public static int max(int a, int b) {
800 return (a >= b) ? a : b;
804 * Returns the greater of two {@code long} values. That is, the
805 * result is the argument closer to the value of
806 * {@link Long#MAX_VALUE}. If the arguments have the same value,
807 * the result is that same value.
809 * @param a an argument.
810 * @param b another argument.
811 * @return the larger of {@code a} and {@code b}.
813 public static long max(long a, long b) {
814 return (a >= b) ? a : b;
818 * Returns the greater of two {@code float} values. That is,
819 * the result is the argument closer to positive infinity. If the
820 * arguments have the same value, the result is that same
821 * value. If either value is NaN, then the result is NaN. Unlike
822 * the numerical comparison operators, this method considers
823 * negative zero to be strictly smaller than positive zero. If one
824 * argument is positive zero and the other negative zero, the
825 * result is positive zero.
827 * @param a an argument.
828 * @param b another argument.
829 * @return the larger of {@code a} and {@code b}.
831 @JavaScriptBody(args={"a", "b"},
832 body="return Math.max(a,b);"
834 public static float max(float a, float b) {
835 throw new UnsupportedOperationException();
839 * Returns the greater of two {@code double} values. That
840 * is, the result is the argument closer to positive infinity. If
841 * the arguments have the same value, the result is that same
842 * value. If either value is NaN, then the result is NaN. Unlike
843 * the numerical comparison operators, this method considers
844 * negative zero to be strictly smaller than positive zero. If one
845 * argument is positive zero and the other negative zero, the
846 * result is positive zero.
848 * @param a an argument.
849 * @param b another argument.
850 * @return the larger of {@code a} and {@code b}.
852 @JavaScriptBody(args={"a", "b"},
853 body="return Math.max(a,b);"
855 public static double max(double a, double b) {
856 throw new UnsupportedOperationException();
860 * Returns the smaller of two {@code int} values. That is,
861 * the result the argument closer to the value of
862 * {@link Integer#MIN_VALUE}. If the arguments have the same
863 * value, the result is that same value.
865 * @param a an argument.
866 * @param b another argument.
867 * @return the smaller of {@code a} and {@code b}.
869 public static int min(int a, int b) {
870 return (a <= b) ? a : b;
874 * Returns the smaller of two {@code long} values. That is,
875 * the result is the argument closer to the value of
876 * {@link Long#MIN_VALUE}. If the arguments have the same
877 * value, the result is that same value.
879 * @param a an argument.
880 * @param b another argument.
881 * @return the smaller of {@code a} and {@code b}.
883 public static long min(long a, long b) {
884 return (a <= b) ? a : b;
888 * Returns the smaller of two {@code float} values. That is,
889 * the result is the value closer to negative infinity. If the
890 * arguments have the same value, the result is that same
891 * value. If either value is NaN, then the result is NaN. Unlike
892 * the numerical comparison operators, this method considers
893 * negative zero to be strictly smaller than positive zero. If
894 * one argument is positive zero and the other is negative zero,
895 * the result is negative zero.
897 * @param a an argument.
898 * @param b another argument.
899 * @return the smaller of {@code a} and {@code b}.
901 @JavaScriptBody(args={"a", "b"},
902 body="return Math.min(a,b);"
904 public static float min(float a, float b) {
905 throw new UnsupportedOperationException();
909 * Returns the smaller of two {@code double} values. That
910 * is, the result is the value closer to negative infinity. If the
911 * arguments have the same value, the result is that same
912 * value. If either value is NaN, then the result is NaN. Unlike
913 * the numerical comparison operators, this method considers
914 * negative zero to be strictly smaller than positive zero. If one
915 * argument is positive zero and the other is negative zero, the
916 * result is negative zero.
918 * @param a an argument.
919 * @param b another argument.
920 * @return the smaller of {@code a} and {@code b}.
922 @JavaScriptBody(args={"a", "b"},
923 body="return Math.min(a,b);"
925 public static double min(double a, double b) {
926 throw new UnsupportedOperationException();
930 * Returns the size of an ulp of the argument. An ulp of a
931 * {@code double} value is the positive distance between this
932 * floating-point value and the {@code double} value next
933 * larger in magnitude. Note that for non-NaN <i>x</i>,
934 * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
938 * <li> If the argument is NaN, then the result is NaN.
939 * <li> If the argument is positive or negative infinity, then the
940 * result is positive infinity.
941 * <li> If the argument is positive or negative zero, then the result is
942 * {@code Double.MIN_VALUE}.
943 * <li> If the argument is ±{@code Double.MAX_VALUE}, then
944 * the result is equal to 2<sup>971</sup>.
947 * @param d the floating-point value whose ulp is to be returned
948 * @return the size of an ulp of the argument
949 * @author Joseph D. Darcy
952 // public static double ulp(double d) {
953 // return sun.misc.FpUtils.ulp(d);
957 * Returns the size of an ulp of the argument. An ulp of a
958 * {@code float} value is the positive distance between this
959 * floating-point value and the {@code float} value next
960 * larger in magnitude. Note that for non-NaN <i>x</i>,
961 * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
965 * <li> If the argument is NaN, then the result is NaN.
966 * <li> If the argument is positive or negative infinity, then the
967 * result is positive infinity.
968 * <li> If the argument is positive or negative zero, then the result is
969 * {@code Float.MIN_VALUE}.
970 * <li> If the argument is ±{@code Float.MAX_VALUE}, then
971 * the result is equal to 2<sup>104</sup>.
974 * @param f the floating-point value whose ulp is to be returned
975 * @return the size of an ulp of the argument
976 * @author Joseph D. Darcy
979 // public static float ulp(float f) {
980 // return sun.misc.FpUtils.ulp(f);
984 * Returns the signum function of the argument; zero if the argument
985 * is zero, 1.0 if the argument is greater than zero, -1.0 if the
986 * argument is less than zero.
990 * <li> If the argument is NaN, then the result is NaN.
991 * <li> If the argument is positive zero or negative zero, then the
992 * result is the same as the argument.
995 * @param d the floating-point value whose signum is to be returned
996 * @return the signum function of the argument
997 * @author Joseph D. Darcy
1000 public static double signum(double d) {
1001 if (d < 0.0) { return -1.0; }
1002 if (d > 0.0) { return 1.0; }
1007 * Returns the signum function of the argument; zero if the argument
1008 * is zero, 1.0f if the argument is greater than zero, -1.0f if the
1009 * argument is less than zero.
1013 * <li> If the argument is NaN, then the result is NaN.
1014 * <li> If the argument is positive zero or negative zero, then the
1015 * result is the same as the argument.
1018 * @param f the floating-point value whose signum is to be returned
1019 * @return the signum function of the argument
1020 * @author Joseph D. Darcy
1023 public static float signum(float f) {
1024 if (f < 0.0f) { return -1.0f; }
1025 if (f > 0.0f) { return 1.0f; }
1030 * Returns the first floating-point argument with the sign of the
1031 * second floating-point argument. Note that unlike the {@link
1032 * StrictMath#copySign(double, double) StrictMath.copySign}
1033 * method, this method does not require NaN {@code sign}
1034 * arguments to be treated as positive values; implementations are
1035 * permitted to treat some NaN arguments as positive and other NaN
1036 * arguments as negative to allow greater performance.
1038 * @param magnitude the parameter providing the magnitude of the result
1039 * @param sign the parameter providing the sign of the result
1040 * @return a value with the magnitude of {@code magnitude}
1041 * and the sign of {@code sign}.
1044 // public static double copySign(double magnitude, double sign) {
1045 // return sun.misc.FpUtils.rawCopySign(magnitude, sign);
1049 * Returns the first floating-point argument with the sign of the
1050 * second floating-point argument. Note that unlike the {@link
1051 * StrictMath#copySign(float, float) StrictMath.copySign}
1052 * method, this method does not require NaN {@code sign}
1053 * arguments to be treated as positive values; implementations are
1054 * permitted to treat some NaN arguments as positive and other NaN
1055 * arguments as negative to allow greater performance.
1057 * @param magnitude the parameter providing the magnitude of the result
1058 * @param sign the parameter providing the sign of the result
1059 * @return a value with the magnitude of {@code magnitude}
1060 * and the sign of {@code sign}.
1063 // public static float copySign(float magnitude, float sign) {
1064 // return sun.misc.FpUtils.rawCopySign(magnitude, sign);
1068 * Returns the unbiased exponent used in the representation of a
1069 * {@code float}. Special cases:
1072 * <li>If the argument is NaN or infinite, then the result is
1073 * {@link Float#MAX_EXPONENT} + 1.
1074 * <li>If the argument is zero or subnormal, then the result is
1075 * {@link Float#MIN_EXPONENT} -1.
1077 * @param f a {@code float} value
1078 * @return the unbiased exponent of the argument
1081 // public static int getExponent(float f) {
1082 // return sun.misc.FpUtils.getExponent(f);
1086 * Returns the unbiased exponent used in the representation of a
1087 * {@code double}. Special cases:
1090 * <li>If the argument is NaN or infinite, then the result is
1091 * {@link Double#MAX_EXPONENT} + 1.
1092 * <li>If the argument is zero or subnormal, then the result is
1093 * {@link Double#MIN_EXPONENT} -1.
1095 * @param d a {@code double} value
1096 * @return the unbiased exponent of the argument
1099 // public static int getExponent(double d) {
1100 // return sun.misc.FpUtils.getExponent(d);
1104 * Returns the floating-point number adjacent to the first
1105 * argument in the direction of the second argument. If both
1106 * arguments compare as equal the second argument is returned.
1111 * <li> If either argument is a NaN, then NaN is returned.
1113 * <li> If both arguments are signed zeros, {@code direction}
1114 * is returned unchanged (as implied by the requirement of
1115 * returning the second argument if the arguments compare as
1118 * <li> If {@code start} is
1119 * ±{@link Double#MIN_VALUE} and {@code direction}
1120 * has a value such that the result should have a smaller
1121 * magnitude, then a zero with the same sign as {@code start}
1124 * <li> If {@code start} is infinite and
1125 * {@code direction} has a value such that the result should
1126 * have a smaller magnitude, {@link Double#MAX_VALUE} with the
1127 * same sign as {@code start} is returned.
1129 * <li> If {@code start} is equal to ±
1130 * {@link Double#MAX_VALUE} and {@code direction} has a
1131 * value such that the result should have a larger magnitude, an
1132 * infinity with same sign as {@code start} is returned.
1135 * @param start starting floating-point value
1136 * @param direction value indicating which of
1137 * {@code start}'s neighbors or {@code start} should
1139 * @return The floating-point number adjacent to {@code start} in the
1140 * direction of {@code direction}.
1143 // public static double nextAfter(double start, double direction) {
1144 // return sun.misc.FpUtils.nextAfter(start, direction);
1148 * Returns the floating-point number adjacent to the first
1149 * argument in the direction of the second argument. If both
1150 * arguments compare as equal a value equivalent to the second argument
1156 * <li> If either argument is a NaN, then NaN is returned.
1158 * <li> If both arguments are signed zeros, a value equivalent
1159 * to {@code direction} is returned.
1161 * <li> If {@code start} is
1162 * ±{@link Float#MIN_VALUE} and {@code direction}
1163 * has a value such that the result should have a smaller
1164 * magnitude, then a zero with the same sign as {@code start}
1167 * <li> If {@code start} is infinite and
1168 * {@code direction} has a value such that the result should
1169 * have a smaller magnitude, {@link Float#MAX_VALUE} with the
1170 * same sign as {@code start} is returned.
1172 * <li> If {@code start} is equal to ±
1173 * {@link Float#MAX_VALUE} and {@code direction} has a
1174 * value such that the result should have a larger magnitude, an
1175 * infinity with same sign as {@code start} is returned.
1178 * @param start starting floating-point value
1179 * @param direction value indicating which of
1180 * {@code start}'s neighbors or {@code start} should
1182 * @return The floating-point number adjacent to {@code start} in the
1183 * direction of {@code direction}.
1186 // public static float nextAfter(float start, double direction) {
1187 // return sun.misc.FpUtils.nextAfter(start, direction);
1191 * Returns the floating-point value adjacent to {@code d} in
1192 * the direction of positive infinity. This method is
1193 * semantically equivalent to {@code nextAfter(d,
1194 * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
1195 * implementation may run faster than its equivalent
1196 * {@code nextAfter} call.
1200 * <li> If the argument is NaN, the result is NaN.
1202 * <li> If the argument is positive infinity, the result is
1203 * positive infinity.
1205 * <li> If the argument is zero, the result is
1206 * {@link Double#MIN_VALUE}
1210 * @param d starting floating-point value
1211 * @return The adjacent floating-point value closer to positive
1215 // public static double nextUp(double d) {
1216 // return sun.misc.FpUtils.nextUp(d);
1220 * Returns the floating-point value adjacent to {@code f} in
1221 * the direction of positive infinity. This method is
1222 * semantically equivalent to {@code nextAfter(f,
1223 * Float.POSITIVE_INFINITY)}; however, a {@code nextUp}
1224 * implementation may run faster than its equivalent
1225 * {@code nextAfter} call.
1229 * <li> If the argument is NaN, the result is NaN.
1231 * <li> If the argument is positive infinity, the result is
1232 * positive infinity.
1234 * <li> If the argument is zero, the result is
1235 * {@link Float#MIN_VALUE}
1239 * @param f starting floating-point value
1240 * @return The adjacent floating-point value closer to positive
1244 // public static float nextUp(float f) {
1245 // return sun.misc.FpUtils.nextUp(f);
1250 * Return {@code d} ×
1251 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1252 * by a single correctly rounded floating-point multiply to a
1253 * member of the double value set. See the Java
1254 * Language Specification for a discussion of floating-point
1255 * value sets. If the exponent of the result is between {@link
1256 * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
1257 * answer is calculated exactly. If the exponent of the result
1258 * would be larger than {@code Double.MAX_EXPONENT}, an
1259 * infinity is returned. Note that if the result is subnormal,
1260 * precision may be lost; that is, when {@code scalb(x, n)}
1261 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1262 * <i>x</i>. When the result is non-NaN, the result has the same
1263 * sign as {@code d}.
1267 * <li> If the first argument is NaN, NaN is returned.
1268 * <li> If the first argument is infinite, then an infinity of the
1269 * same sign is returned.
1270 * <li> If the first argument is zero, then a zero of the same
1274 * @param d number to be scaled by a power of two.
1275 * @param scaleFactor power of 2 used to scale {@code d}
1276 * @return {@code d} × 2<sup>{@code scaleFactor}</sup>
1279 // public static double scalb(double d, int scaleFactor) {
1280 // return sun.misc.FpUtils.scalb(d, scaleFactor);
1284 * Return {@code f} ×
1285 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1286 * by a single correctly rounded floating-point multiply to a
1287 * member of the float value set. See the Java
1288 * Language Specification for a discussion of floating-point
1289 * value sets. If the exponent of the result is between {@link
1290 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
1291 * answer is calculated exactly. If the exponent of the result
1292 * would be larger than {@code Float.MAX_EXPONENT}, an
1293 * infinity is returned. Note that if the result is subnormal,
1294 * precision may be lost; that is, when {@code scalb(x, n)}
1295 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1296 * <i>x</i>. When the result is non-NaN, the result has the same
1297 * sign as {@code f}.
1301 * <li> If the first argument is NaN, NaN is returned.
1302 * <li> If the first argument is infinite, then an infinity of the
1303 * same sign is returned.
1304 * <li> If the first argument is zero, then a zero of the same
1308 * @param f number to be scaled by a power of two.
1309 * @param scaleFactor power of 2 used to scale {@code f}
1310 * @return {@code f} × 2<sup>{@code scaleFactor}</sup>
1313 // public static float scalb(float f, int scaleFactor) {
1314 // return sun.misc.FpUtils.scalb(f, scaleFactor);