• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
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.
10  *
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).
16  *
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.
20  *
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
23  * questions.
24  */
25 
26 package java.lang;
27 import java.util.Random;
28 import sun.misc.FpUtils;
29 import sun.misc.DoubleConsts;
30 
31 /**
32  * The class {@code StrictMath} contains methods for performing basic
33  * numeric operations such as the elementary exponential, logarithm,
34  * square root, and trigonometric functions.
35  *
36  * <p>To help ensure portability of Java programs, the definitions of
37  * some of the numeric functions in this package require that they
38  * produce the same results as certain published algorithms. These
39  * algorithms are available from the well-known network library
40  * {@code netlib} as the package "Freely Distributable Math
41  * Library," <a
42  * href="ftp://ftp.netlib.org/fdlibm.tar">{@code fdlibm}</a>. These
43  * algorithms, which are written in the C programming language, are
44  * then to be understood as executed with all floating-point
45  * operations following the rules of Java floating-point arithmetic.
46  *
47  * <p>The Java math library is defined with respect to
48  * {@code fdlibm} version 5.3. Where {@code fdlibm} provides
49  * more than one definition for a function (such as
50  * {@code acos}), use the "IEEE 754 core function" version
51  * (residing in a file whose name begins with the letter
52  * {@code e}).  The methods which require {@code fdlibm}
53  * semantics are {@code sin}, {@code cos}, {@code tan},
54  * {@code asin}, {@code acos}, {@code atan},
55  * {@code exp}, {@code log}, {@code log10},
56  * {@code cbrt}, {@code atan2}, {@code pow},
57  * {@code sinh}, {@code cosh}, {@code tanh},
58  * {@code hypot}, {@code expm1}, and {@code log1p}.
59  *
60  * @author  unascribed
61  * @author  Joseph D. Darcy
62  * @since   1.3
63  */
64 
65 public final class StrictMath {
66 
67     /**
68      * Don't let anyone instantiate this class.
69      */
StrictMath()70     private StrictMath() {}
71 
72     /**
73      * The {@code double} value that is closer than any other to
74      * <i>e</i>, the base of the natural logarithms.
75      */
76     public static final double E = 2.7182818284590452354;
77 
78     /**
79      * The {@code double} value that is closer than any other to
80      * <i>pi</i>, the ratio of the circumference of a circle to its
81      * diameter.
82      */
83     public static final double PI = 3.14159265358979323846;
84 
85     /**
86      * Returns the trigonometric sine of an angle. Special cases:
87      * <ul><li>If the argument is NaN or an infinity, then the
88      * result is NaN.
89      * <li>If the argument is zero, then the result is a zero with the
90      * same sign as the argument.</ul>
91      *
92      * @param   a   an angle, in radians.
93      * @return  the sine of the argument.
94      */
sin(double a)95     public static native double sin(double a);
96 
97     /**
98      * Returns the trigonometric cosine of an angle. Special cases:
99      * <ul><li>If the argument is NaN or an infinity, then the
100      * result is NaN.</ul>
101      *
102      * @param   a   an angle, in radians.
103      * @return  the cosine of the argument.
104      */
cos(double a)105     public static native double cos(double a);
106 
107     /**
108      * Returns the trigonometric tangent of an angle. Special cases:
109      * <ul><li>If the argument is NaN or an infinity, then the result
110      * is NaN.
111      * <li>If the argument is zero, then the result is a zero with the
112      * same sign as the argument.</ul>
113      *
114      * @param   a   an angle, in radians.
115      * @return  the tangent of the argument.
116      */
tan(double a)117     public static native double tan(double a);
118 
119     /**
120      * Returns the arc sine of a value; the returned angle is in the
121      * range -<i>pi</i>/2 through <i>pi</i>/2.  Special cases:
122      * <ul><li>If the argument is NaN or its absolute value is greater
123      * than 1, then the result is NaN.
124      * <li>If the argument is zero, then the result is a zero with the
125      * same sign as the argument.</ul>
126      *
127      * @param   a   the value whose arc sine is to be returned.
128      * @return  the arc sine of the argument.
129      */
asin(double a)130     public static native double asin(double a);
131 
132     /**
133      * Returns the arc cosine of a value; the returned angle is in the
134      * range 0.0 through <i>pi</i>.  Special case:
135      * <ul><li>If the argument is NaN or its absolute value is greater
136      * than 1, then the result is NaN.</ul>
137      *
138      * @param   a   the value whose arc cosine is to be returned.
139      * @return  the arc cosine of the argument.
140      */
acos(double a)141     public static native double acos(double a);
142 
143     /**
144      * Returns the arc tangent of a value; the returned angle is in the
145      * range -<i>pi</i>/2 through <i>pi</i>/2.  Special cases:
146      * <ul><li>If the argument is NaN, then the result is NaN.
147      * <li>If the argument is zero, then the result is a zero with the
148      * same sign as the argument.</ul>
149      *
150      * @param   a   the value whose arc tangent is to be returned.
151      * @return  the arc tangent of the argument.
152      */
atan(double a)153     public static native double atan(double a);
154 
155     /**
156      * Converts an angle measured in degrees to an approximately
157      * equivalent angle measured in radians.  The conversion from
158      * degrees to radians is generally inexact.
159      *
160      * @param   angdeg   an angle, in degrees
161      * @return  the measurement of the angle {@code angdeg}
162      *          in radians.
163      */
toRadians(double angdeg)164     public static strictfp double toRadians(double angdeg) {
165         return angdeg / 180.0 * PI;
166     }
167 
168     /**
169      * Converts an angle measured in radians to an approximately
170      * equivalent angle measured in degrees.  The conversion from
171      * radians to degrees is generally inexact; users should
172      * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
173      * equal {@code 0.0}.
174      *
175      * @param   angrad   an angle, in radians
176      * @return  the measurement of the angle {@code angrad}
177      *          in degrees.
178      */
toDegrees(double angrad)179     public static strictfp double toDegrees(double angrad) {
180         return angrad * 180.0 / PI;
181     }
182 
183     /**
184      * Returns Euler's number <i>e</i> raised to the power of a
185      * {@code double} value. Special cases:
186      * <ul><li>If the argument is NaN, the result is NaN.
187      * <li>If the argument is positive infinity, then the result is
188      * positive infinity.
189      * <li>If the argument is negative infinity, then the result is
190      * positive zero.</ul>
191      *
192      * @param   a   the exponent to raise <i>e</i> to.
193      * @return  the value <i>e</i><sup>{@code a}</sup>,
194      *          where <i>e</i> is the base of the natural logarithms.
195      */
exp(double a)196     public static native double exp(double a);
197 
198     /**
199      * Returns the natural logarithm (base <i>e</i>) of a {@code double}
200      * value. Special cases:
201      * <ul><li>If the argument is NaN or less than zero, then the result
202      * is NaN.
203      * <li>If the argument is positive infinity, then the result is
204      * positive infinity.
205      * <li>If the argument is positive zero or negative zero, then the
206      * result is negative infinity.</ul>
207      *
208      * @param   a   a value
209      * @return  the value ln&nbsp;{@code a}, the natural logarithm of
210      *          {@code a}.
211      */
log(double a)212     public static native double log(double a);
213 
214 
215     /**
216      * Returns the base 10 logarithm of a {@code double} value.
217      * Special cases:
218      *
219      * <ul><li>If the argument is NaN or less than zero, then the result
220      * is NaN.
221      * <li>If the argument is positive infinity, then the result is
222      * positive infinity.
223      * <li>If the argument is positive zero or negative zero, then the
224      * result is negative infinity.
225      * <li> If the argument is equal to 10<sup><i>n</i></sup> for
226      * integer <i>n</i>, then the result is <i>n</i>.
227      * </ul>
228      *
229      * @param   a   a value
230      * @return  the base 10 logarithm of  {@code a}.
231      * @since 1.5
232      */
log10(double a)233     public static native double log10(double a);
234 
235     /**
236      * Returns the correctly rounded positive square root of a
237      * {@code double} value.
238      * Special cases:
239      * <ul><li>If the argument is NaN or less than zero, then the result
240      * is NaN.
241      * <li>If the argument is positive infinity, then the result is positive
242      * infinity.
243      * <li>If the argument is positive zero or negative zero, then the
244      * result is the same as the argument.</ul>
245      * Otherwise, the result is the {@code double} value closest to
246      * the true mathematical square root of the argument value.
247      *
248      * @param   a   a value.
249      * @return  the positive square root of {@code a}.
250      */
sqrt(double a)251     public static native double sqrt(double a);
252 
253     /**
254      * Returns the cube root of a {@code double} value.  For
255      * positive finite {@code x}, {@code cbrt(-x) ==
256      * -cbrt(x)}; that is, the cube root of a negative value is
257      * the negative of the cube root of that value's magnitude.
258      * Special cases:
259      *
260      * <ul>
261      *
262      * <li>If the argument is NaN, then the result is NaN.
263      *
264      * <li>If the argument is infinite, then the result is an infinity
265      * with the same sign as the argument.
266      *
267      * <li>If the argument is zero, then the result is a zero with the
268      * same sign as the argument.
269      *
270      * </ul>
271      *
272      * @param   a   a value.
273      * @return  the cube root of {@code a}.
274      * @since 1.5
275      */
cbrt(double a)276     public static native double cbrt(double a);
277 
278     /**
279      * Computes the remainder operation on two arguments as prescribed
280      * by the IEEE 754 standard.
281      * The remainder value is mathematically equal to
282      * <code>f1&nbsp;-&nbsp;f2</code>&nbsp;&times;&nbsp;<i>n</i>,
283      * where <i>n</i> is the mathematical integer closest to the exact
284      * mathematical value of the quotient {@code f1/f2}, and if two
285      * mathematical integers are equally close to {@code f1/f2},
286      * then <i>n</i> is the integer that is even. If the remainder is
287      * zero, its sign is the same as the sign of the first argument.
288      * Special cases:
289      * <ul><li>If either argument is NaN, or the first argument is infinite,
290      * or the second argument is positive zero or negative zero, then the
291      * result is NaN.
292      * <li>If the first argument is finite and the second argument is
293      * infinite, then the result is the same as the first argument.</ul>
294      *
295      * @param   f1   the dividend.
296      * @param   f2   the divisor.
297      * @return  the remainder when {@code f1} is divided by
298      *          {@code f2}.
299      */
IEEEremainder(double f1, double f2)300     public static native double IEEEremainder(double f1, double f2);
301 
302     /**
303      * Returns the smallest (closest to negative infinity)
304      * {@code double} value that is greater than or equal to the
305      * argument and is equal to a mathematical integer. Special cases:
306      * <ul><li>If the argument value is already equal to a
307      * mathematical integer, then the result is the same as the
308      * argument.  <li>If the argument is NaN or an infinity or
309      * positive zero or negative zero, then the result is the same as
310      * the argument.  <li>If the argument value is less than zero but
311      * greater than -1.0, then the result is negative zero.</ul> Note
312      * that the value of {@code StrictMath.ceil(x)} is exactly the
313      * value of {@code -StrictMath.floor(-x)}.
314      *
315      * @param   a   a value.
316      * @return  the smallest (closest to negative infinity)
317      *          floating-point value that is greater than or equal to
318      *          the argument and is equal to a mathematical integer.
319      */
ceil(double a)320     public static double ceil(double a) {
321         return floorOrCeil(a, -0.0, 1.0, 1.0);
322     }
323 
324     /**
325      * Returns the largest (closest to positive infinity)
326      * {@code double} value that is less than or equal to the
327      * argument and is equal to a mathematical integer. Special cases:
328      * <ul><li>If the argument value is already equal to a
329      * mathematical integer, then the result is the same as the
330      * argument.  <li>If the argument is NaN or an infinity or
331      * positive zero or negative zero, then the result is the same as
332      * the argument.</ul>
333      *
334      * @param   a   a value.
335      * @return  the largest (closest to positive infinity)
336      *          floating-point value that less than or equal to the argument
337      *          and is equal to a mathematical integer.
338      */
floor(double a)339     public static double floor(double a) {
340         return floorOrCeil(a, -1.0, 0.0, -1.0);
341     }
342 
343     /**
344      * Internal method to share logic between floor and ceil.
345      *
346      * @param a the value to be floored or ceiled
347      * @param negativeBoundary result for values in (-1, 0)
348      * @param positiveBoundary result for values in (0, 1)
349      * @param increment value to add when the argument is non-integral
350      */
floorOrCeil(double a, double negativeBoundary, double positiveBoundary, double sign)351     private static double floorOrCeil(double a,
352                                       double negativeBoundary,
353                                       double positiveBoundary,
354                                       double sign) {
355         int exponent = Math.getExponent(a);
356 
357         if (exponent < 0) {
358             /*
359              * Absolute value of argument is less than 1.
360              * floorOrceil(-0.0) => -0.0
361              * floorOrceil(+0.0) => +0.0
362              */
363             return ((a == 0.0) ? a :
364                     ( (a < 0.0) ?  negativeBoundary : positiveBoundary) );
365         } else if (exponent >= 52) {
366             /*
367              * Infinity, NaN, or a value so large it must be integral.
368              */
369             return a;
370         }
371         // Else the argument is either an integral value already XOR it
372         // has to be rounded to one.
373         assert exponent >= 0 && exponent <= 51;
374 
375         long doppel = Double.doubleToRawLongBits(a);
376         long mask   = DoubleConsts.SIGNIF_BIT_MASK >> exponent;
377 
378         if ( (mask & doppel) == 0L )
379             return a; // integral value
380         else {
381             double result = Double.longBitsToDouble(doppel & (~mask));
382             if (sign*a > 0.0)
383                 result = result + sign;
384             return result;
385         }
386     }
387 
388     /**
389      * Returns the {@code double} value that is closest in value
390      * to the argument and is equal to a mathematical integer. If two
391      * {@code double} values that are mathematical integers are
392      * equally close to the value of the argument, the result is the
393      * integer value that is even. Special cases:
394      * <ul><li>If the argument value is already equal to a mathematical
395      * integer, then the result is the same as the argument.
396      * <li>If the argument is NaN or an infinity or positive zero or negative
397      * zero, then the result is the same as the argument.</ul>
398      *
399      * @param   a   a value.
400      * @return  the closest floating-point value to {@code a} that is
401      *          equal to a mathematical integer.
402      * @author Joseph D. Darcy
403      */
rint(double a)404     public static double rint(double a) {
405         /*
406          * If the absolute value of a is not less than 2^52, it
407          * is either a finite integer (the double format does not have
408          * enough significand bits for a number that large to have any
409          * fractional portion), an infinity, or a NaN.  In any of
410          * these cases, rint of the argument is the argument.
411          *
412          * Otherwise, the sum (twoToThe52 + a ) will properly round
413          * away any fractional portion of a since ulp(twoToThe52) ==
414          * 1.0; subtracting out twoToThe52 from this sum will then be
415          * exact and leave the rounded integer portion of a.
416          *
417          * This method does *not* need to be declared strictfp to get
418          * fully reproducible results.  Whether or not a method is
419          * declared strictfp can only make a difference in the
420          * returned result if some operation would overflow or
421          * underflow with strictfp semantics.  The operation
422          * (twoToThe52 + a ) cannot overflow since large values of a
423          * are screened out; the add cannot underflow since twoToThe52
424          * is too large.  The subtraction ((twoToThe52 + a ) -
425          * twoToThe52) will be exact as discussed above and thus
426          * cannot overflow or meaningfully underflow.  Finally, the
427          * last multiply in the return statement is by plus or minus
428          * 1.0, which is exact too.
429          */
430         double twoToThe52 = (double)(1L << 52); // 2^52
431         double sign = FpUtils.rawCopySign(1.0, a); // preserve sign info
432         a = Math.abs(a);
433 
434         if (a < twoToThe52) { // E_min <= ilogb(a) <= 51
435             a = ((twoToThe52 + a ) - twoToThe52);
436         }
437 
438         return sign * a; // restore original sign
439     }
440 
441     /**
442      * Returns the angle <i>theta</i> from the conversion of rectangular
443      * coordinates ({@code x},&nbsp;{@code y}) to polar
444      * coordinates (r,&nbsp;<i>theta</i>).
445      * This method computes the phase <i>theta</i> by computing an arc tangent
446      * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special
447      * cases:
448      * <ul><li>If either argument is NaN, then the result is NaN.
449      * <li>If the first argument is positive zero and the second argument
450      * is positive, or the first argument is positive and finite and the
451      * second argument is positive infinity, then the result is positive
452      * zero.
453      * <li>If the first argument is negative zero and the second argument
454      * is positive, or the first argument is negative and finite and the
455      * second argument is positive infinity, then the result is negative zero.
456      * <li>If the first argument is positive zero and the second argument
457      * is negative, or the first argument is positive and finite and the
458      * second argument is negative infinity, then the result is the
459      * {@code double} value closest to <i>pi</i>.
460      * <li>If the first argument is negative zero and the second argument
461      * is negative, or the first argument is negative and finite and the
462      * second argument is negative infinity, then the result is the
463      * {@code double} value closest to -<i>pi</i>.
464      * <li>If the first argument is positive and the second argument is
465      * positive zero or negative zero, or the first argument is positive
466      * infinity and the second argument is finite, then the result is the
467      * {@code double} value closest to <i>pi</i>/2.
468      * <li>If the first argument is negative and the second argument is
469      * positive zero or negative zero, or the first argument is negative
470      * infinity and the second argument is finite, then the result is the
471      * {@code double} value closest to -<i>pi</i>/2.
472      * <li>If both arguments are positive infinity, then the result is the
473      * {@code double} value closest to <i>pi</i>/4.
474      * <li>If the first argument is positive infinity and the second argument
475      * is negative infinity, then the result is the {@code double}
476      * value closest to 3*<i>pi</i>/4.
477      * <li>If the first argument is negative infinity and the second argument
478      * is positive infinity, then the result is the {@code double} value
479      * closest to -<i>pi</i>/4.
480      * <li>If both arguments are negative infinity, then the result is the
481      * {@code double} value closest to -3*<i>pi</i>/4.</ul>
482      *
483      * @param   y   the ordinate coordinate
484      * @param   x   the abscissa coordinate
485      * @return  the <i>theta</i> component of the point
486      *          (<i>r</i>,&nbsp;<i>theta</i>)
487      *          in polar coordinates that corresponds to the point
488      *          (<i>x</i>,&nbsp;<i>y</i>) in Cartesian coordinates.
489      */
atan2(double y, double x)490     public static native double atan2(double y, double x);
491 
492 
493     /**
494      * Returns the value of the first argument raised to the power of the
495      * second argument. Special cases:
496      *
497      * <ul><li>If the second argument is positive or negative zero, then the
498      * result is 1.0.
499      * <li>If the second argument is 1.0, then the result is the same as the
500      * first argument.
501      * <li>If the second argument is NaN, then the result is NaN.
502      * <li>If the first argument is NaN and the second argument is nonzero,
503      * then the result is NaN.
504      *
505      * <li>If
506      * <ul>
507      * <li>the absolute value of the first argument is greater than 1
508      * and the second argument is positive infinity, or
509      * <li>the absolute value of the first argument is less than 1 and
510      * the second argument is negative infinity,
511      * </ul>
512      * then the result is positive infinity.
513      *
514      * <li>If
515      * <ul>
516      * <li>the absolute value of the first argument is greater than 1 and
517      * the second argument is negative infinity, or
518      * <li>the absolute value of the
519      * first argument is less than 1 and the second argument is positive
520      * infinity,
521      * </ul>
522      * then the result is positive zero.
523      *
524      * <li>If the absolute value of the first argument equals 1 and the
525      * second argument is infinite, then the result is NaN.
526      *
527      * <li>If
528      * <ul>
529      * <li>the first argument is positive zero and the second argument
530      * is greater than zero, or
531      * <li>the first argument is positive infinity and the second
532      * argument is less than zero,
533      * </ul>
534      * then the result is positive zero.
535      *
536      * <li>If
537      * <ul>
538      * <li>the first argument is positive zero and the second argument
539      * is less than zero, or
540      * <li>the first argument is positive infinity and the second
541      * argument is greater than zero,
542      * </ul>
543      * then the result is positive infinity.
544      *
545      * <li>If
546      * <ul>
547      * <li>the first argument is negative zero and the second argument
548      * is greater than zero but not a finite odd integer, or
549      * <li>the first argument is negative infinity and the second
550      * argument is less than zero but not a finite odd integer,
551      * </ul>
552      * then the result is positive zero.
553      *
554      * <li>If
555      * <ul>
556      * <li>the first argument is negative zero and the second argument
557      * is a positive finite odd integer, or
558      * <li>the first argument is negative infinity and the second
559      * argument is a negative finite odd integer,
560      * </ul>
561      * then the result is negative zero.
562      *
563      * <li>If
564      * <ul>
565      * <li>the first argument is negative zero and the second argument
566      * is less than zero but not a finite odd integer, or
567      * <li>the first argument is negative infinity and the second
568      * argument is greater than zero but not a finite odd integer,
569      * </ul>
570      * then the result is positive infinity.
571      *
572      * <li>If
573      * <ul>
574      * <li>the first argument is negative zero and the second argument
575      * is a negative finite odd integer, or
576      * <li>the first argument is negative infinity and the second
577      * argument is a positive finite odd integer,
578      * </ul>
579      * then the result is negative infinity.
580      *
581      * <li>If the first argument is finite and less than zero
582      * <ul>
583      * <li> if the second argument is a finite even integer, the
584      * result is equal to the result of raising the absolute value of
585      * the first argument to the power of the second argument
586      *
587      * <li>if the second argument is a finite odd integer, the result
588      * is equal to the negative of the result of raising the absolute
589      * value of the first argument to the power of the second
590      * argument
591      *
592      * <li>if the second argument is finite and not an integer, then
593      * the result is NaN.
594      * </ul>
595      *
596      * <li>If both arguments are integers, then the result is exactly equal
597      * to the mathematical result of raising the first argument to the power
598      * of the second argument if that result can in fact be represented
599      * exactly as a {@code double} value.</ul>
600      *
601      * <p>(In the foregoing descriptions, a floating-point value is
602      * considered to be an integer if and only if it is finite and a
603      * fixed point of the method {@link #ceil ceil} or,
604      * equivalently, a fixed point of the method {@link #floor
605      * floor}. A value is a fixed point of a one-argument
606      * method if and only if the result of applying the method to the
607      * value is equal to the value.)
608      *
609      * @param   a   base.
610      * @param   b   the exponent.
611      * @return  the value {@code a}<sup>{@code b}</sup>.
612      */
pow(double a, double b)613     public static native double pow(double a, double b);
614 
615     /**
616      * Returns the closest {@code int} to the argument, with ties
617      * rounding to positive infinity.
618      *
619      * <p>Special cases:
620      * <ul><li>If the argument is NaN, the result is 0.
621      * <li>If the argument is negative infinity or any value less than or
622      * equal to the value of {@code Integer.MIN_VALUE}, the result is
623      * equal to the value of {@code Integer.MIN_VALUE}.
624      * <li>If the argument is positive infinity or any value greater than or
625      * equal to the value of {@code Integer.MAX_VALUE}, the result is
626      * equal to the value of {@code Integer.MAX_VALUE}.</ul>
627      *
628      * @param   a   a floating-point value to be rounded to an integer.
629      * @return  the value of the argument rounded to the nearest
630      *          {@code int} value.
631      * @see     java.lang.Integer#MAX_VALUE
632      * @see     java.lang.Integer#MIN_VALUE
633      */
round(float a)634     public static int round(float a) {
635         return Math.round(a);
636     }
637 
638     /**
639      * Returns the closest {@code long} to the argument, with ties
640      * rounding to positive infinity.
641      *
642      * <p>Special cases:
643      * <ul><li>If the argument is NaN, the result is 0.
644      * <li>If the argument is negative infinity or any value less than or
645      * equal to the value of {@code Long.MIN_VALUE}, the result is
646      * equal to the value of {@code Long.MIN_VALUE}.
647      * <li>If the argument is positive infinity or any value greater than or
648      * equal to the value of {@code Long.MAX_VALUE}, the result is
649      * equal to the value of {@code Long.MAX_VALUE}.</ul>
650      *
651      * @param   a  a floating-point value to be rounded to a
652      *          {@code long}.
653      * @return  the value of the argument rounded to the nearest
654      *          {@code long} value.
655      * @see     java.lang.Long#MAX_VALUE
656      * @see     java.lang.Long#MIN_VALUE
657      */
round(double a)658     public static long round(double a) {
659         return Math.round(a);
660     }
661 
662     private static Random randomNumberGenerator;
663 
initRNG()664     private static synchronized Random initRNG() {
665         Random rnd = randomNumberGenerator;
666         return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
667     }
668 
669     /**
670      * Returns a {@code double} value with a positive sign, greater
671      * than or equal to {@code 0.0} and less than {@code 1.0}.
672      * Returned values are chosen pseudorandomly with (approximately)
673      * uniform distribution from that range.
674      *
675      * <p>When this method is first called, it creates a single new
676      * pseudorandom-number generator, exactly as if by the expression
677      *
678      * <blockquote>{@code new java.util.Random()}</blockquote>
679      *
680      * This new pseudorandom-number generator is used thereafter for
681      * all calls to this method and is used nowhere else.
682      *
683      * <p>This method is properly synchronized to allow correct use by
684      * more than one thread. However, if many threads need to generate
685      * pseudorandom numbers at a great rate, it may reduce contention
686      * for each thread to have its own pseudorandom number generator.
687      *
688      * @return  a pseudorandom {@code double} greater than or equal
689      * to {@code 0.0} and less than {@code 1.0}.
690      * @see Random#nextDouble()
691      */
random()692     public static double random() {
693         Random rnd = randomNumberGenerator;
694         if (rnd == null) rnd = initRNG();
695         return rnd.nextDouble();
696     }
697 
698     /**
699      * Returns the sum of its arguments,
700      * throwing an exception if the result overflows an {@code int}.
701      *
702      * @param x the first value
703      * @param y the second value
704      * @return the result
705      * @throws ArithmeticException if the result overflows an int
706      * @see Math#addExact(int,int)
707      * @since 1.8
708      */
addExact(int x, int y)709     public static int addExact(int x, int y) {
710         return Math.addExact(x, y);
711     }
712 
713     /**
714      * Returns the sum of its arguments,
715      * throwing an exception if the result overflows a {@code long}.
716      *
717      * @param x the first value
718      * @param y the second value
719      * @return the result
720      * @throws ArithmeticException if the result overflows a long
721      * @see Math#addExact(long,long)
722      * @since 1.8
723      */
addExact(long x, long y)724     public static long addExact(long x, long y) {
725         return Math.addExact(x, y);
726     }
727 
728     /**
729      * Returns the difference of the arguments,
730      * throwing an exception if the result overflows an {@code int}.
731      *
732      * @param x the first value
733      * @param y the second value to subtract from the first
734      * @return the result
735      * @throws ArithmeticException if the result overflows an int
736      * @see Math#subtractExact(int,int)
737      * @since 1.8
738      */
subtractExact(int x, int y)739     public static int subtractExact(int x, int y) {
740         return Math.subtractExact(x, y);
741     }
742 
743     /**
744      * Returns the difference of the arguments,
745      * throwing an exception if the result overflows a {@code long}.
746      *
747      * @param x the first value
748      * @param y the second value to subtract from the first
749      * @return the result
750      * @throws ArithmeticException if the result overflows a long
751      * @see Math#subtractExact(long,long)
752      * @since 1.8
753      */
subtractExact(long x, long y)754     public static long subtractExact(long x, long y) {
755         return Math.subtractExact(x, y);
756     }
757 
758     /**
759      * Returns the product of the arguments,
760      * throwing an exception if the result overflows an {@code int}.
761      *
762      * @param x the first value
763      * @param y the second value
764      * @return the result
765      * @throws ArithmeticException if the result overflows an int
766      * @see Math#multiplyExact(int,int)
767      * @since 1.8
768      */
multiplyExact(int x, int y)769     public static int multiplyExact(int x, int y) {
770         return Math.multiplyExact(x, y);
771     }
772 
773     /**
774      * Returns the product of the arguments,
775      * throwing an exception if the result overflows a {@code long}.
776      *
777      * @param x the first value
778      * @param y the second value
779      * @return the result
780      * @throws ArithmeticException if the result overflows a long
781      * @see Math#multiplyExact(long,long)
782      * @since 1.8
783      */
multiplyExact(long x, long y)784     public static long multiplyExact(long x, long y) {
785         return Math.multiplyExact(x, y);
786     }
787 
788     /**
789      * Returns the value of the {@code long} argument;
790      * throwing an exception if the value overflows an {@code int}.
791      *
792      * @param value the long value
793      * @return the argument as an int
794      * @throws ArithmeticException if the {@code argument} overflows an int
795      * @see Math#toIntExact(long)
796      * @since 1.8
797      */
toIntExact(long value)798     public static int toIntExact(long value) {
799         return Math.toIntExact(value);
800     }
801 
802     /**
803      * Returns the largest (closest to positive infinity)
804      * {@code int} value that is less than or equal to the algebraic quotient.
805      * There is one special case, if the dividend is the
806      * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1},
807      * then integer overflow occurs and
808      * the result is equal to the {@code Integer.MIN_VALUE}.
809      * <p>
810      * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and
811      * a comparison to the integer division {@code /} operator.
812      *
813      * @param x the dividend
814      * @param y the divisor
815      * @return the largest (closest to positive infinity)
816      * {@code int} value that is less than or equal to the algebraic quotient.
817      * @throws ArithmeticException if the divisor {@code y} is zero
818      * @see Math#floorDiv(int, int)
819      * @see Math#floor(double)
820      * @since 1.8
821      */
floorDiv(int x, int y)822     public static int floorDiv(int x, int y) {
823         return Math.floorDiv(x, y);
824     }
825 
826     /**
827      * Returns the largest (closest to positive infinity)
828      * {@code long} value that is less than or equal to the algebraic quotient.
829      * There is one special case, if the dividend is the
830      * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
831      * then integer overflow occurs and
832      * the result is equal to the {@code Long.MIN_VALUE}.
833      * <p>
834      * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and
835      * a comparison to the integer division {@code /} operator.
836      *
837      * @param x the dividend
838      * @param y the divisor
839      * @return the largest (closest to positive infinity)
840      * {@code long} value that is less than or equal to the algebraic quotient.
841      * @throws ArithmeticException if the divisor {@code y} is zero
842      * @see Math#floorDiv(long, long)
843      * @see Math#floor(double)
844      * @since 1.8
845      */
floorDiv(long x, long y)846     public static long floorDiv(long x, long y) {
847         return Math.floorDiv(x, y);
848     }
849 
850     /**
851      * Returns the floor modulus of the {@code int} arguments.
852      * <p>
853      * The floor modulus is {@code x - (floorDiv(x, y) * y)},
854      * has the same sign as the divisor {@code y}, and
855      * is in the range of {@code -abs(y) < r < +abs(y)}.
856      * <p>
857      * The relationship between {@code floorDiv} and {@code floorMod} is such that:
858      * <ul>
859      *   <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
860      * </ul>
861      * <p>
862      * See {@link Math#floorMod(int, int) Math.floorMod} for examples and
863      * a comparison to the {@code %} operator.
864      *
865      * @param x the dividend
866      * @param y the divisor
867      * @return the floor modulus {@code x - (floorDiv(x, y) * y)}
868      * @throws ArithmeticException if the divisor {@code y} is zero
869      * @see Math#floorMod(int, int)
870      * @see StrictMath#floorDiv(int, int)
871      * @since 1.8
872      */
floorMod(int x, int y)873     public static int floorMod(int x, int y) {
874         return Math.floorMod(x , y);
875     }
876     /**
877      * Returns the floor modulus of the {@code long} arguments.
878      * <p>
879      * The floor modulus is {@code x - (floorDiv(x, y) * y)},
880      * has the same sign as the divisor {@code y}, and
881      * is in the range of {@code -abs(y) < r < +abs(y)}.
882      * <p>
883      * The relationship between {@code floorDiv} and {@code floorMod} is such that:
884      * <ul>
885      *   <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
886      * </ul>
887      * <p>
888      * See {@link Math#floorMod(int, int) Math.floorMod} for examples and
889      * a comparison to the {@code %} operator.
890      *
891      * @param x the dividend
892      * @param y the divisor
893      * @return the floor modulus {@code x - (floorDiv(x, y) * y)}
894      * @throws ArithmeticException if the divisor {@code y} is zero
895      * @see Math#floorMod(long, long)
896      * @see StrictMath#floorDiv(long, long)
897      * @since 1.8
898      */
floorMod(long x, long y)899     public static long floorMod(long x, long y) {
900         return Math.floorMod(x, y);
901     }
902 
903     /**
904      * Returns the absolute value of an {@code int} value..
905      * If the argument is not negative, the argument is returned.
906      * If the argument is negative, the negation of the argument is returned.
907      *
908      * <p>Note that if the argument is equal to the value of
909      * {@link Integer#MIN_VALUE}, the most negative representable
910      * {@code int} value, the result is that same value, which is
911      * negative.
912      *
913      * @param   a   the  argument whose absolute value is to be determined.
914      * @return  the absolute value of the argument.
915      */
abs(int a)916     public static int abs(int a) {
917         return (a < 0) ? -a : a;
918     }
919 
920     /**
921      * Returns the absolute value of a {@code long} value.
922      * If the argument is not negative, the argument is returned.
923      * If the argument is negative, the negation of the argument is returned.
924      *
925      * <p>Note that if the argument is equal to the value of
926      * {@link Long#MIN_VALUE}, the most negative representable
927      * {@code long} value, the result is that same value, which
928      * is negative.
929      *
930      * @param   a   the  argument whose absolute value is to be determined.
931      * @return  the absolute value of the argument.
932      */
abs(long a)933     public static long abs(long a) {
934         return (a < 0) ? -a : a;
935     }
936 
937     /**
938      * Returns the absolute value of a {@code float} value.
939      * If the argument is not negative, the argument is returned.
940      * If the argument is negative, the negation of the argument is returned.
941      * Special cases:
942      * <ul><li>If the argument is positive zero or negative zero, the
943      * result is positive zero.
944      * <li>If the argument is infinite, the result is positive infinity.
945      * <li>If the argument is NaN, the result is NaN.</ul>
946      * In other words, the result is the same as the value of the expression:
947      * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))}
948      *
949      * @param   a   the argument whose absolute value is to be determined
950      * @return  the absolute value of the argument.
951      */
abs(float a)952     public static float abs(float a) {
953         return (a <= 0.0F) ? 0.0F - a : a;
954     }
955 
956     /**
957      * Returns the absolute value of a {@code double} value.
958      * If the argument is not negative, the argument is returned.
959      * If the argument is negative, the negation of the argument is returned.
960      * Special cases:
961      * <ul><li>If the argument is positive zero or negative zero, the result
962      * is positive zero.
963      * <li>If the argument is infinite, the result is positive infinity.
964      * <li>If the argument is NaN, the result is NaN.</ul>
965      * In other words, the result is the same as the value of the expression:
966      * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)}
967      *
968      * @param   a   the argument whose absolute value is to be determined
969      * @return  the absolute value of the argument.
970      */
abs(double a)971     public static double abs(double a) {
972         return (a <= 0.0D) ? 0.0D - a : a;
973     }
974 
975     /**
976      * Returns the greater of two {@code int} values. That is, the
977      * result is the argument closer to the value of
978      * {@link Integer#MAX_VALUE}. If the arguments have the same value,
979      * the result is that same value.
980      *
981      * @param   a   an argument.
982      * @param   b   another argument.
983      * @return  the larger of {@code a} and {@code b}.
984      */
max(int a, int b)985     public static int max(int a, int b) {
986         return (a >= b) ? a : b;
987     }
988 
989     /**
990      * Returns the greater of two {@code long} values. That is, the
991      * result is the argument closer to the value of
992      * {@link Long#MAX_VALUE}. If the arguments have the same value,
993      * the result is that same value.
994      *
995      * @param   a   an argument.
996      * @param   b   another argument.
997      * @return  the larger of {@code a} and {@code b}.
998         */
max(long a, long b)999     public static long max(long a, long b) {
1000         return (a >= b) ? a : b;
1001     }
1002 
1003     // Use raw bit-wise conversions on guaranteed non-NaN arguments.
1004     private static long negativeZeroFloatBits  = Float.floatToRawIntBits(-0.0f);
1005     private static long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d);
1006 
1007     /**
1008      * Returns the greater of two {@code float} values.  That is,
1009      * the result is the argument closer to positive infinity. If the
1010      * arguments have the same value, the result is that same
1011      * value. If either value is NaN, then the result is NaN.  Unlike
1012      * the numerical comparison operators, this method considers
1013      * negative zero to be strictly smaller than positive zero. If one
1014      * argument is positive zero and the other negative zero, the
1015      * result is positive zero.
1016      *
1017      * @param   a   an argument.
1018      * @param   b   another argument.
1019      * @return  the larger of {@code a} and {@code b}.
1020      */
max(float a, float b)1021     public static float max(float a, float b) {
1022         if (a != a)
1023             return a;   // a is NaN
1024         if ((a == 0.0f) &&
1025             (b == 0.0f) &&
1026             (Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
1027             // Raw conversion ok since NaN can't map to -0.0.
1028             return b;
1029         }
1030         return (a >= b) ? a : b;
1031     }
1032 
1033     /**
1034      * Returns the greater of two {@code double} values.  That
1035      * is, the result is the argument closer to positive infinity. If
1036      * the arguments have the same value, the result is that same
1037      * value. If either value is NaN, then the result is NaN.  Unlike
1038      * the numerical comparison operators, this method considers
1039      * negative zero to be strictly smaller than positive zero. If one
1040      * argument is positive zero and the other negative zero, the
1041      * result is positive zero.
1042      *
1043      * @param   a   an argument.
1044      * @param   b   another argument.
1045      * @return  the larger of {@code a} and {@code b}.
1046      */
max(double a, double b)1047     public static double max(double a, double b) {
1048         if (a != a)
1049             return a;   // a is NaN
1050         if ((a == 0.0d) &&
1051             (b == 0.0d) &&
1052             (Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
1053             // Raw conversion ok since NaN can't map to -0.0.
1054             return b;
1055         }
1056         return (a >= b) ? a : b;
1057     }
1058 
1059     /**
1060      * Returns the smaller of two {@code int} values. That is,
1061      * the result the argument closer to the value of
1062      * {@link Integer#MIN_VALUE}.  If the arguments have the same
1063      * value, the result is that same value.
1064      *
1065      * @param   a   an argument.
1066      * @param   b   another argument.
1067      * @return  the smaller of {@code a} and {@code b}.
1068      */
min(int a, int b)1069     public static int min(int a, int b) {
1070         return (a <= b) ? a : b;
1071     }
1072 
1073     /**
1074      * Returns the smaller of two {@code long} values. That is,
1075      * the result is the argument closer to the value of
1076      * {@link Long#MIN_VALUE}. If the arguments have the same
1077      * value, the result is that same value.
1078      *
1079      * @param   a   an argument.
1080      * @param   b   another argument.
1081      * @return  the smaller of {@code a} and {@code b}.
1082      */
min(long a, long b)1083     public static long min(long a, long b) {
1084         return (a <= b) ? a : b;
1085     }
1086 
1087     /**
1088      * Returns the smaller of two {@code float} values.  That is,
1089      * the result is the value closer to negative infinity. If the
1090      * arguments have the same value, the result is that same
1091      * value. If either value is NaN, then the result is NaN.  Unlike
1092      * the numerical comparison operators, this method considers
1093      * negative zero to be strictly smaller than positive zero.  If
1094      * one argument is positive zero and the other is negative zero,
1095      * the result is negative zero.
1096      *
1097      * @param   a   an argument.
1098      * @param   b   another argument.
1099      * @return  the smaller of {@code a} and {@code b.}
1100      */
min(float a, float b)1101     public static float min(float a, float b) {
1102         if (a != a)
1103             return a;   // a is NaN
1104         if ((a == 0.0f) &&
1105             (b == 0.0f) &&
1106             (Float.floatToRawIntBits(b) == negativeZeroFloatBits)) {
1107             // Raw conversion ok since NaN can't map to -0.0.
1108             return b;
1109         }
1110         return (a <= b) ? a : b;
1111     }
1112 
1113     /**
1114      * Returns the smaller of two {@code double} values.  That
1115      * is, the result is the value closer to negative infinity. If the
1116      * arguments have the same value, the result is that same
1117      * value. If either value is NaN, then the result is NaN.  Unlike
1118      * the numerical comparison operators, this method considers
1119      * negative zero to be strictly smaller than positive zero. If one
1120      * argument is positive zero and the other is negative zero, the
1121      * result is negative zero.
1122      *
1123      * @param   a   an argument.
1124      * @param   b   another argument.
1125      * @return  the smaller of {@code a} and {@code b}.
1126      */
min(double a, double b)1127     public static double min(double a, double b) {
1128         if (a != a)
1129             return a;   // a is NaN
1130         if ((a == 0.0d) &&
1131             (b == 0.0d) &&
1132             (Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) {
1133             // Raw conversion ok since NaN can't map to -0.0.
1134             return b;
1135         }
1136         return (a <= b) ? a : b;
1137     }
1138 
1139     /**
1140      * Returns the size of an ulp of the argument.  An ulp of a
1141      * {@code double} value is the positive distance between this
1142      * floating-point value and the {@code double} value next
1143      * larger in magnitude.  Note that for non-NaN <i>x</i>,
1144      * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
1145      *
1146      * <p>Special Cases:
1147      * <ul>
1148      * <li> If the argument is NaN, then the result is NaN.
1149      * <li> If the argument is positive or negative infinity, then the
1150      * result is positive infinity.
1151      * <li> If the argument is positive or negative zero, then the result is
1152      * {@code Double.MIN_VALUE}.
1153      * <li> If the argument is &plusmn;{@code Double.MAX_VALUE}, then
1154      * the result is equal to 2<sup>971</sup>.
1155      * </ul>
1156      *
1157      * @param d the floating-point value whose ulp is to be returned
1158      * @return the size of an ulp of the argument
1159      * @author Joseph D. Darcy
1160      * @since 1.5
1161      */
ulp(double d)1162     public static double ulp(double d) {
1163         return sun.misc.FpUtils.ulp(d);
1164     }
1165 
1166     /**
1167      * Returns the size of an ulp of the argument.  An ulp of a
1168      * {@code float} value is the positive distance between this
1169      * floating-point value and the {@code float} value next
1170      * larger in magnitude.  Note that for non-NaN <i>x</i>,
1171      * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
1172      *
1173      * <p>Special Cases:
1174      * <ul>
1175      * <li> If the argument is NaN, then the result is NaN.
1176      * <li> If the argument is positive or negative infinity, then the
1177      * result is positive infinity.
1178      * <li> If the argument is positive or negative zero, then the result is
1179      * {@code Float.MIN_VALUE}.
1180      * <li> If the argument is &plusmn;{@code Float.MAX_VALUE}, then
1181      * the result is equal to 2<sup>104</sup>.
1182      * </ul>
1183      *
1184      * @param f the floating-point value whose ulp is to be returned
1185      * @return the size of an ulp of the argument
1186      * @author Joseph D. Darcy
1187      * @since 1.5
1188      */
ulp(float f)1189     public static float ulp(float f) {
1190         return sun.misc.FpUtils.ulp(f);
1191     }
1192 
1193     /**
1194      * Returns the signum function of the argument; zero if the argument
1195      * is zero, 1.0 if the argument is greater than zero, -1.0 if the
1196      * argument is less than zero.
1197      *
1198      * <p>Special Cases:
1199      * <ul>
1200      * <li> If the argument is NaN, then the result is NaN.
1201      * <li> If the argument is positive zero or negative zero, then the
1202      *      result is the same as the argument.
1203      * </ul>
1204      *
1205      * @param d the floating-point value whose signum is to be returned
1206      * @return the signum function of the argument
1207      * @author Joseph D. Darcy
1208      * @since 1.5
1209      */
signum(double d)1210     public static double signum(double d) {
1211         return sun.misc.FpUtils.signum(d);
1212     }
1213 
1214     /**
1215      * Returns the signum function of the argument; zero if the argument
1216      * is zero, 1.0f if the argument is greater than zero, -1.0f if the
1217      * argument is less than zero.
1218      *
1219      * <p>Special Cases:
1220      * <ul>
1221      * <li> If the argument is NaN, then the result is NaN.
1222      * <li> If the argument is positive zero or negative zero, then the
1223      *      result is the same as the argument.
1224      * </ul>
1225      *
1226      * @param f the floating-point value whose signum is to be returned
1227      * @return the signum function of the argument
1228      * @author Joseph D. Darcy
1229      * @since 1.5
1230      */
signum(float f)1231     public static float signum(float f) {
1232         return sun.misc.FpUtils.signum(f);
1233     }
1234 
1235     /**
1236      * Returns the hyperbolic sine of a {@code double} value.
1237      * The hyperbolic sine of <i>x</i> is defined to be
1238      * (<i>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></i>)/2
1239      * where <i>e</i> is {@linkplain Math#E Euler's number}.
1240      *
1241      * <p>Special cases:
1242      * <ul>
1243      *
1244      * <li>If the argument is NaN, then the result is NaN.
1245      *
1246      * <li>If the argument is infinite, then the result is an infinity
1247      * with the same sign as the argument.
1248      *
1249      * <li>If the argument is zero, then the result is a zero with the
1250      * same sign as the argument.
1251      *
1252      * </ul>
1253      *
1254      * @param   x The number whose hyperbolic sine is to be returned.
1255      * @return  The hyperbolic sine of {@code x}.
1256      * @since 1.5
1257      */
sinh(double x)1258     public static native double sinh(double x);
1259 
1260     /**
1261      * Returns the hyperbolic cosine of a {@code double} value.
1262      * The hyperbolic cosine of <i>x</i> is defined to be
1263      * (<i>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></i>)/2
1264      * where <i>e</i> is {@linkplain Math#E Euler's number}.
1265      *
1266      * <p>Special cases:
1267      * <ul>
1268      *
1269      * <li>If the argument is NaN, then the result is NaN.
1270      *
1271      * <li>If the argument is infinite, then the result is positive
1272      * infinity.
1273      *
1274      * <li>If the argument is zero, then the result is {@code 1.0}.
1275      *
1276      * </ul>
1277      *
1278      * @param   x The number whose hyperbolic cosine is to be returned.
1279      * @return  The hyperbolic cosine of {@code x}.
1280      * @since 1.5
1281      */
cosh(double x)1282     public static native double cosh(double x);
1283 
1284     /**
1285      * Returns the hyperbolic tangent of a {@code double} value.
1286      * The hyperbolic tangent of <i>x</i> is defined to be
1287      * (<i>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></i>)/(<i>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></i>),
1288      * in other words, {@linkplain Math#sinh
1289      * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}.  Note
1290      * that the absolute value of the exact tanh is always less than
1291      * 1.
1292      *
1293      * <p>Special cases:
1294      * <ul>
1295      *
1296      * <li>If the argument is NaN, then the result is NaN.
1297      *
1298      * <li>If the argument is zero, then the result is a zero with the
1299      * same sign as the argument.
1300      *
1301      * <li>If the argument is positive infinity, then the result is
1302      * {@code +1.0}.
1303      *
1304      * <li>If the argument is negative infinity, then the result is
1305      * {@code -1.0}.
1306      *
1307      * </ul>
1308      *
1309      * @param   x The number whose hyperbolic tangent is to be returned.
1310      * @return  The hyperbolic tangent of {@code x}.
1311      * @since 1.5
1312      */
tanh(double x)1313     public static native double tanh(double x);
1314 
1315     /**
1316      * Returns sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
1317      * without intermediate overflow or underflow.
1318      *
1319      * <p>Special cases:
1320      * <ul>
1321      *
1322      * <li> If either argument is infinite, then the result
1323      * is positive infinity.
1324      *
1325      * <li> If either argument is NaN and neither argument is infinite,
1326      * then the result is NaN.
1327      *
1328      * </ul>
1329      *
1330      * @param x a value
1331      * @param y a value
1332      * @return sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
1333      * without intermediate overflow or underflow
1334      * @since 1.5
1335      */
hypot(double x, double y)1336     public static native double hypot(double x, double y);
1337 
1338     /**
1339      * Returns <i>e</i><sup>x</sup>&nbsp;-1.  Note that for values of
1340      * <i>x</i> near 0, the exact sum of
1341      * {@code expm1(x)}&nbsp;+&nbsp;1 is much closer to the true
1342      * result of <i>e</i><sup>x</sup> than {@code exp(x)}.
1343      *
1344      * <p>Special cases:
1345      * <ul>
1346      * <li>If the argument is NaN, the result is NaN.
1347      *
1348      * <li>If the argument is positive infinity, then the result is
1349      * positive infinity.
1350      *
1351      * <li>If the argument is negative infinity, then the result is
1352      * -1.0.
1353      *
1354      * <li>If the argument is zero, then the result is a zero with the
1355      * same sign as the argument.
1356      *
1357      * </ul>
1358      *
1359      * @param   x   the exponent to raise <i>e</i> to in the computation of
1360      *              <i>e</i><sup>{@code x}</sup>&nbsp;-1.
1361      * @return  the value <i>e</i><sup>{@code x}</sup>&nbsp;-&nbsp;1.
1362      * @since 1.5
1363      */
expm1(double x)1364     public static native double expm1(double x);
1365 
1366     /**
1367      * Returns the natural logarithm of the sum of the argument and 1.
1368      * Note that for small values {@code x}, the result of
1369      * {@code log1p(x)} is much closer to the true result of ln(1
1370      * + {@code x}) than the floating-point evaluation of
1371      * {@code log(1.0+x)}.
1372      *
1373      * <p>Special cases:
1374      * <ul>
1375      *
1376      * <li>If the argument is NaN or less than -1, then the result is
1377      * NaN.
1378      *
1379      * <li>If the argument is positive infinity, then the result is
1380      * positive infinity.
1381      *
1382      * <li>If the argument is negative one, then the result is
1383      * negative infinity.
1384      *
1385      * <li>If the argument is zero, then the result is a zero with the
1386      * same sign as the argument.
1387      *
1388      * </ul>
1389      *
1390      * @param   x   a value
1391      * @return the value ln({@code x}&nbsp;+&nbsp;1), the natural
1392      * log of {@code x}&nbsp;+&nbsp;1
1393      * @since 1.5
1394      */
log1p(double x)1395     public static native double log1p(double x);
1396 
1397     /**
1398      * Returns the first floating-point argument with the sign of the
1399      * second floating-point argument.  For this method, a NaN
1400      * {@code sign} argument is always treated as if it were
1401      * positive.
1402      *
1403      * @param magnitude  the parameter providing the magnitude of the result
1404      * @param sign   the parameter providing the sign of the result
1405      * @return a value with the magnitude of {@code magnitude}
1406      * and the sign of {@code sign}.
1407      * @since 1.6
1408      */
copySign(double magnitude, double sign)1409     public static double copySign(double magnitude, double sign) {
1410         return sun.misc.FpUtils.copySign(magnitude, sign);
1411     }
1412 
1413     /**
1414      * Returns the first floating-point argument with the sign of the
1415      * second floating-point argument.  For this method, a NaN
1416      * {@code sign} argument is always treated as if it were
1417      * positive.
1418      *
1419      * @param magnitude  the parameter providing the magnitude of the result
1420      * @param sign   the parameter providing the sign of the result
1421      * @return a value with the magnitude of {@code magnitude}
1422      * and the sign of {@code sign}.
1423      * @since 1.6
1424      */
copySign(float magnitude, float sign)1425     public static float copySign(float magnitude, float sign) {
1426         return sun.misc.FpUtils.copySign(magnitude, sign);
1427     }
1428     /**
1429      * Returns the unbiased exponent used in the representation of a
1430      * {@code float}.  Special cases:
1431      *
1432      * <ul>
1433      * <li>If the argument is NaN or infinite, then the result is
1434      * {@link Float#MAX_EXPONENT} + 1.
1435      * <li>If the argument is zero or subnormal, then the result is
1436      * {@link Float#MIN_EXPONENT} -1.
1437      * </ul>
1438      * @param f a {@code float} value
1439      * @since 1.6
1440      */
getExponent(float f)1441     public static int getExponent(float f) {
1442         return sun.misc.FpUtils.getExponent(f);
1443     }
1444 
1445     /**
1446      * Returns the unbiased exponent used in the representation of a
1447      * {@code double}.  Special cases:
1448      *
1449      * <ul>
1450      * <li>If the argument is NaN or infinite, then the result is
1451      * {@link Double#MAX_EXPONENT} + 1.
1452      * <li>If the argument is zero or subnormal, then the result is
1453      * {@link Double#MIN_EXPONENT} -1.
1454      * </ul>
1455      * @param d a {@code double} value
1456      * @since 1.6
1457      */
getExponent(double d)1458     public static int getExponent(double d) {
1459         return sun.misc.FpUtils.getExponent(d);
1460     }
1461 
1462     /**
1463      * Returns the floating-point number adjacent to the first
1464      * argument in the direction of the second argument.  If both
1465      * arguments compare as equal the second argument is returned.
1466      *
1467      * <p>Special cases:
1468      * <ul>
1469      * <li> If either argument is a NaN, then NaN is returned.
1470      *
1471      * <li> If both arguments are signed zeros, {@code direction}
1472      * is returned unchanged (as implied by the requirement of
1473      * returning the second argument if the arguments compare as
1474      * equal).
1475      *
1476      * <li> If {@code start} is
1477      * &plusmn;{@link Double#MIN_VALUE} and {@code direction}
1478      * has a value such that the result should have a smaller
1479      * magnitude, then a zero with the same sign as {@code start}
1480      * is returned.
1481      *
1482      * <li> If {@code start} is infinite and
1483      * {@code direction} has a value such that the result should
1484      * have a smaller magnitude, {@link Double#MAX_VALUE} with the
1485      * same sign as {@code start} is returned.
1486      *
1487      * <li> If {@code start} is equal to &plusmn;
1488      * {@link Double#MAX_VALUE} and {@code direction} has a
1489      * value such that the result should have a larger magnitude, an
1490      * infinity with same sign as {@code start} is returned.
1491      * </ul>
1492      *
1493      * @param start  starting floating-point value
1494      * @param direction value indicating which of
1495      * {@code start}'s neighbors or {@code start} should
1496      * be returned
1497      * @return The floating-point number adjacent to {@code start} in the
1498      * direction of {@code direction}.
1499      * @since 1.6
1500      */
nextAfter(double start, double direction)1501     public static double nextAfter(double start, double direction) {
1502         return sun.misc.FpUtils.nextAfter(start, direction);
1503     }
1504 
1505     /**
1506      * Returns the floating-point number adjacent to the first
1507      * argument in the direction of the second argument.  If both
1508      * arguments compare as equal a value equivalent to the second argument
1509      * is returned.
1510      *
1511      * <p>Special cases:
1512      * <ul>
1513      * <li> If either argument is a NaN, then NaN is returned.
1514      *
1515      * <li> If both arguments are signed zeros, a value equivalent
1516      * to {@code direction} is returned.
1517      *
1518      * <li> If {@code start} is
1519      * &plusmn;{@link Float#MIN_VALUE} and {@code direction}
1520      * has a value such that the result should have a smaller
1521      * magnitude, then a zero with the same sign as {@code start}
1522      * is returned.
1523      *
1524      * <li> If {@code start} is infinite and
1525      * {@code direction} has a value such that the result should
1526      * have a smaller magnitude, {@link Float#MAX_VALUE} with the
1527      * same sign as {@code start} is returned.
1528      *
1529      * <li> If {@code start} is equal to &plusmn;
1530      * {@link Float#MAX_VALUE} and {@code direction} has a
1531      * value such that the result should have a larger magnitude, an
1532      * infinity with same sign as {@code start} is returned.
1533      * </ul>
1534      *
1535      * @param start  starting floating-point value
1536      * @param direction value indicating which of
1537      * {@code start}'s neighbors or {@code start} should
1538      * be returned
1539      * @return The floating-point number adjacent to {@code start} in the
1540      * direction of {@code direction}.
1541      * @since 1.6
1542      */
nextAfter(float start, double direction)1543     public static float nextAfter(float start, double direction) {
1544         return sun.misc.FpUtils.nextAfter(start, direction);
1545     }
1546 
1547     /**
1548      * Returns the floating-point value adjacent to {@code d} in
1549      * the direction of positive infinity.  This method is
1550      * semantically equivalent to {@code nextAfter(d,
1551      * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
1552      * implementation may run faster than its equivalent
1553      * {@code nextAfter} call.
1554      *
1555      * <p>Special Cases:
1556      * <ul>
1557      * <li> If the argument is NaN, the result is NaN.
1558      *
1559      * <li> If the argument is positive infinity, the result is
1560      * positive infinity.
1561      *
1562      * <li> If the argument is zero, the result is
1563      * {@link Double#MIN_VALUE}
1564      *
1565      * </ul>
1566      *
1567      * @param d starting floating-point value
1568      * @return The adjacent floating-point value closer to positive
1569      * infinity.
1570      * @since 1.6
1571      */
nextUp(double d)1572     public static double nextUp(double d) {
1573         return sun.misc.FpUtils.nextUp(d);
1574     }
1575 
1576     /**
1577      * Returns the floating-point value adjacent to {@code f} in
1578      * the direction of positive infinity.  This method is
1579      * semantically equivalent to {@code nextAfter(f,
1580      * Float.POSITIVE_INFINITY)}; however, a {@code nextUp}
1581      * implementation may run faster than its equivalent
1582      * {@code nextAfter} call.
1583      *
1584      * <p>Special Cases:
1585      * <ul>
1586      * <li> If the argument is NaN, the result is NaN.
1587      *
1588      * <li> If the argument is positive infinity, the result is
1589      * positive infinity.
1590      *
1591      * <li> If the argument is zero, the result is
1592      * {@link Float#MIN_VALUE}
1593      *
1594      * </ul>
1595      *
1596      * @param f starting floating-point value
1597      * @return The adjacent floating-point value closer to positive
1598      * infinity.
1599      * @since 1.6
1600      */
nextUp(float f)1601     public static float nextUp(float f) {
1602         return sun.misc.FpUtils.nextUp(f);
1603     }
1604 
1605     /**
1606      * Returns the floating-point value adjacent to {@code d} in
1607      * the direction of negative infinity.  This method is
1608      * semantically equivalent to {@code nextAfter(d,
1609      * Double.NEGATIVE_INFINITY)}; however, a
1610      * {@code nextDown} implementation may run faster than its
1611      * equivalent {@code nextAfter} call.
1612      *
1613      * <p>Special Cases:
1614      * <ul>
1615      * <li> If the argument is NaN, the result is NaN.
1616      *
1617      * <li> If the argument is negative infinity, the result is
1618      * negative infinity.
1619      *
1620      * <li> If the argument is zero, the result is
1621      * {@code -Double.MIN_VALUE}
1622      *
1623      * </ul>
1624      *
1625      * @param d  starting floating-point value
1626      * @return The adjacent floating-point value closer to negative
1627      * infinity.
1628      * @since 1.8
1629      */
nextDown(double d)1630     public static double nextDown(double d) {
1631         return Math.nextDown(d);
1632     }
1633 
1634     /**
1635      * Returns the floating-point value adjacent to {@code f} in
1636      * the direction of negative infinity.  This method is
1637      * semantically equivalent to {@code nextAfter(f,
1638      * Float.NEGATIVE_INFINITY)}; however, a
1639      * {@code nextDown} implementation may run faster than its
1640      * equivalent {@code nextAfter} call.
1641      *
1642      * <p>Special Cases:
1643      * <ul>
1644      * <li> If the argument is NaN, the result is NaN.
1645      *
1646      * <li> If the argument is negative infinity, the result is
1647      * negative infinity.
1648      *
1649      * <li> If the argument is zero, the result is
1650      * {@code -Float.MIN_VALUE}
1651      *
1652      * </ul>
1653      *
1654      * @param f  starting floating-point value
1655      * @return The adjacent floating-point value closer to negative
1656      * infinity.
1657      * @since 1.8
1658      */
nextDown(float f)1659     public static float nextDown(float f) {
1660         return Math.nextDown(f);
1661     }
1662 
1663     /**
1664      * Return {@code d} &times;
1665      * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1666      * by a single correctly rounded floating-point multiply to a
1667      * member of the double value set.  See the Java
1668      * Language Specification for a discussion of floating-point
1669      * value sets.  If the exponent of the result is between {@link
1670      * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
1671      * answer is calculated exactly.  If the exponent of the result
1672      * would be larger than {@code Double.MAX_EXPONENT}, an
1673      * infinity is returned.  Note that if the result is subnormal,
1674      * precision may be lost; that is, when {@code scalb(x, n)}
1675      * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1676      * <i>x</i>.  When the result is non-NaN, the result has the same
1677      * sign as {@code d}.
1678      *
1679      * <p>Special cases:
1680      * <ul>
1681      * <li> If the first argument is NaN, NaN is returned.
1682      * <li> If the first argument is infinite, then an infinity of the
1683      * same sign is returned.
1684      * <li> If the first argument is zero, then a zero of the same
1685      * sign is returned.
1686      * </ul>
1687      *
1688      * @param d number to be scaled by a power of two.
1689      * @param scaleFactor power of 2 used to scale {@code d}
1690      * @return {@code d} &times; 2<sup>{@code scaleFactor}</sup>
1691      * @since 1.6
1692      */
scalb(double d, int scaleFactor)1693     public static double scalb(double d, int scaleFactor) {
1694         return sun.misc.FpUtils.scalb(d, scaleFactor);
1695     }
1696 
1697     /**
1698      * Return {@code f} &times;
1699      * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1700      * by a single correctly rounded floating-point multiply to a
1701      * member of the float value set.  See the Java
1702      * Language Specification for a discussion of floating-point
1703      * value sets.  If the exponent of the result is between {@link
1704      * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
1705      * answer is calculated exactly.  If the exponent of the result
1706      * would be larger than {@code Float.MAX_EXPONENT}, an
1707      * infinity is returned.  Note that if the result is subnormal,
1708      * precision may be lost; that is, when {@code scalb(x, n)}
1709      * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1710      * <i>x</i>.  When the result is non-NaN, the result has the same
1711      * sign as {@code f}.
1712      *
1713      * <p>Special cases:
1714      * <ul>
1715      * <li> If the first argument is NaN, NaN is returned.
1716      * <li> If the first argument is infinite, then an infinity of the
1717      * same sign is returned.
1718      * <li> If the first argument is zero, then a zero of the same
1719      * sign is returned.
1720      * </ul>
1721      *
1722      * @param f number to be scaled by a power of two.
1723      * @param scaleFactor power of 2 used to scale {@code f}
1724      * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
1725      * @since 1.6
1726      */
scalb(float f, int scaleFactor)1727     public static float scalb(float f, int scaleFactor) {
1728         return sun.misc.FpUtils.scalb(f, scaleFactor);
1729     }
1730 }
1731