StrictMath.java revision 55392539fea537abfb6581b474918f9d611fba27
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package java.lang;
19
20import java.util.Random;
21
22/**
23 * Class StrictMath provides basic math constants and operations such as
24 * trigonometric functions, hyperbolic functions, exponential, logarithms, etc.
25 * <p>
26 * In contrast to class {@link Math}, the methods in this class return exactly
27 * the same results on all platforms. Algorithms based on these methods thus
28 * behave the same (e.g. regarding numerical convergence) on all platforms,
29 * complying with the slogan "write once, run everywhere". On the other side,
30 * the implementation of class StrictMath may be less efficient than that of
31 * class Math, as class StrictMath cannot utilize platform specific features
32 * such as an extended precision math co-processors.
33 * <p>
34 * The methods in this class are specified using the "Freely Distributable Math
35 * Library" (fdlibm), version 5.3.
36 * <p>
37 * <a href="http://www.netlib.org/fdlibm/">http://www.netlib.org/fdlibm/</a>
38 */
39public final class StrictMath {
40
41    /**
42     * The double value closest to e, the base of the natural logarithm.
43     */
44    public final static double E = Math.E;
45
46    /**
47     * The double value closest to pi, the ratio of a circle's circumference to
48     * its diameter.
49     */
50    public final static double PI = Math.PI;
51
52    private static java.util.Random random;
53
54    /**
55     * Prevents this class from being instantiated.
56     */
57    private StrictMath() {
58    }
59
60    /**
61     * Returns the absolute value of the argument.
62     * <p>
63     * Special cases:
64     * <ul>
65     * <li>{@code abs(-0.0) = +0.0}</li>
66     * <li>{@code abs(+infinity) = +infinity}</li>
67     * <li>{@code abs(-infinity) = +infinity}</li>
68     * <li>{@code abs(NaN) = NaN}</li>
69     * </ul>
70     *
71     * @param d
72     *            the value whose absolute value has to be computed.
73     * @return the absolute value of the argument.
74     */
75    public static double abs(double d) {
76        long bits = Double.doubleToLongBits(d);
77        bits &= 0x7fffffffffffffffL;
78        return Double.longBitsToDouble(bits);
79    }
80
81    /**
82     * Returns the absolute value of the argument.
83     * <p>
84     * Special cases:
85     * <ul>
86     * <li>{@code abs(-0.0) = +0.0}</li>
87     * <li>{@code abs(+infinity) = +infinity}</li>
88     * <li>{@code abs(-infinity) = +infinity}</li>
89     * <li>{@code abs(NaN) = NaN}</li>
90     * </ul>
91     *
92     * @param f
93     *            the value whose absolute value has to be computed.
94     * @return the argument if it is positive, otherwise the negation of the
95     *         argument.
96     */
97    public static float abs(float f) {
98        int bits = Float.floatToIntBits(f);
99        bits &= 0x7fffffff;
100        return Float.intBitsToFloat(bits);
101    }
102
103    /**
104     * Returns the absolute value of the argument.
105     * <p>
106     * If the argument is {@code Integer.MIN_VALUE}, {@code Integer.MIN_VALUE}
107     * is returned.
108     *
109     * @param i
110     *            the value whose absolute value has to be computed.
111     * @return the argument if it is positive, otherwise the negation of the
112     *         argument.
113     */
114    public static int abs(int i) {
115        return i >= 0 ? i : -i;
116    }
117
118    /**
119     * Returns the absolute value of the argument.
120     * <p>
121     * If the argument is {@code Long.MIN_VALUE}, {@code Long.MIN_VALUE} is
122     * returned.
123     *
124     * @param l
125     *            the value whose absolute value has to be computed.
126     * @return the argument if it is positive, otherwise the negation of the
127     *         argument.
128     */
129    public static long abs(long l) {
130        return l >= 0 ? l : -l;
131    }
132
133    /**
134     * Returns the closest double approximation of the arc cosine of the
135     * argument within the range {@code [0..pi]}.
136     * <p>
137     * Special cases:
138     * <ul>
139     * <li>{@code acos((anything > 1) = NaN}</li>
140     * <li>{@code acos((anything < -1) = NaN}</li>
141     * <li>{@code acos(NaN) = NaN}</li>
142     * </ul>
143     *
144     * @param d
145     *            the value to compute arc cosine of.
146     * @return the arc cosine of the argument.
147     */
148    public static native double acos(double d);
149
150    /**
151     * Returns the closest double approximation of the arc sine of the argument
152     * within the range {@code [-pi/2..pi/2]}.
153     * <p>
154     * Special cases:
155     * <ul>
156     * <li>{@code asin((anything > 1)) = NaN}</li>
157     * <li>{@code asin((anything < -1)) = NaN}</li>
158     * <li>{@code asin(NaN) = NaN}</li>
159     * </ul>
160     *
161     * @param d
162     *            the value whose arc sine has to be computed.
163     * @return the arc sine of the argument.
164     */
165    public static native double asin(double d);
166
167    /**
168     * Returns the closest double approximation of the arc tangent of the
169     * argument within the range {@code [-pi/2..pi/2]}.
170     * <p>
171     * Special cases:
172     * <ul>
173     * <li>{@code atan(+0.0) = +0.0}</li>
174     * <li>{@code atan(-0.0) = -0.0}</li>
175     * <li>{@code atan(+infinity) = +pi/2}</li>
176     * <li>{@code atan(-infinity) = -pi/2}</li>
177     * <li>{@code atan(NaN) = NaN}</li>
178     * </ul>
179     *
180     * @param d
181     *            the value whose arc tangent has to be computed.
182     * @return the arc tangent of the argument.
183     */
184    public static native double atan(double d);
185
186    /**
187     * Returns the closest double approximation of the arc tangent of
188     * {@code y/x} within the range {@code [-pi..pi]}. This is the angle of the
189     * polar representation of the rectangular coordinates (x,y).
190     * <p>
191     * Special cases:
192     * <ul>
193     * <li>{@code atan2((anything), NaN ) = NaN;}</li>
194     * <li>{@code atan2(NaN , (anything) ) = NaN;}</li>
195     * <li>{@code atan2(+0.0, +(anything but NaN)) = +0.0}</li>
196     * <li>{@code atan2(-0.0, +(anything but NaN)) = -0.0}</li>
197     * <li>{@code atan2(+0.0, -(anything but NaN)) = +pi}</li>
198     * <li>{@code atan2(-0.0, -(anything but NaN)) = -pi}</li>
199     * <li>{@code atan2(+(anything but 0 and NaN), 0) = +pi/2}</li>
200     * <li>{@code atan2(-(anything but 0 and NaN), 0) = -pi/2}</li>
201     * <li>{@code atan2(+(anything but infinity and NaN), +infinity)} {@code =}
202     * {@code +0.0}</li>
203     * <li>{@code atan2(-(anything but infinity and NaN), +infinity)} {@code =}
204     * {@code -0.0}</li>
205     * <li>{@code atan2(+(anything but infinity and NaN), -infinity) = +pi}</li>
206     * <li>{@code atan2(-(anything but infinity and NaN), -infinity) = -pi}</li>
207     * <li>{@code atan2(+infinity, +infinity ) = +pi/4}</li>
208     * <li>{@code atan2(-infinity, +infinity ) = -pi/4}</li>
209     * <li>{@code atan2(+infinity, -infinity ) = +3pi/4}</li>
210     * <li>{@code atan2(-infinity, -infinity ) = -3pi/4}</li>
211     * <li>{@code atan2(+infinity, (anything but,0, NaN, and infinity))}
212     * {@code =} {@code +pi/2}</li>
213     * <li>{@code atan2(-infinity, (anything but,0, NaN, and infinity))}
214     * {@code =} {@code -pi/2}</li>
215     * </ul>
216     *
217     * @param y
218     *            the numerator of the value whose atan has to be computed.
219     * @param x
220     *            the denominator of the value whose atan has to be computed.
221     * @return the arc tangent of {@code y/x}.
222     */
223    public static native double atan2(double y, double x);
224
225    /**
226     * Returns the closest double approximation of the cube root of the
227     * argument.
228     * <p>
229     * Special cases:
230     * <ul>
231     * <li>{@code cbrt(+0.0) = +0.0}</li>
232     * <li>{@code cbrt(-0.0) = -0.0}</li>
233     * <li>{@code cbrt(+infinity) = +infinity}</li>
234     * <li>{@code cbrt(-infinity) = -infinity}</li>
235     * <li>{@code cbrt(NaN) = NaN}</li>
236     * </ul>
237     *
238     * @param d
239     *            the value whose cube root has to be computed.
240     * @return the cube root of the argument.
241     */
242    public static native double cbrt(double d);
243
244    /**
245     * Returns the double conversion of the most negative (closest to negative
246     * infinity) integer value which is greater than the argument.
247     * <p>
248     * Special cases:
249     * <ul>
250     * <li>{@code ceil(+0.0) = +0.0}</li>
251     * <li>{@code ceil(-0.0) = -0.0}</li>
252     * <li>{@code ceil((anything in range (-1,0)) = -0.0}</li>
253     * <li>{@code ceil(+infinity) = +infinity}</li>
254     * <li>{@code ceil(-infinity) = -infinity}</li>
255     * <li>{@code ceil(NaN) = NaN}</li>
256     * </ul>
257     *
258     * @param d
259     *            the value whose closest integer value has to be computed.
260     * @return the ceiling of the argument.
261     */
262    public static native double ceil(double d);
263
264
265    /**
266     * Returns the closest double approximation of the hyperbolic cosine of the
267     * argument.
268     * <p>
269     * Special cases:
270     * <ul>
271     * <li>{@code cosh(+infinity) = +infinity}</li>
272     * <li>{@code cosh(-infinity) = +infinity}</li>
273     * <li>{@code cosh(NaN) = NaN}</li>
274     * </ul>
275     *
276     * @param d
277     *            the value whose hyperbolic cosine has to be computed.
278     * @return the hyperbolic cosine of the argument.
279     */
280    public static native double cosh(double d);
281
282    /**
283     * Returns the closest double approximation of the cosine of the argument.
284     * <p>
285     * Special cases:
286     * <ul>
287     * <li>{@code cos(+infinity) = NaN}</li>
288     * <li>{@code cos(-infinity) = NaN}</li>
289     * <li>{@code cos(NaN) = NaN}</li>
290     * </ul>
291     *
292     * @param d
293     *            the angle whose cosine has to be computed, in radians.
294     * @return the cosine of the argument.
295     */
296    public static native double cos(double d);
297
298    /**
299     * Returns the closest double approximation of the raising "e" to the power
300     * of the argument.
301     * <p>
302     * Special cases:
303     * <ul>
304     * <li>{@code exp(+infinity) = +infinity}</li>
305     * <li>{@code exp(-infinity) = +0.0}</li>
306     * <li>{@code exp(NaN) = NaN}</li>
307     * </ul>
308     *
309     * @param d
310     *            the value whose exponential has to be computed.
311     * @return the exponential of the argument.
312     */
313    public static native double exp(double d);
314
315    /**
316     * Returns the closest double approximation of <i>{@code e}</i><sup>
317     * {@code d}</sup>{@code - 1}. If the argument is very close to 0, it is
318     * much more accurate to use {@code expm1(d)+1} than {@code exp(d)} (due to
319     * cancellation of significant digits).
320     * <p>
321     * Special cases:
322     * <ul>
323     * <li>{@code expm1(+0.0) = +0.0}</li>
324     * <li>{@code expm1(-0.0) = -0.0}</li>
325     * <li>{@code expm1(+infinity) = +infinity}</li>
326     * <li>{@code expm1(-infinity) = -1.0}</li>
327     * <li>{@code expm1(NaN) = NaN}</li>
328     * </ul>
329     *
330     * @param d
331     *            the value to compute the <i>{@code e}</i><sup>{@code d}</sup>
332     *            {@code - 1} of.
333     * @return the <i>{@code e}</i><sup>{@code d}</sup>{@code - 1} value
334     *         of the argument.
335     */
336    public static native double expm1(double d);
337
338    /**
339     * Returns the double conversion of the most positive (closest to
340     * positive infinity) integer value which is less than the argument.
341     * <p>
342     * Special cases:
343     * <ul>
344     * <li>{@code floor(+0.0) = +0.0}</li>
345     * <li>{@code floor(-0.0) = -0.0}</li>
346     * <li>{@code floor(+infinity) = +infinity}</li>
347     * <li>{@code floor(-infinity) = -infinity}</li>
348     * <li>{@code floor(NaN) = NaN}</li>
349     * </ul>
350     *
351     * @param d the value whose closest integer value has to be computed.
352     * @return the floor of the argument.
353     */
354    public static native double floor(double d);
355
356    /**
357     * Returns {@code sqrt(}<i>{@code x}</i><sup>{@code 2}</sup>{@code +}
358     * <i> {@code y}</i><sup>{@code 2}</sup>{@code )}. The final result is
359     * without medium underflow or overflow.
360     * <p>
361     * Special cases:
362     * <ul>
363     * <li>{@code hypot(+infinity, (anything including NaN)) = +infinity}</li>
364     * <li>{@code hypot(-infinity, (anything including NaN)) = +infinity}</li>
365     * <li>{@code hypot((anything including NaN), +infinity) = +infinity}</li>
366     * <li>{@code hypot((anything including NaN), -infinity) = +infinity}</li>
367     * <li>{@code hypot(NaN, NaN) = NaN}</li>
368     * </ul>
369     *
370     * @param x
371     *            a double number.
372     * @param y
373     *            a double number.
374     * @return the {@code sqrt(}<i>{@code x}</i><sup>{@code 2}</sup>{@code +}
375     *         <i> {@code y}</i><sup>{@code 2}</sup>{@code )} value of the
376     *         arguments.
377     */
378    public static native double hypot(double x, double y);
379
380    /**
381     * Returns the remainder of dividing {@code x} by {@code y} using the IEEE
382     * 754 rules. The result is {@code x-round(x/p)*p} where {@code round(x/p)}
383     * is the nearest integer (rounded to even), but without numerical
384     * cancellation problems.
385     * <p>
386     * Special cases:
387     * <ul>
388     * <li>{@code IEEEremainder((anything), 0) = NaN}</li>
389     * <li>{@code IEEEremainder(+infinity, (anything)) = NaN}</li>
390     * <li>{@code IEEEremainder(-infinity, (anything)) = NaN}</li>
391     * <li>{@code IEEEremainder(NaN, (anything)) = NaN}</li>
392     * <li>{@code IEEEremainder((anything), NaN) = NaN}</li>
393     * <li>{@code IEEEremainder(x, +infinity) = x } where x is anything but
394     * +/-infinity</li>
395     * <li>{@code IEEEremainder(x, -infinity) = x } where x is anything but
396     * +/-infinity</li>
397     * </ul>
398     *
399     * @param x
400     *            the numerator of the operation.
401     * @param y
402     *            the denominator of the operation.
403     * @return the IEEE754 floating point reminder of of {@code x/y}.
404     */
405    public static native double IEEEremainder(double x, double y);
406
407    /**
408     * Returns the closest double approximation of the natural logarithm of the
409     * argument.
410     * <p>
411     * Special cases:
412     * <ul>
413     * <li>{@code log(+0.0) = -infinity}</li>
414     * <li>{@code log(-0.0) = -infinity}</li>
415     * <li>{@code log((anything < 0) = NaN}</li>
416     * <li>{@code log(+infinity) = +infinity}</li>
417     * <li>{@code log(-infinity) = NaN}</li>
418     * <li>{@code log(NaN) = NaN}</li>
419     * </ul>
420     *
421     * @param d
422     *            the value whose log has to be computed.
423     * @return the natural logarithm of the argument.
424     */
425    public static native double log(double d);
426
427    /**
428     * Returns the closest double approximation of the base 10 logarithm of the
429     * argument.
430     * <p>
431     * Special cases:
432     * <ul>
433     * <li>{@code log10(+0.0) = -infinity}</li>
434     * <li>{@code log10(-0.0) = -infinity}</li>
435     * <li>{@code log10((anything < 0) = NaN}</li>
436     * <li>{@code log10(+infinity) = +infinity}</li>
437     * <li>{@code log10(-infinity) = NaN}</li>
438     * <li>{@code log10(NaN) = NaN}</li>
439     * </ul>
440     *
441     * @param d
442     *            the value whose base 10 log has to be computed.
443     * @return the natural logarithm of the argument.
444     */
445    public static native double log10(double d);
446
447    /**
448     * Returns the closest double approximation of the natural logarithm of the
449     * sum of the argument and 1. If the argument is very close to 0, it is much
450     * more accurate to use {@code log1p(d)} than {@code log(1.0+d)} (due to
451     * numerical cancellation).
452     * <p>
453     * Special cases:
454     * <ul>
455     * <li>{@code log1p(+0.0) = +0.0}</li>
456     * <li>{@code log1p(-0.0) = -0.0}</li>
457     * <li>{@code log1p((anything < 1)) = NaN}</li>
458     * <li>{@code log1p(-1.0) = -infinity}</li>
459     * <li>{@code log1p(+infinity) = +infinity}</li>
460     * <li>{@code log1p(-infinity) = NaN}</li>
461     * <li>{@code log1p(NaN) = NaN}</li>
462     * </ul>
463     *
464     * @param d
465     *            the value to compute the {@code ln(1+d)} of.
466     * @return the natural logarithm of the sum of the argument and 1.
467     */
468    public static native double log1p(double d);
469
470    /**
471     * Returns the most positive (closest to positive infinity) of the two
472     * arguments.
473     * <p>
474     * Special cases:
475     * <ul>
476     * <li>{@code max(NaN, (anything)) = NaN}</li>
477     * <li>{@code max((anything), NaN) = NaN}</li>
478     * <li>{@code max(+0.0, -0.0) = +0.0}</li>
479     * <li>{@code max(-0.0, +0.0) = +0.0}</li>
480     * </ul>
481     *
482     * @param d1
483     *            the first argument.
484     * @param d2
485     *            the second argument.
486     * @return the larger of {@code d1} and {@code d2}.
487     */
488    public static double max(double d1, double d2) {
489        if (d1 > d2)
490            return d1;
491        if (d1 < d2)
492            return d2;
493        /* if either arg is NaN, return NaN */
494        if (d1 != d2)
495            return Double.NaN;
496        /* max( +0.0,-0.0) == +0.0 */
497        if (d1 == 0.0
498                && ((Double.doubleToLongBits(d1) & Double.doubleToLongBits(d2)) & 0x8000000000000000L) == 0)
499            return 0.0;
500        return d1;
501    }
502
503    /**
504     * Returns the most positive (closest to positive infinity) of the two
505     * arguments.
506     * <p>
507     * Special cases:
508     * <ul>
509     * <li>{@code max(NaN, (anything)) = NaN}</li>
510     * <li>{@code max((anything), NaN) = NaN}</li>
511     * <li>{@code max(+0.0, -0.0) = +0.0}</li>
512     * <li>{@code max(-0.0, +0.0) = +0.0}</li>
513     * </ul>
514     *
515     * @param f1
516     *            the first argument.
517     * @param f2
518     *            the second argument.
519     * @return the larger of {@code f1} and {@code f2}.
520     */
521    public static float max(float f1, float f2) {
522        if (f1 > f2)
523            return f1;
524        if (f1 < f2)
525            return f2;
526        /* if either arg is NaN, return NaN */
527        if (f1 != f2)
528            return Float.NaN;
529        /* max( +0.0,-0.0) == +0.0 */
530        if (f1 == 0.0f
531                && ((Float.floatToIntBits(f1) & Float.floatToIntBits(f2)) & 0x80000000) == 0)
532            return 0.0f;
533        return f1;
534    }
535
536    /**
537     * Returns the most positive (closest to positive infinity) of the two
538     * arguments.
539     *
540     * @param i1
541     *            the first argument.
542     * @param i2
543     *            the second argument.
544     * @return the larger of {@code i1} and {@code i2}.
545     */
546    public static int max(int i1, int i2) {
547        return i1 > i2 ? i1 : i2;
548    }
549
550    /**
551     * Returns the most positive (closest to positive infinity) of the two
552     * arguments.
553     *
554     * @param l1
555     *            the first argument.
556     * @param l2
557     *            the second argument.
558     * @return the larger of {@code l1} and {@code l2}.
559     */
560    public static long max(long l1, long l2) {
561        return l1 > l2 ? l1 : l2;
562    }
563
564    /**
565     * Returns the most negative (closest to negative infinity) of the two
566     * arguments.
567     * <p>
568     * Special cases:
569     * <ul>
570     * <li>{@code min(NaN, (anything)) = NaN}</li>
571     * <li>{@code min((anything), NaN) = NaN}</li>
572     * <li>{@code min(+0.0, -0.0) = -0.0}</li>
573     * <li>{@code min(-0.0, +0.0) = -0.0}</li>
574     * </ul>
575     *
576     * @param d1
577     *            the first argument.
578     * @param d2
579     *            the second argument.
580     * @return the smaller of {@code d1} and {@code d2}.
581     */
582    public static double min(double d1, double d2) {
583        if (d1 > d2)
584            return d2;
585        if (d1 < d2)
586            return d1;
587        /* if either arg is NaN, return NaN */
588        if (d1 != d2)
589            return Double.NaN;
590        /* min( +0.0,-0.0) == -0.0 */
591        if (d1 == 0.0
592                && ((Double.doubleToLongBits(d1) | Double.doubleToLongBits(d2)) & 0x8000000000000000l) != 0)
593            return 0.0 * (-1.0);
594        return d1;
595    }
596
597    /**
598     * Returns the most negative (closest to negative infinity) of the two
599     * arguments.
600     * <p>
601     * Special cases:
602     * <ul>
603     * <li>{@code min(NaN, (anything)) = NaN}</li>
604     * <li>{@code min((anything), NaN) = NaN}</li>
605     * <li>{@code min(+0.0, -0.0) = -0.0}</li>
606     * <li>{@code min(-0.0, +0.0) = -0.0}</li>
607     * </ul>
608     *
609     * @param f1
610     *            the first argument.
611     * @param f2
612     *            the second argument.
613     * @return the smaller of {@code f1} and {@code f2}.
614     */
615    public static float min(float f1, float f2) {
616        if (f1 > f2)
617            return f2;
618        if (f1 < f2)
619            return f1;
620        /* if either arg is NaN, return NaN */
621        if (f1 != f2)
622            return Float.NaN;
623        /* min( +0.0,-0.0) == -0.0 */
624        if (f1 == 0.0f
625                && ((Float.floatToIntBits(f1) | Float.floatToIntBits(f2)) & 0x80000000) != 0)
626            return 0.0f * (-1.0f);
627        return f1;
628    }
629
630    /**
631     * Returns the most negative (closest to negative infinity) of the two
632     * arguments.
633     *
634     * @param i1
635     *            the first argument.
636     * @param i2
637     *            the second argument.
638     * @return the smaller of {@code i1} and {@code i2}.
639     */
640    public static int min(int i1, int i2) {
641        return i1 < i2 ? i1 : i2;
642    }
643
644    /**
645     * Returns the most negative (closest to negative infinity) of the two
646     * arguments.
647     *
648     * @param l1
649     *            the first argument.
650     * @param l2
651     *            the second argument.
652     * @return the smaller of {@code l1} and {@code l2}.
653     */
654    public static long min(long l1, long l2) {
655        return l1 < l2 ? l1 : l2;
656    }
657
658    /**
659     * Returns the closest double approximation of the result of raising
660     * {@code x} to the power of {@code y}.
661     * <p>
662     * Special cases:
663     * <ul>
664     * <li>{@code pow((anything), +0.0) = 1.0}</li>
665     * <li>{@code pow((anything), -0.0) = 1.0}</li>
666     * <li>{@code pow(x, 1.0) = x}</li>
667     * <li>{@code pow((anything), NaN) = NaN}</li>
668     * <li>{@code pow(NaN, (anything except 0)) = NaN}</li>
669     * <li>{@code pow(+/-(|x| > 1), +infinity) = +infinity}</li>
670     * <li>{@code pow(+/-(|x| > 1), -infinity) = +0.0}</li>
671     * <li>{@code pow(+/-(|x| < 1), +infinity) = +0.0}</li>
672     * <li>{@code pow(+/-(|x| < 1), -infinity) = +infinity}</li>
673     * <li>{@code pow(+/-1.0 , +infinity) = NaN}</li>
674     * <li>{@code pow(+/-1.0 , -infinity) = NaN}</li>
675     * <li>{@code pow(+0.0, (+anything except 0, NaN)) = +0.0}</li>
676     * <li>{@code pow(-0.0, (+anything except 0, NaN, odd integer)) = +0.0}</li>
677     * <li>{@code pow(+0.0, (-anything except 0, NaN)) = +infinity}</li>
678     * <li>{@code pow(-0.0, (-anything except 0, NAN, odd integer))} {@code =}
679     * {@code +infinity}</li>
680     * <li>{@code pow(-0.0, (odd integer)) = -pow( +0 , (odd integer) )}</li>
681     * <li>{@code pow(+infinity, (+anything except 0, NaN)) = +infinity}</li>
682     * <li>{@code pow(+infinity, (-anything except 0, NaN)) = +0.0}</li>
683     * <li>{@code pow(-infinity, (anything)) = -pow(0, (-anything))}</li>
684     * <li>{@code pow((-anything), (integer))} {@code =}
685     * {@code pow(-1,(integer))*pow(+anything,integer)}</li>
686     * <li>{@code pow((-anything except 0 and infinity), (non-integer))}
687     * {@code =} {@code NAN}</li>
688     * </ul>
689     *
690     * @param x
691     *            the base of the operation.
692     * @param y
693     *            the exponent of the operation.
694     * @return {@code x} to the power of {@code y}.
695     */
696    public static native double pow(double x, double y);
697
698    /**
699     * Returns a pseudo-random number between 0.0 (inclusive) and 1.0
700     * (exclusive).
701     *
702     * @return a pseudo-random number.
703     */
704    public static double random() {
705        if (random == null)
706            random = new Random();
707        return random.nextDouble();
708    }
709
710    /**
711     * Returns the double conversion of the result of rounding the argument to
712     * an integer. Tie breaks are rounded towards even.
713     * <p>
714     * Special cases:
715     * <ul>
716     * <li>{@code rint(+0.0) = +0.0}</li>
717     * <li>{@code rint(-0.0) = -0.0}</li>
718     * <li>{@code rint(+infinity) = +infinity}</li>
719     * <li>{@code rint(-infinity) = -infinity}</li>
720     * <li>{@code rint(NaN) = NaN}</li>
721     * </ul>
722     *
723     * @param d
724     *            the value to be rounded.
725     * @return the closest integer to the argument (as a double).
726     */
727    public static native double rint(double d);
728
729    /**
730     * Returns the result of rounding the argument to an integer. The result is
731     * equivalent to {@code (long) Math.floor(d+0.5)}.
732     * <p>
733     * Special cases:
734     * <ul>
735     * <li>{@code round(+0.0) = +0.0}</li>
736     * <li>{@code round(-0.0) = +0.0}</li>
737     * <li>{@code round((anything > Long.MAX_VALUE) = Long.MAX_VALUE}</li>
738     * <li>{@code round((anything < Long.MIN_VALUE) = Long.MIN_VALUE}</li>
739     * <li>{@code round(+infinity) = Long.MAX_VALUE}</li>
740     * <li>{@code round(-infinity) = Long.MIN_VALUE}</li>
741     * <li>{@code round(NaN) = +0.0}</li>
742     * </ul>
743     *
744     * @param d
745     *            the value to be rounded.
746     * @return the closest integer to the argument.
747     */
748    public static long round(double d) {
749        // check for NaN
750        if (d != d)
751            return 0L;
752        return (long) Math.floor(d + 0.5d);
753    }
754
755    /**
756     * Returns the result of rounding the argument to an integer. The result is
757     * equivalent to {@code (int) Math.floor(f+0.5)}.
758     * <p>
759     * Special cases:
760     * <ul>
761     * <li>{@code round(+0.0) = +0.0}</li>
762     * <li>{@code round(-0.0) = +0.0}</li>
763     * <li>{@code round((anything > Integer.MAX_VALUE) = Integer.MAX_VALUE}</li>
764     * <li>{@code round((anything < Integer.MIN_VALUE) = Integer.MIN_VALUE}</li>
765     * <li>{@code round(+infinity) = Integer.MAX_VALUE}</li>
766     * <li>{@code round(-infinity) = Integer.MIN_VALUE}</li>
767     * <li>{@code round(NaN) = +0.0}</li>
768     * </ul>
769     *
770     * @param f
771     *            the value to be rounded.
772     * @return the closest integer to the argument.
773     */
774    public static int round(float f) {
775        // check for NaN
776        if (f != f)
777            return 0;
778        return (int) Math.floor(f + 0.5f);
779    }
780
781    /**
782     * Returns the signum function of the argument. If the argument is less than
783     * zero, it returns -1.0. If the argument is greater than zero, 1.0 is
784     * returned. If the argument is either positive or negative zero, the
785     * argument is returned as result.
786     * <p>
787     * Special cases:
788     * <ul>
789     * <li>{@code signum(+0.0) = +0.0}</li>
790     * <li>{@code signum(-0.0) = -0.0}</li>
791     * <li>{@code signum(+infinity) = +1.0}</li>
792     * <li>{@code signum(-infinity) = -1.0}</li>
793     * <li>{@code signum(NaN) = NaN}</li>
794     * </ul>
795     *
796     * @param d
797     *            the value whose signum has to be computed.
798     * @return the value of the signum function.
799     */
800    public static double signum(double d){
801        if(Double.isNaN(d)){
802            return Double.NaN;
803        }
804        double sig = d;
805        if(d > 0){
806            sig = 1.0;
807        }else if (d < 0){
808            sig = -1.0;
809        }
810        return sig;
811    }
812
813    /**
814     * Returns the signum function of the argument. If the argument is less than
815     * zero, it returns -1.0. If the argument is greater than zero, 1.0 is
816     * returned. If the argument is either positive or negative zero, the
817     * argument is returned as result.
818     * <p>
819     * Special cases:
820     * <ul>
821     * <li>{@code signum(+0.0) = +0.0}</li>
822     * <li>{@code signum(-0.0) = -0.0}</li>
823     * <li>{@code signum(+infinity) = +1.0}</li>
824     * <li>{@code signum(-infinity) = -1.0}</li>
825     * <li>{@code signum(NaN) = NaN}</li>
826     * </ul>
827     *
828     * @param f
829     *            the value whose signum has to be computed.
830     * @return the value of the signum function.
831     */
832    public static float signum(float f){
833        if(Float.isNaN(f)){
834            return Float.NaN;
835        }
836        float sig = f;
837        if(f > 0){
838            sig = 1.0f;
839        }else if (f < 0){
840            sig = -1.0f;
841        }
842        return sig;
843    }
844
845    /**
846     * Returns the closest double approximation of the hyperbolic sine of the
847     * argument.
848     * <p>
849     * Special cases:
850     * <ul>
851     * <li>{@code sinh(+0.0) = +0.0}</li>
852     * <li>{@code sinh(-0.0) = -0.0}</li>
853     * <li>{@code sinh(+infinity) = +infinity}</li>
854     * <li>{@code sinh(-infinity) = -infinity}</li>
855     * <li>{@code sinh(NaN) = NaN}</li>
856     * </ul>
857     *
858     * @param d
859     *            the value whose hyperbolic sine has to be computed.
860     * @return the hyperbolic sine of the argument.
861     */
862    public static native double sinh(double d);
863
864    /**
865     * Returns the closest double approximation of the sine of the argument.
866     * <p>
867     * Special cases:
868     * <ul>
869     * <li>{@code sin(+0.0) = +0.0}</li>
870     * <li>{@code sin(-0.0) = -0.0}</li>
871     * <li>{@code sin(+infinity) = NaN}</li>
872     * <li>{@code sin(-infinity) = NaN}</li>
873     * <li>{@code sin(NaN) = NaN}</li>
874     * </ul>
875     *
876     * @param d
877     *            the angle whose sin has to be computed, in radians.
878     * @return the sine of the argument.
879     */
880    public static native double sin(double d);
881
882    /**
883     * Returns the closest double approximation of the square root of the
884     * argument.
885     * <p>
886     * Special cases:
887     * <ul>
888     * <li>{@code sqrt(+0.0) = +0.0}</li>
889     * <li>{@code sqrt(-0.0) = -0.0}</li>
890     * <li>{@code sqrt( (anything < 0) ) = NaN}</li>
891     * <li>{@code sqrt(+infinity) = +infinity}</li>
892     * <li>{@code sqrt(NaN) = NaN}</li>
893     * </ul>
894     *
895     * @param d
896     *            the value whose square root has to be computed.
897     * @return the square root of the argument.
898     */
899    public static native double sqrt(double d);
900
901    /**
902     * Returns the closest double approximation of the tangent of the argument.
903     * <p>
904     * Special cases:
905     * <ul>
906     * <li>{@code tan(+0.0) = +0.0}</li>
907     * <li>{@code tan(-0.0) = -0.0}</li>
908     * <li>{@code tan(+infinity) = NaN}</li>
909     * <li>{@code tan(-infinity) = NaN}</li>
910     * <li>{@code tan(NaN) = NaN}</li>
911     * </ul>
912     *
913     * @param d
914     *            the angle whose tangens has to be computed, in radians.
915     * @return the tangent of the argument.
916     */
917    public static native double tan(double d);
918
919    /**
920     * Returns the closest double approximation of the hyperbolic tangent of the
921     * argument. The absolute value is always less than 1.
922     * <p>
923     * Special cases:
924     * <ul>
925     * <li>{@code tanh(+0.0) = +0.0}</li>
926     * <li>{@code tanh(-0.0) = -0.0}</li>
927     * <li>{@code tanh(+infinity) = +1.0}</li>
928     * <li>{@code tanh(-infinity) = -1.0}</li>
929     * <li>{@code tanh(NaN) = NaN}</li>
930     * </ul>
931     *
932     * @param d
933     *            the value whose hyperbolic tangent has to be computed.
934     * @return the hyperbolic tangent of the argument
935     */
936    public static native double tanh(double d);
937
938    /**
939     * Returns the measure in degrees of the supplied radian angle. The result
940     * is {@code angrad * 180 / pi}.
941     * <p>
942     * Special cases:
943     * <ul>
944     * <li>{@code toDegrees(+0.0) = +0.0}</li>
945     * <li>{@code toDegrees(-0.0) = -0.0}</li>
946     * <li>{@code toDegrees(+infinity) = +infinity}</li>
947     * <li>{@code toDegrees(-infinity) = -infinity}</li>
948     * <li>{@code toDegrees(NaN) = NaN}</li>
949     * </ul>
950     *
951     * @param angrad
952     *            an angle in radians.
953     * @return the degree measure of the angle.
954     */
955    public static double toDegrees(double angrad) {
956        return angrad * 180d / PI;
957    }
958
959    /**
960     * Returns the measure in radians of the supplied degree angle. The result
961     * is {@code angdeg / 180 * pi}.
962     * <p>
963     * Special cases:
964     * <ul>
965     * <li>{@code toRadians(+0.0) = +0.0}</li>
966     * <li>{@code toRadians(-0.0) = -0.0}</li>
967     * <li>{@code toRadians(+infinity) = +infinity}</li>
968     * <li>{@code toRadians(-infinity) = -infinity}</li>
969     * <li>{@code toRadians(NaN) = NaN}</li>
970     * </ul>
971     *
972     * @param angdeg
973     *            an angle in degrees.
974     * @return the radian measure of the angle.
975     */
976    public static double toRadians(double angdeg) {
977        return angdeg / 180d * PI;
978    }
979
980    /**
981     * Returns the argument's ulp (unit in the last place). The size of a ulp of
982     * a double value is the positive distance between this value and the double
983     * value next larger in magnitude. For non-NaN {@code x},
984     * {@code ulp(-x) == ulp(x)}.
985     * <p>
986     * Special cases:
987     * <ul>
988     * <li>{@code ulp(+0.0) = Double.MIN_VALUE}</li>
989     * <li>{@code ulp(-0.0) = Double.MIN_VALUE}</li>
990     * <li>{@code ulp(+infintiy) = infinity}</li>
991     * <li>{@code ulp(-infintiy) = infinity}</li>
992     * <li>{@code ulp(NaN) = NaN}</li>
993     * </ul>
994     *
995     * @param d
996     *            the floating-point value to compute ulp of.
997     * @return the size of a ulp of the argument.
998     */
999    public static double ulp(double d) {
1000        // special cases
1001        if (Double.isInfinite(d)) {
1002            return Double.POSITIVE_INFINITY;
1003        } else if (d == Double.MAX_VALUE || d == -Double.MAX_VALUE) {
1004            return pow(2, 971);
1005        }
1006        d = Math.abs(d);
1007        return nextafter(d, Double.MAX_VALUE) - d;
1008    }
1009
1010    /**
1011     * Returns the argument's ulp (unit in the last place). The size of a ulp of
1012     * a float value is the positive distance between this value and the float
1013     * value next larger in magnitude. For non-NaN {@code x},
1014     * {@code ulp(-x) == ulp(x)}.
1015     * <p>
1016     * Special cases:
1017     * <ul>
1018     * <li>{@code ulp(+0.0) = Float.MIN_VALUE}</li>
1019     * <li>{@code ulp(-0.0) = Float.MIN_VALUE}</li>
1020     * <li>{@code ulp(+infintiy) = infinity}</li>
1021     * <li>{@code ulp(-infintiy) = infinity}</li>
1022     * <li>{@code ulp(NaN) = NaN}</li>
1023     * </ul>
1024     *
1025     * @param f
1026     *            the floating-point value to compute ulp of.
1027     * @return the size of a ulp of the argument.
1028     */
1029    public static float ulp(float f) {
1030        // special cases
1031        if (Float.isNaN(f)) {
1032            return Float.NaN;
1033        } else if (Float.isInfinite(f)) {
1034            return Float.POSITIVE_INFINITY;
1035        } else if (f == Float.MAX_VALUE || f == -Float.MAX_VALUE) {
1036            return (float) pow(2, 104);
1037        }
1038        f = Math.abs(f);
1039        return nextafterf(f, Float.MAX_VALUE) - f;
1040    }
1041
1042    private native static double nextafter(double x, double y);
1043
1044    private native static float nextafterf(float x, float y);
1045}
1046