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