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