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