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