Math.java revision fdb2704414a9ed92394ada0d1395e4db86889465
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
20
21/**
22 * Class math provides various floating point support routines and some standard
23 * constants.
24 */
25public final class Math {
26
27    /**
28     * The double value closest to e, the base of the natural logarithm.
29     */
30    public static final double E = 2.718281828459045;
31
32    /**
33     * The double value closest to pi, the ratio of a circle's circumference to its diameter.
34     */
35    public static final double PI = 3.141592653589793;
36
37    private static java.util.Random random;
38
39    /**
40     * Prevents this class from being instantiated.
41     */
42    private Math() {
43    }
44
45    /**
46     * Returns the absolute value of the argument.
47     *
48     * @param d
49     *            the value to be converted
50     * @return the argument if it is positive, otherwise the negation of the
51     *         argument.
52     */
53    public static double abs(double d) {
54        long bits = Double.doubleToLongBits(d);
55        bits &= 0x7fffffffffffffffL;
56        return Double.longBitsToDouble(bits);
57    }
58
59    /**
60     * Returns the absolute value of the argument.
61     *
62     * @param f
63     *            the value to be converted
64     * @return the argument if it is positive, otherwise the negation of the
65     *         argument.
66     */
67    public static float abs(float f) {
68        int bits = Float.floatToIntBits(f);
69        bits &= 0x7fffffff;
70        return Float.intBitsToFloat(bits);
71    }
72
73    /**
74     * Returns the absolute value of the argument.
75     *
76     * @param i
77     *            the value to be converted
78     * @return the argument if it is positive, otherwise the negation of the
79     *         argument.
80     */
81    public static int abs(int i) {
82        return i >= 0 ? i : -i;
83    }
84
85    /**
86     * Returns the absolute value of the argument.
87     *
88     * @param l
89     *            the value to be converted
90     * @return the argument if it is positive, otherwise the negation of the
91     *         argument.
92     */
93    public static long abs(long l) {
94        return l >= 0 ? l : -l;
95    }
96
97    /**
98     * Returns the closest double approximation of the arc cosine of the
99     * argument
100     *
101     * @param d
102     *            the value to compute acos of
103     * @return the arc cosine of the argument.
104     */
105    public static native double acos(double d);
106
107    /**
108     * Returns the closest double approximation of the arc sine of the argument
109     *
110     * @param d
111     *            the value to compute asin of
112     * @return the arc sine of the argument.
113     */
114    public static native double asin(double d);
115
116    /**
117     * Returns the closest double approximation of the arc tangent of the
118     * argument
119     *
120     * @param d
121     *            the value to compute atan of
122     * @return the arc tangent of the argument.
123     */
124    public static native double atan(double d);
125
126    /**
127     * Returns the closest double approximation of the arc tangent of the result
128     * of dividing the first argument by the second argument.
129     *
130     * @param d1
131     *            the numerator of the value to compute atan of
132     * @param d2
133     *            the denominator of the value to compute atan of
134     * @return the arc tangent of d1/d2.
135     */
136    public static native double atan2(double d1, double d2);
137
138    /**
139     * Returns the closest double approximation of the cube root of the
140     * argument. The final result should be within 1ulp of the real result.
141     *
142     * @param d
143     *            the value to compute cube root of
144     * @return the cube root of the argument.
145     */
146    public static native double cbrt(double d);
147
148    /**
149     * Returns the double conversion of the most negative (i.e. closest to
150     * negative infinity) integer value which is greater than the argument.
151     *
152     * @param d the value to be converted
153     * @return the ceiling of the argument.
154     */
155    public static native double ceil(double d);
156
157    /**
158     * Returns the closest double approximation of the cosine of the argument
159     *
160     * @param d
161     *            the angle to compute the cosine of, in radians
162     * @return the cosine of the argument.
163     */
164    public static native double cos(double d);
165
166    /**
167     * Returns the closest double approximation of the hyperbolic cosine of the
168     * argument. The final result should be within 2.5ulps of the real result.
169     *
170     * @param d
171     *            the value to compute hyperbolic cosine of
172     * @return the hyperbolic cosine of the argument.
173     */
174    public static native double cosh(double d);
175
176    /**
177     * Returns the closest double approximation of the raising "e" to the power
178     * of the argument
179     *
180     * @param d
181     *            the value to compute the exponential of
182     * @return the exponential of the argument.
183     */
184    public static native double exp(double d);
185
186    /**
187     * Returns the closest double approximation of <i>e</i><sup>d</sup> - 1.
188     * If the argument is very close to 0, it is much more accurate to use
189     * expm1(d)+1 than exp(d).
190     *
191     * The final result should be within 1 ulp of the real result. For any
192     * finite input, the result should be no less than -1.0. If the real result
193     * is within 0.5 ulp of -1, -1.0 should be answered.
194     *
195     * @param d
196     *            the value to compute the <i>e</i><sup>d</sup> - 1 of
197     * @return the <i>e</i><sup>d</sup> - 1 value of the argument.
198     */
199    public static native double expm1(double d);
200
201    /**
202     * Returns the double conversion of the most positive (i.e. closest to
203     * positive infinity) integer value which is less than the argument.
204     *
205     * @param d the value to be converted
206     * @return the floor of the argument.
207     */
208    public static native double floor(double d);
209
210    /**
211     * Returns sqrt(<i>x</i><sup>2</sup>+<i>y</i><sup>2</sup>). The
212     * final result is without medium underflow or overflow.
213     *
214     * The final result should be within 1 ulp of the real result. If one
215     * parameter remains constant, the result should be semi-monotonic.
216     *
217     * @param x
218     *            a double number
219     * @param y
220     *            a double number
221     * @return the sqrt(<i>x</i><sup>2</sup>+<i>y</i><sup>2</sup>) value
222     *         of the arguments.
223     */
224    public static native double hypot(double x, double y);
225
226    /**
227     * Returns the remainder of dividing the first argument by the second using
228     * the IEEE 754 rules.
229     *
230     * @param d1
231     *            the numerator of the operation
232     * @param d2
233     *            the denominator of the operation
234     * @return the result of d1/d2.
235     */
236    public static native double IEEEremainder(double d1, double d2);
237
238    /**
239     * Returns the closest double approximation of the natural logarithm of the
240     * argument
241     *
242     * @param d
243     *            the value to compute the log of
244     * @return the natural logarithm of the argument.
245     */
246    public static native double log(double d);
247
248    /**
249     * Returns the closest double approximation of the base 10 logarithm of the
250     * argument
251     *
252     * @param d
253     *            the value to compute the log10 of
254     * @return the natural logarithm of the argument.
255     */
256    public static native double log10(double d);
257
258    /**
259     * Returns the closest double approximation of the natural logarithm of the
260     * sum of the argument and 1. If the argument is very close to 0, it is much
261     * more accurate to use log1p(d) than log(1.0+d).
262     *
263     * The final result should be within 1 ulp of the real result and be
264     * semi-monotonic.
265     *
266     * @param d
267     *            the value to compute the ln(1+d) of
268     * @return the natural logarithm of the sum of the argument and 1.
269     */
270    public static native double log1p(double d);
271
272    /**
273     * Returns the most positive (i.e. closest to positive infinity) of the two
274     * arguments.
275     *
276     * @param d1
277     *            the first argument to check
278     * @param d2
279     *            the second argument
280     * @return the larger of d1 and d2.
281     */
282    public static double max(double d1, double d2) {
283        if (d1 > d2)
284            return d1;
285        if (d1 < d2)
286            return d2;
287        /* if either arg is NaN, return NaN */
288        if (d1 != d2)
289            return Double.NaN;
290        /* max( +0.0,-0.0) == +0.0 */
291        if (d1 == 0.0
292                && ((Double.doubleToLongBits(d1) & Double.doubleToLongBits(d2)) & 0x8000000000000000L) == 0)
293            return 0.0;
294        return d1;
295    }
296
297    /**
298     * Returns the most positive (i.e. closest to positive infinity) of the two
299     * arguments.
300     *
301     * @param f1
302     *            the first argument to check
303     * @param f2
304     *            the second argument
305     * @return the larger of f1 and f2.
306     */
307    public static float max(float f1, float f2) {
308        if (f1 > f2)
309            return f1;
310        if (f1 < f2)
311            return f2;
312        /* if either arg is NaN, return NaN */
313        if (f1 != f2)
314            return Float.NaN;
315        /* max( +0.0,-0.0) == +0.0 */
316        if (f1 == 0.0f
317                && ((Float.floatToIntBits(f1) & Float.floatToIntBits(f2)) & 0x80000000) == 0)
318            return 0.0f;
319        return f1;
320    }
321
322    /**
323     * Returns the most positive (i.e. closest to positive infinity) of the two
324     * arguments.
325     *
326     * @param i1
327     *            the first argument to check
328     * @param i2
329     *            the second argument
330     * @return the larger of i1 and i2.
331     */
332    public static int max(int i1, int i2) {
333        return i1 > i2 ? i1 : i2;
334    }
335
336    /**
337     * Returns the most positive (i.e. closest to positive infinity) of the two
338     * arguments.
339     *
340     * @param l1
341     *            the first argument to check
342     * @param l2
343     *            the second argument
344     * @return the larger of l1 and l2.
345     */
346    public static long max(long l1, long l2) {
347        return l1 > l2 ? l1 : l2;
348    }
349
350    /**
351     * Returns the most negative (i.e. closest to negative infinity) of the two
352     * arguments.
353     *
354     * @param d1
355     *            the first argument to check
356     * @param d2
357     *            the second argument
358     * @return the smaller of d1 and d2.
359     */
360    public static double min(double d1, double d2) {
361        if (d1 > d2)
362            return d2;
363        if (d1 < d2)
364            return d1;
365        /* if either arg is NaN, return NaN */
366        if (d1 != d2)
367            return Double.NaN;
368        /* min( +0.0,-0.0) == -0.0 */
369        if (d1 == 0.0
370                && ((Double.doubleToLongBits(d1) | Double.doubleToLongBits(d2)) & 0x8000000000000000l) != 0)
371            return 0.0 * (-1.0);
372        return d1;
373    }
374
375    /**
376     * Returns the most negative (i.e. closest to negative infinity) of the two
377     * arguments.
378     *
379     * @param f1
380     *            the first argument to check
381     * @param f2
382     *            the second argument
383     * @return the smaller of f1 and f2.
384     */
385    public static float min(float f1, float f2) {
386        if (f1 > f2)
387            return f2;
388        if (f1 < f2)
389            return f1;
390        /* if either arg is NaN, return NaN */
391        if (f1 != f2)
392            return Float.NaN;
393        /* min( +0.0,-0.0) == -0.0 */
394        if (f1 == 0.0f
395                && ((Float.floatToIntBits(f1) | Float.floatToIntBits(f2)) & 0x80000000) != 0)
396            return 0.0f * (-1.0f);
397        return f1;
398    }
399
400    /**
401     * Returns the most negative (i.e. closest to negative infinity) of the two
402     * arguments.
403     *
404     * @param i1
405     *            the first argument to check
406     * @param i2
407     *            the second argument
408     * @return the smaller of i1 and i2.
409     */
410    public static int min(int i1, int i2) {
411        return i1 < i2 ? i1 : i2;
412    }
413
414    /**
415     * Returns the most negative (i.e. closest to negative infinity) of the two
416     * arguments.
417     *
418     * @param l1
419     *            the first argument to check
420     * @param l2
421     *            the second argument
422     * @return the smaller of l1 and l2.
423     */
424    public static long min(long l1, long l2) {
425        return l1 < l2 ? l1 : l2;
426    }
427
428    /**
429     * Returns the closest double approximation of the result of raising the
430     * first argument to the power of the second.
431     *
432     * @param d1
433     *            the base of the operation.
434     * @param d2
435     *            the exponent of the operation.
436     * @return d1 to the power of d2
437     */
438    public static native double pow(double d1, double d2);
439
440    /**
441     * Returns the double conversion of the result of rounding the argument to
442     * an integer.
443     *
444     * @param d
445     *            the value to be converted
446     * @return the closest integer to the argument (as a double).
447     */
448    public static native double rint(double d);
449
450    /**
451     * Returns the result of rounding the argument to an integer.
452     *
453     * @param d
454     *            the value to be converted
455     * @return the closest integer to the argument.
456     */
457    public static long round(double d) {
458        // check for NaN
459        if (d != d)
460            return 0L;
461        return (long) floor(d + 0.5d);
462    }
463
464    /**
465     * Returns the result of rounding the argument to an integer.
466     *
467     * @param f
468     *            the value to be converted
469     * @return the closest integer to the argument.
470     */
471    public static int round(float f) {
472        // check for NaN
473        if (f != f)
474            return 0;
475        return (int) floor(f + 0.5f);
476    }
477
478    /**
479     * Returns the signum function of the argument. If the argument is less than
480     * zero, it returns -1.0. If greater than zero, 1.0 is returned. It returns
481     * zero if the argument is also zero.
482     *
483     * @param d
484     *            the value to compute signum function of
485     * @return the value of the signum function.
486     */
487    public static double signum(double d) {
488        return StrictMath.signum(d);
489    }
490
491    /**
492     * Returns the signum function of the argument. If the argument is less than
493     * zero, it returns -1.0. If greater than zero, 1.0 is returned. It returns
494     * zero if the argument is also zero.
495     *
496     * @param f
497     *            the value to compute signum function of
498     * @return the value of the signum function.
499     */
500    public static float signum(float f) {
501        return StrictMath.signum(f);
502    }
503
504    /**
505     * Returns the closest double approximation of the sine of the argument
506     *
507     * @param d
508     *            the angle to compute the sine of, in radians
509     * @return the sine of the argument.
510     */
511    public static native double sin(double d);
512
513    /**
514     * Returns the closest double approximation of the hyperbolic sine of the
515     * argument. The final result should be within 2.5ulps of the real result.
516     *
517     * @param d
518     *            the value to compute hyperbolic sine of
519     * @return the hyperbolic sine of the argument.
520     */
521    public static native double sinh(double d);
522
523    /**
524     * Returns the closest double approximation of the square root of the
525     * argument
526     *
527     * @param d
528     *            the value to compute sqrt of
529     * @return the square root of the argument.
530     */
531    public static native double sqrt(double d);
532
533    /**
534     * Returns the closest double approximation of the tangent of the argument
535     *
536     * @param d
537     *            the angle to compute the tangent of, in radians
538     * @return the tangent of the argument.
539     */
540    public static native double tan(double d);
541
542    /**
543     * Returns the closest double approximation of the hyperbolic tangent of the
544     * argument. The absolute value is always less than 1. The final result
545     * should be within 2.5ulps of the real result. If the real result is
546     * within 0.5ulp of 1 or -1, it should answer exactly +1 or -1.
547     *
548     * @param d
549     *            the value to compute hyperbolic tangent of
550     * @return the hyperbolic tangent of the argument.
551     */
552    public static native double tanh(double d);
553
554    /**
555     * Returns a pseudo-random number between 0.0 and 1.0.
556     *
557     * @return a pseudo-random number
558     */
559    public static double random() {
560        if (random == null) {
561            random = new java.util.Random();
562        }
563        return random.nextDouble();
564    }
565
566    /**
567     * Returns the measure in radians of the supplied degree angle
568     *
569     * @param angdeg
570     *            an angle in degrees
571     * @return the radian measure of the angle.
572     */
573    public static double toRadians(double angdeg) {
574        return angdeg / 180d * PI;
575    }
576
577    /**
578     * Returns the measure in degrees of the supplied radian angle
579     *
580     * @param angrad
581     *            an angle in radians
582     * @return the degree measure of the angle.
583     */
584    public static double toDegrees(double angrad) {
585        return angrad * 180d / PI;
586    }
587
588    /**
589     * Returns the argument's ulp. The size of a ulp of a double value is the
590     * positive distance between this value and the double value next larger
591     * in magnitude. For non-NaN x, ulp(-x) == ulp(x).
592     *
593     * @param d
594     *            the floating-point value to compute ulp of
595     * @return the size of a ulp of the argument.
596     */
597    public static double ulp(double d) {
598        // special cases
599        if (Double.isInfinite(d)) {
600            return Double.POSITIVE_INFINITY;
601        } else if (d == Double.MAX_VALUE || d == -Double.MAX_VALUE) {
602            return pow(2, 971);
603        }
604        d = abs(d);
605        return nextafter(d, Double.MAX_VALUE) - d;
606    }
607
608    /**
609     * Returns the argument's ulp. The size of a ulp of a float value is the
610     * positive distance between this value and the float value next larger
611     * in magnitude. For non-NaN x, ulp(-x) == ulp(x).
612     *
613     * @param f
614     *            the floating-point value to compute ulp of
615     * @return the size of a ulp of the argument.
616     */
617    public static float ulp(float f) {
618        // special cases
619        if (Float.isNaN(f)) {
620            return Float.NaN;
621        } else if (Float.isInfinite(f)) {
622            return Float.POSITIVE_INFINITY;
623        } else if (f == Float.MAX_VALUE || f == -Float.MAX_VALUE) {
624            return (float) pow(2, 104);
625        }
626        f = abs(f);
627        return nextafterf(f, Float.MAX_VALUE) - f;
628    }
629
630    private native static double nextafter(double x, double y);
631
632    private native static float nextafterf(float x, float y);
633}
634