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