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