Math.java revision 5d40b59c6bba79dc978f173ad64cdfbd8a937018
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 Math provides basic math constants and operations such as trigonometric
24 * functions, hyperbolic functions, exponential, logarithms, etc.
25 */
26public final class Math {
27    private static final int FLOAT_EXPONENT_BIAS = 127;
28
29    private static final int FLOAT_EXPONENT_MASK = 0x7F800000;
30
31    private static final int DOUBLE_NON_MANTISSA_BITS = 12;
32
33    private static final int DOUBLE_MANTISSA_BITS = 52;
34
35    private static final int FLOAT_NON_MANTISSA_BITS = 9;
36
37    private static final int FLOAT_MANTISSA_BITS = 23;
38
39    private static final int DOUBLE_EXPONENT_BIAS = 1023;
40
41    private static final long DOUBLE_EXPONENT_MASK = 0x7ff0000000000000L;
42
43    private static final int FLOAT_MANTISSA_MASK = 0x007fffff;
44
45    private static final int FLOAT_SIGN_MASK = 0x80000000;
46
47    private static final long DOUBLE_MANTISSA_MASK = 0x000fffffffffffffL;
48
49    private static final long DOUBLE_SIGN_MASK = 0x8000000000000000L;
50
51    /**
52     * The double value closest to e, the base of the natural logarithm.
53     */
54    public static final double E = 2.718281828459045;
55
56    /**
57     * The double value closest to pi, the ratio of a circle's circumference to
58     * its diameter.
59     */
60    public static final double PI = 3.141592653589793;
61
62    private static Random random;
63
64    /**
65     * Prevents this class from being instantiated.
66     */
67    private Math() {
68    }
69
70    /**
71     * Returns the absolute value of the argument.
72     * <p>
73     * Special cases:
74     * <ul>
75     * <li>{@code abs(-0.0) = +0.0}</li>
76     * <li>{@code abs(+infinity) = +infinity}</li>
77     * <li>{@code abs(-infinity) = +infinity}</li>
78     * <li>{@code abs(NaN) = NaN}</li>
79     * </ul>
80     *
81     * @param d
82     *            the value whose absolute value has to be computed.
83     * @return the absolute value of the argument.
84     */
85    public static double abs(double d) {
86        long bits = Double.doubleToLongBits(d);
87        bits &= 0x7fffffffffffffffL;
88        return Double.longBitsToDouble(bits);
89    }
90
91    /**
92     * Returns the absolute value of the argument.
93     * <p>
94     * Special cases:
95     * <ul>
96     * <li>{@code abs(-0.0) = +0.0}</li>
97     * <li>{@code abs(+infinity) = +infinity}</li>
98     * <li>{@code abs(-infinity) = +infinity}</li>
99     * <li>{@code abs(NaN) = NaN}</li>
100     * </ul>
101     *
102     * @param f
103     *            the value whose absolute value has to be computed.
104     * @return the argument if it is positive, otherwise the negation of the
105     *         argument.
106     */
107    public static float abs(float f) {
108        int bits = Float.floatToIntBits(f);
109        bits &= 0x7fffffff;
110        return Float.intBitsToFloat(bits);
111    }
112
113    /**
114     * Returns the absolute value of the argument.
115     * <p>
116     * If the argument is {@code Integer.MIN_VALUE}, {@code Integer.MIN_VALUE}
117     * is returned.
118     *
119     * @param i
120     *            the value whose absolute value has to be computed.
121     * @return the argument if it is positive, otherwise the negation of the
122     *         argument.
123     */
124    public static int abs(int i) {
125        return i >= 0 ? i : -i;
126    }
127
128    /**
129     * Returns the absolute value of the argument. If the argument is {@code
130     * Long.MIN_VALUE}, {@code Long.MIN_VALUE} is returned.
131     *
132     * @param l
133     *            the value whose absolute value has to be computed.
134     * @return the argument if it is positive, otherwise the negation of the
135     *         argument.
136     */
137    public static long abs(long l) {
138        return l >= 0 ? l : -l;
139    }
140
141    /**
142     * Returns the closest double approximation of the arc cosine of the
143     * argument within the range {@code [0..pi]}. The returned result is within
144     * 1 ulp (unit in the last place) of the real result.
145     * <p>
146     * Special cases:
147     * <ul>
148     * <li>{@code acos((anything > 1) = NaN}</li>
149     * <li>{@code acos((anything < -1) = NaN}</li>
150     * <li>{@code acos(NaN) = NaN}</li>
151     * </ul>
152     *
153     * @param d
154     *            the value to compute arc cosine of.
155     * @return the arc cosine of the argument.
156     */
157    public static native double acos(double d);
158
159    /**
160     * Returns the closest double approximation of the arc sine of the argument
161     * within the range {@code [-pi/2..pi/2]}. The returned result is within 1
162     * ulp (unit in the last place) of the real result.
163     * <p>
164     * Special cases:
165     * <ul>
166     * <li>{@code asin((anything > 1)) = NaN}</li>
167     * <li>{@code asin((anything < -1)) = NaN}</li>
168     * <li>{@code asin(NaN) = NaN}</li>
169     * </ul>
170     *
171     * @param d
172     *            the value whose arc sine has to be computed.
173     * @return the arc sine of the argument.
174     */
175    public static native double asin(double d);
176
177    /**
178     * Returns the closest double approximation of the arc tangent of the
179     * argument within the range {@code [-pi/2..pi/2]}. The returned result is
180     * within 1 ulp (unit in the last place) of the real result.
181     * <p>
182     * Special cases:
183     * <ul>
184     * <li>{@code atan(+0.0) = +0.0}</li>
185     * <li>{@code atan(-0.0) = -0.0}</li>
186     * <li>{@code atan(+infinity) = +pi/2}</li>
187     * <li>{@code atan(-infinity) = -pi/2}</li>
188     * <li>{@code atan(NaN) = NaN}</li>
189     * </ul>
190     *
191     * @param d
192     *            the value whose arc tangent has to be computed.
193     * @return the arc tangent of the argument.
194     */
195    public static native double atan(double d);
196
197    /**
198     * Returns the closest double approximation of the arc tangent of {@code
199     * y/x} within the range {@code [-pi..pi]}. This is the angle of the polar
200     * representation of the rectangular coordinates (x,y). The returned result
201     * is within 2 ulps (units in the last place) of the real result.
202     * <p>
203     * Special cases:
204     * <ul>
205     * <li>{@code atan2((anything), NaN ) = NaN;}</li>
206     * <li>{@code atan2(NaN , (anything) ) = NaN;}</li>
207     * <li>{@code atan2(+0.0, +(anything but NaN)) = +0.0}</li>
208     * <li>{@code atan2(-0.0, +(anything but NaN)) = -0.0}</li>
209     * <li>{@code atan2(+0.0, -(anything but NaN)) = +pi}</li>
210     * <li>{@code atan2(-0.0, -(anything but NaN)) = -pi}</li>
211     * <li>{@code atan2(+(anything but 0 and NaN), 0) = +pi/2}</li>
212     * <li>{@code atan2(-(anything but 0 and NaN), 0) = -pi/2}</li>
213     * <li>{@code atan2(+(anything but infinity and NaN), +infinity)} {@code =}
214     * {@code +0.0}</li>
215     * <li>{@code atan2(-(anything but infinity and NaN), +infinity)} {@code =}
216     * {@code -0.0}</li>
217     * <li>{@code atan2(+(anything but infinity and NaN), -infinity) = +pi}</li>
218     * <li>{@code atan2(-(anything but infinity and NaN), -infinity) = -pi}</li>
219     * <li>{@code atan2(+infinity, +infinity ) = +pi/4}</li>
220     * <li>{@code atan2(-infinity, +infinity ) = -pi/4}</li>
221     * <li>{@code atan2(+infinity, -infinity ) = +3pi/4}</li>
222     * <li>{@code atan2(-infinity, -infinity ) = -3pi/4}</li>
223     * <li>{@code atan2(+infinity, (anything but,0, NaN, and infinity))} {@code
224     * =} {@code +pi/2}</li>
225     * <li>{@code atan2(-infinity, (anything but,0, NaN, and infinity))} {@code
226     * =} {@code -pi/2}</li>
227     * </ul>
228     *
229     * @param y
230     *            the numerator of the value whose atan has to be computed.
231     * @param x
232     *            the denominator of the value whose atan has to be computed.
233     * @return the arc tangent of {@code y/x}.
234     */
235    public static native double atan2(double y, double x);
236
237    /**
238     * Returns the closest double approximation of the cube root of the
239     * argument.
240     * <p>
241     * Special cases:
242     * <ul>
243     * <li>{@code cbrt(+0.0) = +0.0}</li>
244     * <li>{@code cbrt(-0.0) = -0.0}</li>
245     * <li>{@code cbrt(+infinity) = +infinity}</li>
246     * <li>{@code cbrt(-infinity) = -infinity}</li>
247     * <li>{@code cbrt(NaN) = NaN}</li>
248     * </ul>
249     *
250     * @param d
251     *            the value whose cube root has to be computed.
252     * @return the cube root of the argument.
253     */
254    public static native double cbrt(double d);
255
256    /**
257     * Returns the double conversion of the most negative (closest to negative
258     * infinity) integer value which is greater than the argument.
259     * <p>
260     * Special cases:
261     * <ul>
262     * <li>{@code ceil(+0.0) = +0.0}</li>
263     * <li>{@code ceil(-0.0) = -0.0}</li>
264     * <li>{@code ceil((anything in range (-1,0)) = -0.0}</li>
265     * <li>{@code ceil(+infinity) = +infinity}</li>
266     * <li>{@code ceil(-infinity) = -infinity}</li>
267     * <li>{@code ceil(NaN) = NaN}</li>
268     * </ul>
269     *
270     * @param d
271     *            the value whose closest integer value has to be computed.
272     * @return the ceiling of the argument.
273     */
274    public static native double ceil(double d);
275
276    /**
277     * Returns the closest double approximation of the cosine of the argument.
278     * The returned result is within 1 ulp (unit in the last place) of the real
279     * result.
280     * <p>
281     * Special cases:
282     * <ul>
283     * <li>{@code cos(+infinity) = NaN}</li>
284     * <li>{@code cos(-infinity) = NaN}</li>
285     * <li>{@code cos(NaN) = NaN}</li>
286     * </ul>
287     *
288     * @param d
289     *            the angle whose cosine has to be computed, in radians.
290     * @return the cosine of the argument.
291     */
292    public static native double cos(double d);
293
294    /**
295     * Returns the closest double approximation of the hyperbolic cosine of the
296     * argument. The returned result is within 2.5 ulps (units in the last
297     * place) of the real result.
298     * <p>
299     * Special cases:
300     * <ul>
301     * <li>{@code cosh(+infinity) = +infinity}</li>
302     * <li>{@code cosh(-infinity) = +infinity}</li>
303     * <li>{@code cosh(NaN) = NaN}</li>
304     * </ul>
305     *
306     * @param d
307     *            the value whose hyperbolic cosine has to be computed.
308     * @return the hyperbolic cosine of the argument.
309     */
310    public static native double cosh(double d);
311
312    /**
313     * Returns the closest double approximation of the raising "e" to the power
314     * of the argument. The returned result is within 1 ulp (unit in the last
315     * place) of the real result.
316     * <p>
317     * Special cases:
318     * <ul>
319     * <li>{@code exp(+infinity) = +infinity}</li>
320     * <li>{@code exp(-infinity) = +0.0}</li>
321     * <li>{@code exp(NaN) = NaN}</li>
322     * </ul>
323     *
324     * @param d
325     *            the value whose exponential has to be computed.
326     * @return the exponential of the argument.
327     */
328    public static native double exp(double d);
329
330    /**
331     * Returns the closest double approximation of <i>{@code e}</i><sup> {@code
332     * d}</sup>{@code - 1}. If the argument is very close to 0, it is much more
333     * accurate to use {@code expm1(d)+1} than {@code exp(d)} (due to
334     * cancellation of significant digits). The returned result is within 1 ulp
335     * (unit in the last place) of the real result.
336     * <p>
337     * For any finite input, the result is not less than -1.0. If the real
338     * result is within 0.5 ulp of -1, -1.0 is returned.
339     * <p>
340     * Special cases:
341     * <ul>
342     * <li>{@code expm1(+0.0) = +0.0}</li>
343     * <li>{@code expm1(-0.0) = -0.0}</li>
344     * <li>{@code expm1(+infinity) = +infinity}</li>
345     * <li>{@code expm1(-infinity) = -1.0}</li>
346     * <li>{@code expm1(NaN) = NaN}</li>
347     * </ul>
348     *
349     * @param d
350     *            the value to compute the <i>{@code e}</i><sup>{@code d} </sup>
351     *            {@code - 1} of.
352     * @return the <i>{@code e}</i><sup>{@code d}</sup>{@code - 1} value of the
353     *         argument.
354     */
355    public static native double expm1(double d);
356
357    /**
358     * Returns the double conversion of the most positive (closest to positive
359     * infinity) integer value which is less than the argument.
360     * <p>
361     * Special cases:
362     * <ul>
363     * <li>{@code floor(+0.0) = +0.0}</li>
364     * <li>{@code floor(-0.0) = -0.0}</li>
365     * <li>{@code floor(+infinity) = +infinity}</li>
366     * <li>{@code floor(-infinity) = -infinity}</li>
367     * <li>{@code floor(NaN) = NaN}</li>
368     * </ul>
369     *
370     * @param d
371     *            the value whose closest integer value has to be computed.
372     * @return the floor of the argument.
373     */
374    public static native double floor(double d);
375
376    /**
377     * Returns {@code sqrt(}<i>{@code x}</i><sup>{@code 2}</sup>{@code +} <i>
378     * {@code y}</i><sup>{@code 2}</sup>{@code )}. The final result is without
379     * medium underflow or overflow. The returned result is within 1 ulp (unit
380     * in the last place) of the real result. If one parameter remains constant,
381     * the result should be semi-monotonic.
382     * <p>
383     * Special cases:
384     * <ul>
385     * <li>{@code hypot(+infinity, (anything including NaN)) = +infinity}</li>
386     * <li>{@code hypot(-infinity, (anything including NaN)) = +infinity}</li>
387     * <li>{@code hypot((anything including NaN), +infinity) = +infinity}</li>
388     * <li>{@code hypot((anything including NaN), -infinity) = +infinity}</li>
389     * <li>{@code hypot(NaN, NaN) = NaN}</li>
390     * </ul>
391     *
392     * @param x
393     *            a double number.
394     * @param y
395     *            a double number.
396     * @return the {@code sqrt(}<i>{@code x}</i><sup>{@code 2}</sup>{@code +}
397     *         <i> {@code y}</i><sup>{@code 2}</sup>{@code )} value of the
398     *         arguments.
399     */
400    public static native double hypot(double x, double y);
401
402    /**
403     * Returns the remainder of dividing {@code x} by {@code y} using the IEEE
404     * 754 rules. The result is {@code x-round(x/p)*p} where {@code round(x/p)}
405     * is the nearest integer (rounded to even), but without numerical
406     * cancellation problems.
407     * <p>
408     * Special cases:
409     * <ul>
410     * <li>{@code IEEEremainder((anything), 0) = NaN}</li>
411     * <li>{@code IEEEremainder(+infinity, (anything)) = NaN}</li>
412     * <li>{@code IEEEremainder(-infinity, (anything)) = NaN}</li>
413     * <li>{@code IEEEremainder(NaN, (anything)) = NaN}</li>
414     * <li>{@code IEEEremainder((anything), NaN) = NaN}</li>
415     * <li>{@code IEEEremainder(x, +infinity) = x } where x is anything but
416     * +/-infinity</li>
417     * <li>{@code IEEEremainder(x, -infinity) = x } where x is anything but
418     * +/-infinity</li>
419     * </ul>
420     *
421     * @param x
422     *            the numerator of the operation.
423     * @param y
424     *            the denominator of the operation.
425     * @return the IEEE754 floating point reminder of of {@code x/y}.
426     */
427    public static native double IEEEremainder(double x, double y);
428
429    /**
430     * Returns the closest double approximation of the natural logarithm of the
431     * argument. The returned result is within 1 ulp (unit in the last place) of
432     * the real result.
433     * <p>
434     * Special cases:
435     * <ul>
436     * <li>{@code log(+0.0) = -infinity}</li>
437     * <li>{@code log(-0.0) = -infinity}</li>
438     * <li>{@code log((anything < 0) = NaN}</li>
439     * <li>{@code log(+infinity) = +infinity}</li>
440     * <li>{@code log(-infinity) = NaN}</li>
441     * <li>{@code log(NaN) = NaN}</li>
442     * </ul>
443     *
444     * @param d
445     *            the value whose log has to be computed.
446     * @return the natural logarithm of the argument.
447     */
448    public static native double log(double d);
449
450    /**
451     * Returns the closest double approximation of the base 10 logarithm of the
452     * argument. The returned result is within 1 ulp (unit in the last place) of
453     * the real result.
454     * <p>
455     * Special cases:
456     * <ul>
457     * <li>{@code log10(+0.0) = -infinity}</li>
458     * <li>{@code log10(-0.0) = -infinity}</li>
459     * <li>{@code log10((anything < 0) = NaN}</li>
460     * <li>{@code log10(+infinity) = +infinity}</li>
461     * <li>{@code log10(-infinity) = NaN}</li>
462     * <li>{@code log10(NaN) = NaN}</li>
463     * </ul>
464     *
465     * @param d
466     *            the value whose base 10 log has to be computed.
467     * @return the natural logarithm of the argument.
468     */
469    public static native double log10(double d);
470
471    /**
472     * Returns the closest double approximation of the natural logarithm of the
473     * sum of the argument and 1. If the argument is very close to 0, it is much
474     * more accurate to use {@code log1p(d)} than {@code log(1.0+d)} (due to
475     * numerical cancellation). The returned result is within 1 ulp (unit in the
476     * last place) of the real result and is semi-monotonic.
477     * <p>
478     * Special cases:
479     * <ul>
480     * <li>{@code log1p(+0.0) = +0.0}</li>
481     * <li>{@code log1p(-0.0) = -0.0}</li>
482     * <li>{@code log1p((anything < 1)) = NaN}</li>
483     * <li>{@code log1p(-1.0) = -infinity}</li>
484     * <li>{@code log1p(+infinity) = +infinity}</li>
485     * <li>{@code log1p(-infinity) = NaN}</li>
486     * <li>{@code log1p(NaN) = NaN}</li>
487     * </ul>
488     *
489     * @param d
490     *            the value to compute the {@code ln(1+d)} of.
491     * @return the natural logarithm of the sum of the argument and 1.
492     */
493    public static native double log1p(double d);
494
495    /**
496     * Returns the most positive (closest to positive infinity) of the two
497     * arguments.
498     * <p>
499     * Special cases:
500     * <ul>
501     * <li>{@code max(NaN, (anything)) = NaN}</li>
502     * <li>{@code max((anything), NaN) = NaN}</li>
503     * <li>{@code max(+0.0, -0.0) = +0.0}</li>
504     * <li>{@code max(-0.0, +0.0) = +0.0}</li>
505     * </ul>
506     *
507     * @param d1
508     *            the first argument.
509     * @param d2
510     *            the second argument.
511     * @return the larger of {@code d1} and {@code d2}.
512     */
513    public static double max(double d1, double d2) {
514        if (d1 > d2) {
515            return d1;
516        }
517        if (d1 < d2) {
518            return d2;
519        }
520        /* if either arg is NaN, return NaN */
521        if (d1 != d2) {
522            return Double.NaN;
523        }
524        /* max(+0.0,-0.0) == +0.0 */
525        /* Double.doubleToRawLongBits(0.0d) == 0 */
526        if (Double.doubleToRawLongBits(d1) != 0) {
527            return d2;
528        }
529        return 0.0d;
530    }
531
532    /**
533     * Returns the most positive (closest to positive infinity) of the two
534     * arguments.
535     * <p>
536     * Special cases:
537     * <ul>
538     * <li>{@code max(NaN, (anything)) = NaN}</li>
539     * <li>{@code max((anything), NaN) = NaN}</li>
540     * <li>{@code max(+0.0, -0.0) = +0.0}</li>
541     * <li>{@code max(-0.0, +0.0) = +0.0}</li>
542     * </ul>
543     *
544     * @param f1
545     *            the first argument.
546     * @param f2
547     *            the second argument.
548     * @return the larger of {@code f1} and {@code f2}.
549     */
550    public static float max(float f1, float f2) {
551        if (f1 > f2) {
552            return f1;
553        }
554        if (f1 < f2) {
555            return f2;
556        }
557        /* if either arg is NaN, return NaN */
558        if (f1 != f2) {
559            return Float.NaN;
560        }
561        /* max(+0.0,-0.0) == +0.0 */
562        /* Float.floatToRawIntBits(0.0f) == 0*/
563        if (Float.floatToRawIntBits(f1) != 0) {
564            return f2;
565        }
566        return 0.0f;
567    }
568
569    /**
570     * Returns the most positive (closest to positive infinity) of the two
571     * arguments.
572     *
573     * @param i1
574     *            the first argument.
575     * @param i2
576     *            the second argument.
577     * @return the larger of {@code i1} and {@code i2}.
578     */
579    public static int max(int i1, int i2) {
580        return i1 > i2 ? i1 : i2;
581    }
582
583    /**
584     * Returns the most positive (closest to positive infinity) of the two
585     * arguments.
586     *
587     * @param l1
588     *            the first argument.
589     * @param l2
590     *            the second argument.
591     * @return the larger of {@code l1} and {@code l2}.
592     */
593    public static long max(long l1, long l2) {
594        return l1 > l2 ? l1 : l2;
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 d1
610     *            the first argument.
611     * @param d2
612     *            the second argument.
613     * @return the smaller of {@code d1} and {@code d2}.
614     */
615    public static double min(double d1, double d2) {
616        if (d1 > d2) {
617            return d2;
618        }
619        if (d1 < d2) {
620            return d1;
621        }
622        /* if either arg is NaN, return NaN */
623        if (d1 != d2) {
624            return Double.NaN;
625        }
626        /* min(+0.0,-0.0) == -0.0 */
627        /* 0x8000000000000000L == Double.doubleToRawLongBits(-0.0d) */
628        if (Double.doubleToRawLongBits(d1) == 0x8000000000000000L) {
629            return -0.0d;
630        }
631        return d2;
632    }
633
634    /**
635     * Returns the most negative (closest to negative infinity) of the two
636     * arguments.
637     * <p>
638     * Special cases:
639     * <ul>
640     * <li>{@code min(NaN, (anything)) = NaN}</li>
641     * <li>{@code min((anything), NaN) = NaN}</li>
642     * <li>{@code min(+0.0, -0.0) = -0.0}</li>
643     * <li>{@code min(-0.0, +0.0) = -0.0}</li>
644     * </ul>
645     *
646     * @param f1
647     *            the first argument.
648     * @param f2
649     *            the second argument.
650     * @return the smaller of {@code f1} and {@code f2}.
651     */
652    public static float min(float f1, float f2) {
653        if (f1 > f2) {
654            return f2;
655        }
656        if (f1 < f2) {
657            return f1;
658        }
659        /* if either arg is NaN, return NaN */
660        if (f1 != f2) {
661            return Float.NaN;
662        }
663        /* min(+0.0,-0.0) == -0.0 */
664        /* 0x80000000 == Float.floatToRawIntBits(-0.0f) */
665        if (Float.floatToRawIntBits(f1) == 0x80000000) {
666            return -0.0f;
667        }
668        return f2;
669    }
670
671    /**
672     * Returns the most negative (closest to negative infinity) of the two
673     * arguments.
674     *
675     * @param i1
676     *            the first argument.
677     * @param i2
678     *            the second argument.
679     * @return the smaller of {@code i1} and {@code i2}.
680     */
681    public static int min(int i1, int i2) {
682        return i1 < i2 ? i1 : i2;
683    }
684
685    /**
686     * Returns the most negative (closest to negative infinity) of the two
687     * arguments.
688     *
689     * @param l1
690     *            the first argument.
691     * @param l2
692     *            the second argument.
693     * @return the smaller of {@code l1} and {@code l2}.
694     */
695    public static long min(long l1, long l2) {
696        return l1 < l2 ? l1 : l2;
697    }
698
699    /**
700     * Returns the closest double approximation of the result of raising {@code
701     * x} to the power of {@code y}.
702     * <p>
703     * Special cases:
704     * <ul>
705     * <li>{@code pow((anything), +0.0) = 1.0}</li>
706     * <li>{@code pow((anything), -0.0) = 1.0}</li>
707     * <li>{@code pow(x, 1.0) = x}</li>
708     * <li>{@code pow((anything), NaN) = NaN}</li>
709     * <li>{@code pow(NaN, (anything except 0)) = NaN}</li>
710     * <li>{@code pow(+/-(|x| > 1), +infinity) = +infinity}</li>
711     * <li>{@code pow(+/-(|x| > 1), -infinity) = +0.0}</li>
712     * <li>{@code pow(+/-(|x| < 1), +infinity) = +0.0}</li>
713     * <li>{@code pow(+/-(|x| < 1), -infinity) = +infinity}</li>
714     * <li>{@code pow(+/-1.0 , +infinity) = NaN}</li>
715     * <li>{@code pow(+/-1.0 , -infinity) = NaN}</li>
716     * <li>{@code pow(+0.0, (+anything except 0, NaN)) = +0.0}</li>
717     * <li>{@code pow(-0.0, (+anything except 0, NaN, odd integer)) = +0.0}</li>
718     * <li>{@code pow(+0.0, (-anything except 0, NaN)) = +infinity}</li>
719     * <li>{@code pow(-0.0, (-anything except 0, NAN, odd integer))} {@code =}
720     * {@code +infinity}</li>
721     * <li>{@code pow(-0.0, (odd integer)) = -pow( +0 , (odd integer) )}</li>
722     * <li>{@code pow(+infinity, (+anything except 0, NaN)) = +infinity}</li>
723     * <li>{@code pow(+infinity, (-anything except 0, NaN)) = +0.0}</li>
724     * <li>{@code pow(-infinity, (anything)) = -pow(0, (-anything))}</li>
725     * <li>{@code pow((-anything), (integer))} {@code =} {@code
726     * pow(-1,(integer))*pow(+anything,integer) }</li>
727     * <li>{@code pow((-anything except 0 and inf), (non-integer)) = NAN}</li>
728     * </ul>
729     *
730     * @param x
731     *            the base of the operation.
732     * @param y
733     *            the exponent of the operation.
734     * @return {@code x} to the power of {@code y}.
735     */
736    public static native double pow(double x, double y);
737
738    /**
739     * Returns the double conversion of the result of rounding the argument to
740     * an integer. Tie breaks are rounded towards even.
741     * <p>
742     * Special cases:
743     * <ul>
744     * <li>{@code rint(+0.0) = +0.0}</li>
745     * <li>{@code rint(-0.0) = -0.0}</li>
746     * <li>{@code rint(+infinity) = +infinity}</li>
747     * <li>{@code rint(-infinity) = -infinity}</li>
748     * <li>{@code rint(NaN) = NaN}</li>
749     * </ul>
750     *
751     * @param d
752     *            the value to be rounded.
753     * @return the closest integer to the argument (as a double).
754     */
755    public static native double rint(double d);
756
757    /**
758     * Returns the result of rounding the argument to an integer. The result is
759     * equivalent to {@code (long) Math.floor(d+0.5)}.
760     * <p>
761     * Special cases:
762     * <ul>
763     * <li>{@code round(+0.0) = +0.0}</li>
764     * <li>{@code round(-0.0) = +0.0}</li>
765     * <li>{@code round((anything > Long.MAX_VALUE) = Long.MAX_VALUE}</li>
766     * <li>{@code round((anything < Long.MIN_VALUE) = Long.MIN_VALUE}</li>
767     * <li>{@code round(+infinity) = Long.MAX_VALUE}</li>
768     * <li>{@code round(-infinity) = Long.MIN_VALUE}</li>
769     * <li>{@code round(NaN) = +0.0}</li>
770     * </ul>
771     *
772     * @param d
773     *            the value to be rounded.
774     * @return the closest integer to the argument.
775     */
776    public static long round(double d) {
777        // check for NaN
778        if (d != d) {
779            return 0L;
780        }
781        return (long) floor(d + 0.5d);
782    }
783
784    /**
785     * Returns the result of rounding the argument to an integer. The result is
786     * equivalent to {@code (int) Math.floor(f+0.5)}.
787     * <p>
788     * Special cases:
789     * <ul>
790     * <li>{@code round(+0.0) = +0.0}</li>
791     * <li>{@code round(-0.0) = +0.0}</li>
792     * <li>{@code round((anything > Integer.MAX_VALUE) = Integer.MAX_VALUE}</li>
793     * <li>{@code round((anything < Integer.MIN_VALUE) = Integer.MIN_VALUE}</li>
794     * <li>{@code round(+infintiy) = Integer.MAX_VALUE}</li>
795     * <li>{@code round(-infintiy) = Integer.MIN_VALUE}</li>
796     * <li>{@code round(NaN) = +0.0}</li>
797     * </ul>
798     *
799     * @param f
800     *            the value to be rounded.
801     * @return the closest integer to the argument.
802     */
803    public static int round(float f) {
804        // check for NaN
805        if (f != f) {
806            return 0;
807        }
808        return (int) floor(f + 0.5f);
809    }
810
811    /**
812     * Returns the signum function of the argument. If the argument is less than
813     * zero, it returns -1.0. If the argument is greater than zero, 1.0 is
814     * returned. If the argument is either positive or negative zero, the
815     * argument is returned as result.
816     * <p>
817     * Special cases:
818     * <ul>
819     * <li>{@code signum(+0.0) = +0.0}</li>
820     * <li>{@code signum(-0.0) = -0.0}</li>
821     * <li>{@code signum(+infinity) = +1.0}</li>
822     * <li>{@code signum(-infinity) = -1.0}</li>
823     * <li>{@code signum(NaN) = NaN}</li>
824     * </ul>
825     *
826     * @param d
827     *            the value whose signum has to be computed.
828     * @return the value of the signum function.
829     */
830    public static double signum(double d) {
831        if (Double.isNaN(d)) {
832            return Double.NaN;
833        }
834        double sig = d;
835        if (d > 0) {
836            sig = 1.0;
837        } else if (d < 0) {
838            sig = -1.0;
839        }
840        return sig;
841    }
842
843    /**
844     * Returns the signum function of the argument. If the argument is less than
845     * zero, it returns -1.0. If the argument is greater than zero, 1.0 is
846     * returned. If the argument is either positive or negative zero, the
847     * argument is returned as result.
848     * <p>
849     * Special cases:
850     * <ul>
851     * <li>{@code signum(+0.0) = +0.0}</li>
852     * <li>{@code signum(-0.0) = -0.0}</li>
853     * <li>{@code signum(+infinity) = +1.0}</li>
854     * <li>{@code signum(-infinity) = -1.0}</li>
855     * <li>{@code signum(NaN) = NaN}</li>
856     * </ul>
857     *
858     * @param f
859     *            the value whose signum has to be computed.
860     * @return the value of the signum function.
861     */
862    public static float signum(float f) {
863        if (Float.isNaN(f)) {
864            return Float.NaN;
865        }
866        float sig = f;
867        if (f > 0) {
868            sig = 1.0f;
869        } else if (f < 0) {
870            sig = -1.0f;
871        }
872        return sig;
873    }
874
875    /**
876     * Returns the closest double approximation of the sine of the argument. The
877     * returned result is within 1 ulp (unit in the last place) of the real
878     * result.
879     * <p>
880     * Special cases:
881     * <ul>
882     * <li>{@code sin(+0.0) = +0.0}</li>
883     * <li>{@code sin(-0.0) = -0.0}</li>
884     * <li>{@code sin(+infinity) = NaN}</li>
885     * <li>{@code sin(-infinity) = NaN}</li>
886     * <li>{@code sin(NaN) = NaN}</li>
887     * </ul>
888     *
889     * @param d
890     *            the angle whose sin has to be computed, in radians.
891     * @return the sine of the argument.
892     */
893    public static native double sin(double d);
894
895    /**
896     * Returns the closest double approximation of the hyperbolic sine of the
897     * argument. The returned result is within 2.5 ulps (units in the last
898     * place) of the real result.
899     * <p>
900     * Special cases:
901     * <ul>
902     * <li>{@code sinh(+0.0) = +0.0}</li>
903     * <li>{@code sinh(-0.0) = -0.0}</li>
904     * <li>{@code sinh(+infinity) = +infinity}</li>
905     * <li>{@code sinh(-infinity) = -infinity}</li>
906     * <li>{@code sinh(NaN) = NaN}</li>
907     * </ul>
908     *
909     * @param d
910     *            the value whose hyperbolic sine has to be computed.
911     * @return the hyperbolic sine of the argument.
912     */
913    public static native double sinh(double d);
914
915    /**
916     * Returns the closest double approximation of the square root of the
917     * argument.
918     * <p>
919     * Special cases:
920     * <ul>
921     * <li>{@code sqrt(+0.0) = +0.0}</li>
922     * <li>{@code sqrt(-0.0) = -0.0}</li>
923     * <li>{@code sqrt( (anything < 0) ) = NaN}</li>
924     * <li>{@code sqrt(+infinity) = +infinity}</li>
925     * <li>{@code sqrt(NaN) = NaN}</li>
926     * </ul>
927     *
928     * @param d
929     *            the value whose square root has to be computed.
930     * @return the square root of the argument.
931     */
932    public static native double sqrt(double d);
933
934    /**
935     * Returns the closest double approximation of the tangent of the argument.
936     * The returned result is within 1 ulp (unit in the last place) of the real
937     * result.
938     * <p>
939     * Special cases:
940     * <ul>
941     * <li>{@code tan(+0.0) = +0.0}</li>
942     * <li>{@code tan(-0.0) = -0.0}</li>
943     * <li>{@code tan(+infinity) = NaN}</li>
944     * <li>{@code tan(-infinity) = NaN}</li>
945     * <li>{@code tan(NaN) = NaN}</li>
946     * </ul>
947     *
948     * @param d
949     *            the angle whose tangent has to be computed, in radians.
950     * @return the tangent of the argument.
951     */
952    public static native double tan(double d);
953
954    /**
955     * Returns the closest double approximation of the hyperbolic tangent of the
956     * argument. The absolute value is always less than 1. The returned result
957     * is within 2.5 ulps (units in the last place) of the real result. If the
958     * real result is within 0.5ulp of 1 or -1, it should return exactly +1 or
959     * -1.
960     * <p>
961     * Special cases:
962     * <ul>
963     * <li>{@code tanh(+0.0) = +0.0}</li>
964     * <li>{@code tanh(-0.0) = -0.0}</li>
965     * <li>{@code tanh(+infinity) = +1.0}</li>
966     * <li>{@code tanh(-infinity) = -1.0}</li>
967     * <li>{@code tanh(NaN) = NaN}</li>
968     * </ul>
969     *
970     * @param d
971     *            the value whose hyperbolic tangent has to be computed.
972     * @return the hyperbolic tangent of the argument.
973     */
974    public static native double tanh(double d);
975
976    /**
977     * Returns a pseudo-random double {@code n}, where {@code n >= 0.0 && n < 1.0}.
978     * This method reuses a single instance of {@link java.util.Random}.
979     * This method is thread-safe because access to the {@code Random} is synchronized,
980     * but this harms scalability. Applications may find a performance benefit from
981     * allocating a {@code Random} for each of their threads.
982     *
983     * @return a pseudo-random number.
984     */
985    public static synchronized double random() {
986        if (random == null) {
987            random = new Random();
988        }
989        return random.nextDouble();
990    }
991
992    /**
993     * Returns the measure in radians of the supplied degree angle. The result
994     * is {@code angdeg / 180 * pi}.
995     * <p>
996     * Special cases:
997     * <ul>
998     * <li>{@code toRadians(+0.0) = +0.0}</li>
999     * <li>{@code toRadians(-0.0) = -0.0}</li>
1000     * <li>{@code toRadians(+infinity) = +infinity}</li>
1001     * <li>{@code toRadians(-infinity) = -infinity}</li>
1002     * <li>{@code toRadians(NaN) = NaN}</li>
1003     * </ul>
1004     *
1005     * @param angdeg
1006     *            an angle in degrees.
1007     * @return the radian measure of the angle.
1008     */
1009    public static double toRadians(double angdeg) {
1010        return angdeg / 180d * PI;
1011    }
1012
1013    /**
1014     * Returns the measure in degrees of the supplied radian angle. The result
1015     * is {@code angrad * 180 / pi}.
1016     * <p>
1017     * Special cases:
1018     * <ul>
1019     * <li>{@code toDegrees(+0.0) = +0.0}</li>
1020     * <li>{@code toDegrees(-0.0) = -0.0}</li>
1021     * <li>{@code toDegrees(+infinity) = +infinity}</li>
1022     * <li>{@code toDegrees(-infinity) = -infinity}</li>
1023     * <li>{@code toDegrees(NaN) = NaN}</li>
1024     * </ul>
1025     *
1026     * @param angrad
1027     *            an angle in radians.
1028     * @return the degree measure of the angle.
1029     */
1030    public static double toDegrees(double angrad) {
1031        return angrad * 180d / PI;
1032    }
1033
1034    /**
1035     * Returns the argument's ulp (unit in the last place). The size of a ulp of
1036     * a double value is the positive distance between this value and the double
1037     * value next larger in magnitude. For non-NaN {@code x}, {@code ulp(-x) ==
1038     * ulp(x)}.
1039     * <p>
1040     * Special cases:
1041     * <ul>
1042     * <li>{@code ulp(+0.0) = Double.MIN_VALUE}</li>
1043     * <li>{@code ulp(-0.0) = Double.MIN_VALUE}</li>
1044     * <li>{@code ulp(+infintiy) = infinity}</li>
1045     * <li>{@code ulp(-infintiy) = infinity}</li>
1046     * <li>{@code ulp(NaN) = NaN}</li>
1047     * </ul>
1048     *
1049     * @param d
1050     *            the floating-point value to compute ulp of.
1051     * @return the size of a ulp of the argument.
1052     */
1053    public static double ulp(double d) {
1054        // special cases
1055        if (Double.isInfinite(d)) {
1056            return Double.POSITIVE_INFINITY;
1057        } else if (d == Double.MAX_VALUE || d == -Double.MAX_VALUE) {
1058            return pow(2, 971);
1059        }
1060        d = abs(d);
1061        return nextafter(d, Double.MAX_VALUE) - d;
1062    }
1063
1064    /**
1065     * Returns the argument's ulp (unit in the last place). The size of a ulp of
1066     * a float value is the positive distance between this value and the float
1067     * value next larger in magnitude. For non-NaN {@code x}, {@code ulp(-x) ==
1068     * ulp(x)}.
1069     * <p>
1070     * Special cases:
1071     * <ul>
1072     * <li>{@code ulp(+0.0) = Float.MIN_VALUE}</li>
1073     * <li>{@code ulp(-0.0) = Float.MIN_VALUE}</li>
1074     * <li>{@code ulp(+infintiy) = infinity}</li>
1075     * <li>{@code ulp(-infintiy) = infinity}</li>
1076     * <li>{@code ulp(NaN) = NaN}</li>
1077     * </ul>
1078     *
1079     * @param f
1080     *            the floating-point value to compute ulp of.
1081     * @return the size of a ulp of the argument.
1082     */
1083    public static float ulp(float f) {
1084        // special cases
1085        if (Float.isNaN(f)) {
1086            return Float.NaN;
1087        } else if (Float.isInfinite(f)) {
1088            return Float.POSITIVE_INFINITY;
1089        } else if (f == Float.MAX_VALUE || f == -Float.MAX_VALUE) {
1090            return (float) pow(2, 104);
1091        }
1092        f = abs(f);
1093        return nextafterf(f, Float.MAX_VALUE) - f;
1094    }
1095
1096    private native static double nextafter(double x, double y);
1097
1098    private native static float nextafterf(float x, float y);
1099
1100    /**
1101     * Returns a double with the given magnitude and the sign of {@code sign}.
1102     * If {@code sign} is NaN, the sign of the result is arbitrary.
1103     * If you need a determinate sign in such cases, use {@code StrictMath.copySign}.
1104     * @since 1.6
1105     */
1106    public static native double copySign(double magnitude, double sign);
1107
1108    /**
1109     * Returns a float with the given magnitude and the sign of {@code sign}.
1110     * If {@code sign} is NaN, the sign of the result is arbitrary.
1111     * If you need a determinate sign in such cases, use {@code StrictMath.copySign}.
1112     * @since 1.6
1113     */
1114    public static native float copySign(float magnitude, float sign);
1115
1116    /**
1117     * Returns the exponent of float {@code f}.
1118     * @since 1.6
1119     */
1120    public static int getExponent(float f) {
1121        int bits = Float.floatToRawIntBits(f);
1122        bits = (bits & FLOAT_EXPONENT_MASK) >> FLOAT_MANTISSA_BITS;
1123        return bits - FLOAT_EXPONENT_BIAS;
1124    }
1125
1126    /**
1127     * Returns the exponent of double {@code d}.
1128     * @since 1.6
1129     */
1130    public static int getExponent(double d) {
1131        long bits = Double.doubleToRawLongBits(d);
1132        bits = (bits & DOUBLE_EXPONENT_MASK) >> DOUBLE_MANTISSA_BITS;
1133        return (int) bits - DOUBLE_EXPONENT_BIAS;
1134    }
1135
1136    /**
1137     * Returns the next double after {@code start} in the given {@code direction}.
1138     * @since 1.6
1139     */
1140    public static double nextAfter(double start, double direction) {
1141        if (start == 0 && direction == 0) {
1142            return direction;
1143        }
1144        return nextafter(start, direction);
1145    }
1146
1147    /**
1148     * Returns the next float after {@code start} in the given {@code direction}.
1149     * @since 1.6
1150     */
1151    public static float nextAfter(float start, double direction) {
1152        if (Float.isNaN(start) || Double.isNaN(direction)) {
1153            return Float.NaN;
1154        }
1155        if (start == 0 && direction == 0) {
1156            return (float) direction;
1157        }
1158        if ((start == Float.MIN_VALUE && direction < start)
1159                || (start == -Float.MIN_VALUE && direction > start)) {
1160            return (start > 0 ? 0f : -0f);
1161        }
1162        if (Float.isInfinite(start) && (direction != start)) {
1163            return (start > 0 ? Float.MAX_VALUE : -Float.MAX_VALUE);
1164        }
1165        if ((start == Float.MAX_VALUE && direction > start)
1166                || (start == -Float.MAX_VALUE && direction < start)) {
1167            return (start > 0 ? Float.POSITIVE_INFINITY
1168                    : Float.NEGATIVE_INFINITY);
1169        }
1170        if (direction > start) {
1171            if (start > 0) {
1172                return Float.intBitsToFloat(Float.floatToIntBits(start) + 1);
1173            }
1174            if (start < 0) {
1175                return Float.intBitsToFloat(Float.floatToIntBits(start) - 1);
1176            }
1177            return +Float.MIN_VALUE;
1178        }
1179        if (direction < start) {
1180            if (start > 0) {
1181                return Float.intBitsToFloat(Float.floatToIntBits(start) - 1);
1182            }
1183            if (start < 0) {
1184                return Float.intBitsToFloat(Float.floatToIntBits(start) + 1);
1185            }
1186            return -Float.MIN_VALUE;
1187        }
1188        return (float) direction;
1189    }
1190
1191    /**
1192     * Returns the next double larger than {@code d}.
1193     * @since 1.6
1194     */
1195    public static double nextUp(double d) {
1196        if (Double.isNaN(d)) {
1197            return Double.NaN;
1198        }
1199        if (d == Double.POSITIVE_INFINITY) {
1200            return Double.POSITIVE_INFINITY;
1201        }
1202        if (d == 0) {
1203            return Double.MIN_VALUE;
1204        } else if (d > 0) {
1205            return Double.longBitsToDouble(Double.doubleToLongBits(d) + 1);
1206        } else {
1207            return Double.longBitsToDouble(Double.doubleToLongBits(d) - 1);
1208        }
1209    }
1210
1211    /**
1212     * Returns the next float larger than {@code f}.
1213     * @since 1.6
1214     */
1215    public static float nextUp(float f) {
1216        if (Float.isNaN(f)) {
1217            return Float.NaN;
1218        }
1219        if (f == Float.POSITIVE_INFINITY) {
1220            return Float.POSITIVE_INFINITY;
1221        }
1222        if (f == 0) {
1223            return Float.MIN_VALUE;
1224        } else if (f > 0) {
1225            return Float.intBitsToFloat(Float.floatToIntBits(f) + 1);
1226        } else {
1227            return Float.intBitsToFloat(Float.floatToIntBits(f) - 1);
1228        }
1229    }
1230
1231    /**
1232     * Returns {@code d} * 2^{@code scaleFactor}. The result may be rounded.
1233     * @since 1.6
1234     */
1235    public static double scalb(double d, int scaleFactor) {
1236        if (Double.isNaN(d) || Double.isInfinite(d) || d == 0) {
1237            return d;
1238        }
1239        // change double to long for calculation
1240        long bits = Double.doubleToLongBits(d);
1241        // the sign of the results must be the same of given d
1242        long sign = bits & DOUBLE_SIGN_MASK;
1243        // calculates the factor of the result
1244        long factor = ((bits & DOUBLE_EXPONENT_MASK) >> DOUBLE_MANTISSA_BITS)
1245                - DOUBLE_EXPONENT_BIAS + scaleFactor;
1246
1247        // calculates the factor of sub-normal values
1248        int subNormalFactor = Long.numberOfLeadingZeros(bits
1249                & ~DOUBLE_SIGN_MASK)
1250                - DOUBLE_NON_MANTISSA_BITS;
1251        if (subNormalFactor < 0) {
1252            // not sub-normal values
1253            subNormalFactor = 0;
1254        } else {
1255            factor = factor - subNormalFactor;
1256        }
1257        if (factor > Double.MAX_EXPONENT) {
1258            return (d > 0 ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY);
1259        }
1260
1261        long result;
1262        // if result is a sub-normal
1263        if (factor <= -DOUBLE_EXPONENT_BIAS) {
1264            // the number of digits that shifts
1265            long digits = factor + DOUBLE_EXPONENT_BIAS + subNormalFactor;
1266            if (Math.abs(d) < Double.MIN_NORMAL) {
1267                // origin d is already sub-normal
1268                result = shiftLongBits(bits & DOUBLE_MANTISSA_MASK, digits);
1269            } else {
1270                // origin d is not sub-normal, change mantissa to sub-normal
1271                result = shiftLongBits(bits & DOUBLE_MANTISSA_MASK
1272                        | 0x0010000000000000L, digits - 1);
1273            }
1274        } else {
1275            if (Math.abs(d) >= Double.MIN_NORMAL) {
1276                // common situation
1277                result = ((factor + DOUBLE_EXPONENT_BIAS) << DOUBLE_MANTISSA_BITS)
1278                        | (bits & DOUBLE_MANTISSA_MASK);
1279            } else {
1280                // origin d is sub-normal, change mantissa to normal style
1281                result = ((factor + DOUBLE_EXPONENT_BIAS) << DOUBLE_MANTISSA_BITS)
1282                        | ((bits << (subNormalFactor + 1)) & DOUBLE_MANTISSA_MASK);
1283            }
1284        }
1285        return Double.longBitsToDouble(result | sign);
1286    }
1287
1288    /**
1289     * Returns {@code d} * 2^{@code scaleFactor}. The result may be rounded.
1290     * @since 1.6
1291     */
1292    public static float scalb(float d, int scaleFactor) {
1293        if (Float.isNaN(d) || Float.isInfinite(d) || d == 0) {
1294            return d;
1295        }
1296        int bits = Float.floatToIntBits(d);
1297        int sign = bits & FLOAT_SIGN_MASK;
1298        int factor = ((bits & FLOAT_EXPONENT_MASK) >> FLOAT_MANTISSA_BITS)
1299                - FLOAT_EXPONENT_BIAS + scaleFactor;
1300        // calcutes the factor of sub-normal values
1301        int subNormalFactor = Integer.numberOfLeadingZeros(bits
1302                & ~FLOAT_SIGN_MASK)
1303                - FLOAT_NON_MANTISSA_BITS;
1304        if (subNormalFactor < 0) {
1305            // not sub-normal values
1306            subNormalFactor = 0;
1307        } else {
1308            factor = factor - subNormalFactor;
1309        }
1310        if (factor > Float.MAX_EXPONENT) {
1311            return (d > 0 ? Float.POSITIVE_INFINITY : Float.NEGATIVE_INFINITY);
1312        }
1313
1314        int result;
1315        // if result is a sub-normal
1316        if (factor <= -FLOAT_EXPONENT_BIAS) {
1317            // the number of digits that shifts
1318            int digits = factor + FLOAT_EXPONENT_BIAS + subNormalFactor;
1319            if (Math.abs(d) < Float.MIN_NORMAL) {
1320                // origin d is already sub-normal
1321                result = shiftIntBits(bits & FLOAT_MANTISSA_MASK, digits);
1322            } else {
1323                // origin d is not sub-normal, change mantissa to sub-normal
1324                result = shiftIntBits(bits & FLOAT_MANTISSA_MASK | 0x00800000,
1325                        digits - 1);
1326            }
1327        } else {
1328            if (Math.abs(d) >= Float.MIN_NORMAL) {
1329                // common situation
1330                result = ((factor + FLOAT_EXPONENT_BIAS) << FLOAT_MANTISSA_BITS)
1331                        | (bits & FLOAT_MANTISSA_MASK);
1332            } else {
1333                // origin d is sub-normal, change mantissa to normal style
1334                result = ((factor + FLOAT_EXPONENT_BIAS) << FLOAT_MANTISSA_BITS)
1335                        | ((bits << (subNormalFactor + 1)) & FLOAT_MANTISSA_MASK);
1336            }
1337        }
1338        return Float.intBitsToFloat(result | sign);
1339    }
1340
1341    // Shifts integer bits as float, if the digits is positive, left-shift; if
1342    // not, shift to right and calculate its carry.
1343    private static int shiftIntBits(int bits, int digits) {
1344        if (digits > 0) {
1345            return bits << digits;
1346        }
1347        // change it to positive
1348        int absdigits = -digits;
1349        if (!(Integer.numberOfLeadingZeros(bits & ~FLOAT_SIGN_MASK) <= (32 - absdigits))) {
1350            return 0;
1351        }
1352        int ret = bits >> absdigits;
1353        boolean halfbit = ((bits >> (absdigits - 1)) & 0x1) == 1;
1354        if (halfbit) {
1355            if (Integer.numberOfTrailingZeros(bits) < (absdigits - 1)) {
1356                ret = ret + 1;
1357            }
1358            if (Integer.numberOfTrailingZeros(bits) == (absdigits - 1)) {
1359                if ((ret & 0x1) == 1) {
1360                    ret = ret + 1;
1361                }
1362            }
1363        }
1364        return ret;
1365    }
1366
1367    // Shifts long bits as double, if the digits is positive, left-shift; if
1368    // not, shift to right and calculate its carry.
1369    private static long shiftLongBits(long bits, long digits) {
1370        if (digits > 0) {
1371            return bits << digits;
1372        }
1373        // change it to positive
1374        long absdigits = -digits;
1375        if (!(Long.numberOfLeadingZeros(bits & ~DOUBLE_SIGN_MASK) <= (64 - absdigits))) {
1376            return 0;
1377        }
1378        long ret = bits >> absdigits;
1379        boolean halfbit = ((bits >> (absdigits - 1)) & 0x1) == 1;
1380        if (halfbit) {
1381            // some bits will remain after shifting, calculates its carry
1382            // subnormal
1383            if (Long.numberOfTrailingZeros(bits) < (absdigits - 1)) {
1384                ret = ret + 1;
1385            }
1386            if (Long.numberOfTrailingZeros(bits) == (absdigits - 1)) {
1387                if ((ret & 0x1) == 1) {
1388                    ret = ret + 1;
1389                }
1390            }
1391        }
1392        return ret;
1393    }
1394}
1395