Math.java revision a389b4a499f40379b0b204d7ba1c2057663d95c0
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 26 /** 27 * The double value closest to e, the base of the natural logarithm. 28 */ 29 public static final double E = 2.718281828459045; 30 31 /** 32 * The double value closest to pi, the ratio of a circle's circumference to 33 * its diameter. 34 */ 35 public static final double PI = 3.141592653589793; 36 37 private static java.util.Random random; 38 39 /** 40 * Prevents this class from being instantiated. 41 */ 42 private Math() { 43 } 44 45 /** 46 * Returns the absolute value of the argument. 47 * <p> 48 * Special cases: 49 * <ul> 50 * <li>{@code abs(-0.0) = +0.0}</li> 51 * <li>{@code abs(+infinity) = +infinity}</li> 52 * <li>{@code abs(-infinity) = +infinity}</li> 53 * <li>{@code abs(NaN) = NaN}</li> 54 * </ul> 55 * 56 * @param d 57 * the value whose absolute value has to be computed. 58 * @return the absolute value of the argument. 59 */ 60 public static double abs(double d) { 61 long bits = Double.doubleToLongBits(d); 62 bits &= 0x7fffffffffffffffL; 63 return Double.longBitsToDouble(bits); 64 } 65 66 /** 67 * Returns the absolute value of the argument. 68 * <p> 69 * Special cases: 70 * <ul> 71 * <li>{@code abs(-0.0) = +0.0}</li> 72 * <li>{@code abs(+infinity) = +infinity}</li> 73 * <li>{@code abs(-infinity) = +infinity}</li> 74 * <li>{@code abs(NaN) = NaN}</li> 75 * </ul> 76 * 77 * @param f 78 * the value whose absolute value has to be computed. 79 * @return the argument if it is positive, otherwise the negation of the 80 * argument. 81 */ 82 public static float abs(float f) { 83 int bits = Float.floatToIntBits(f); 84 bits &= 0x7fffffff; 85 return Float.intBitsToFloat(bits); 86 } 87 88 /** 89 * Returns the absolute value of the argument. 90 * <p> 91 * If the argument is {@code Integer.MIN_VALUE}, {@code Integer.MIN_VALUE} 92 * is returned. 93 * 94 * @param i 95 * the value whose absolute value has to be computed. 96 * @return the argument if it is positive, otherwise the negation of the 97 * argument. 98 */ 99 public static int abs(int i) { 100 return i >= 0 ? i : -i; 101 } 102 103 /** 104 * Returns the absolute value of the argument. If the argument is {@code 105 * Long.MIN_VALUE}, {@code Long.MIN_VALUE} is returned. 106 * 107 * @param l 108 * the value whose absolute value has to be computed. 109 * @return the argument if it is positive, otherwise the negation of the 110 * argument. 111 */ 112 public static long abs(long l) { 113 return l >= 0 ? l : -l; 114 } 115 116 /** 117 * Returns the closest double approximation of the arc cosine of the 118 * argument within the range {@code [0..pi]}. The returned result is within 119 * 1 ulp (unit in the last place) of the real result. 120 * <p> 121 * Special cases: 122 * <ul> 123 * <li>{@code acos((anything > 1) = NaN}</li> 124 * <li>{@code acos((anything < -1) = NaN}</li> 125 * <li>{@code acos(NaN) = NaN}</li> 126 * </ul> 127 * 128 * @param d 129 * the value to compute arc cosine of. 130 * @return the arc cosine of the argument. 131 */ 132 public static native double acos(double d); 133 134 /** 135 * Returns the closest double approximation of the arc sine of the argument 136 * within the range {@code [-pi/2..pi/2]}. The returned result is within 1 137 * ulp (unit in the last place) of the real result. 138 * <p> 139 * Special cases: 140 * <ul> 141 * <li>{@code asin((anything > 1)) = NaN}</li> 142 * <li>{@code asin((anything < -1)) = NaN}</li> 143 * <li>{@code asin(NaN) = NaN}</li> 144 * </ul> 145 * 146 * @param d 147 * the value whose arc sine has to be computed. 148 * @return the arc sine of the argument. 149 */ 150 public static native double asin(double d); 151 152 /** 153 * Returns the closest double approximation of the arc tangent of the 154 * argument within the range {@code [-pi/2..pi/2]}. The returned result is 155 * within 1 ulp (unit in the last place) of the real result. 156 * <p> 157 * Special cases: 158 * <ul> 159 * <li>{@code atan(+0.0) = +0.0}</li> 160 * <li>{@code atan(-0.0) = -0.0}</li> 161 * <li>{@code atan(+infinity) = +pi/2}</li> 162 * <li>{@code atan(-infinity) = -pi/2}</li> 163 * <li>{@code atan(NaN) = NaN}</li> 164 * </ul> 165 * 166 * @param d 167 * the value whose arc tangent has to be computed. 168 * @return the arc tangent of the argument. 169 */ 170 public static native double atan(double d); 171 172 /** 173 * Returns the closest double approximation of the arc tangent of {@code 174 * y/x} within the range {@code [-pi..pi]}. This is the angle of the polar 175 * representation of the rectangular coordinates (x,y). The returned result 176 * is within 2 ulps (units in the last place) of the real result. 177 * <p> 178 * Special cases: 179 * <ul> 180 * <li>{@code atan2((anything), NaN ) = NaN;}</li> 181 * <li>{@code atan2(NaN , (anything) ) = NaN;}</li> 182 * <li>{@code atan2(+0.0, +(anything but NaN)) = +0.0}</li> 183 * <li>{@code atan2(-0.0, +(anything but NaN)) = -0.0}</li> 184 * <li>{@code atan2(+0.0, -(anything but NaN)) = +pi}</li> 185 * <li>{@code atan2(-0.0, -(anything but NaN)) = -pi}</li> 186 * <li>{@code atan2(+(anything but 0 and NaN), 0) = +pi/2}</li> 187 * <li>{@code atan2(-(anything but 0 and NaN), 0) = -pi/2}</li> 188 * <li>{@code atan2(+(anything but infinity and NaN), +infinity)} {@code =} 189 * {@code +0.0}</li> 190 * <li>{@code atan2(-(anything but infinity and NaN), +infinity)} {@code =} 191 * {@code -0.0}</li> 192 * <li>{@code atan2(+(anything but infinity and NaN), -infinity) = +pi}</li> 193 * <li>{@code atan2(-(anything but infinity and NaN), -infinity) = -pi}</li> 194 * <li>{@code atan2(+infinity, +infinity ) = +pi/4}</li> 195 * <li>{@code atan2(-infinity, +infinity ) = -pi/4}</li> 196 * <li>{@code atan2(+infinity, -infinity ) = +3pi/4}</li> 197 * <li>{@code atan2(-infinity, -infinity ) = -3pi/4}</li> 198 * <li>{@code atan2(+infinity, (anything but,0, NaN, and infinity))} {@code 199 * =} {@code +pi/2}</li> 200 * <li>{@code atan2(-infinity, (anything but,0, NaN, and infinity))} {@code 201 * =} {@code -pi/2}</li> 202 * </ul> 203 * 204 * @param y 205 * the numerator of the value whose atan has to be computed. 206 * @param x 207 * the denominator of the value whose atan has to be computed. 208 * @return the arc tangent of {@code y/x}. 209 */ 210 public static native double atan2(double x, double y); 211 212 /** 213 * Returns the closest double approximation of the cube root of the 214 * argument. 215 * <p> 216 * Special cases: 217 * <ul> 218 * <li>{@code cbrt(+0.0) = +0.0}</li> 219 * <li>{@code cbrt(-0.0) = -0.0}</li> 220 * <li>{@code cbrt(+infinity) = +infinity}</li> 221 * <li>{@code cbrt(-infinity) = -infinity}</li> 222 * <li>{@code cbrt(NaN) = NaN}</li> 223 * </ul> 224 * 225 * @param d 226 * the value whose cube root has to be computed. 227 * @return the cube root of the argument. 228 */ 229 public static native double cbrt(double d); 230 231 /** 232 * Returns the double conversion of the most negative (closest to negative 233 * infinity) integer value which is greater than the argument. 234 * <p> 235 * Special cases: 236 * <ul> 237 * <li>{@code ceil(+0.0) = +0.0}</li> 238 * <li>{@code ceil(-0.0) = -0.0}</li> 239 * <li>{@code ceil((anything in range (-1,0)) = -0.0}</li> 240 * <li>{@code ceil(+infinity) = +infinity}</li> 241 * <li>{@code ceil(-infinity) = -infinity}</li> 242 * <li>{@code ceil(NaN) = NaN}</li> 243 * </ul> 244 * 245 * @param d 246 * the value whose closest integer value has to be computed. 247 * @return the ceiling of the argument. 248 */ 249 // BEGIN android-changed 250 public static native double ceil(double d); 251 // END android-changed 252 253 /** 254 * Returns the closest double approximation of the cosine of the argument. 255 * The returned result is within 1 ulp (unit in the last place) of the real 256 * result. 257 * <p> 258 * Special cases: 259 * <ul> 260 * <li>{@code cos(+infinity) = NaN}</li> 261 * <li>{@code cos(-infinity) = NaN}</li> 262 * <li>{@code cos(NaN) = NaN}</li> 263 * </ul> 264 * 265 * @param d 266 * the angle whose cosine has to be computed, in radians. 267 * @return the cosine of the argument. 268 */ 269 public static native double cos(double d); 270 271 /** 272 * Returns the closest double approximation of the hyperbolic cosine of the 273 * argument. The returned result is within 2.5 ulps (units in the last 274 * place) of the real result. 275 * <p> 276 * Special cases: 277 * <ul> 278 * <li>{@code cosh(+infinity) = +infinity}</li> 279 * <li>{@code cosh(-infinity) = +infinity}</li> 280 * <li>{@code cosh(NaN) = NaN}</li> 281 * </ul> 282 * 283 * @param d 284 * the value whose hyperbolic cosine has to be computed. 285 * @return the hyperbolic cosine of the argument. 286 */ 287 public static native double cosh(double d); 288 289 /** 290 * Returns the closest double approximation of the raising "e" to the power 291 * of the argument. The returned result is within 1 ulp (unit in the last 292 * place) of the real result. 293 * <p> 294 * Special cases: 295 * <ul> 296 * <li>{@code exp(+infinity) = +infinity}</li> 297 * <li>{@code exp(-infinity) = +0.0}</li> 298 * <li>{@code exp(NaN) = NaN}</li> 299 * </ul> 300 * 301 * @param d 302 * the value whose exponential has to be computed. 303 * @return the exponential of the argument. 304 */ 305 public static native double exp(double d); 306 307 /** 308 * Returns the closest double approximation of <i>{@code e}</i><sup> {@code 309 * d}</sup>{@code - 1}. If the argument is very close to 0, it is much more 310 * accurate to use {@code expm1(d)+1} than {@code exp(d)} (due to 311 * cancellation of significant digits). The returned result is within 1 ulp 312 * (unit in the last place) of the real result. 313 * <p> 314 * For any finite input, the result is not less than -1.0. If the real 315 * result is within 0.5 ulp of -1, -1.0 is returned. 316 * <p> 317 * Special cases: 318 * <ul> 319 * <li>{@code expm1(+0.0) = +0.0}</li> 320 * <li>{@code expm1(-0.0) = -0.0}</li> 321 * <li>{@code expm1(+infinity) = +infinity}</li> 322 * <li>{@code expm1(-infinity) = -1.0}</li> 323 * <li>{@code expm1(NaN) = NaN}</li> 324 * </ul> 325 * 326 * @param d 327 * the value to compute the <i>{@code e}</i><sup>{@code d} </sup> 328 * {@code - 1} of. 329 * @return the <i>{@code e}</i><sup>{@code d}</sup>{@code - 1} value of the 330 * argument. 331 */ 332 public static native double expm1(double d); 333 334 /** 335 * Returns the double conversion of the most positive (closest to positive 336 * infinity) integer value which is less than the argument. 337 * <p> 338 * Special cases: 339 * <ul> 340 * <li>{@code floor(+0.0) = +0.0}</li> 341 * <li>{@code floor(-0.0) = -0.0}</li> 342 * <li>{@code floor(+infinity) = +infinity}</li> 343 * <li>{@code floor(-infinity) = -infinity}</li> 344 * <li>{@code floor(NaN) = NaN}</li> 345 * </ul> 346 * 347 * @param d 348 * the value whose closest integer value has to be computed. 349 * @return the floor of the argument. 350 */ 351 // BEGIN android-changed 352 public static native double floor(double d); 353 // END android-changed 354 355 /** 356 * Returns {@code sqrt(}<i>{@code x}</i><sup>{@code 2}</sup>{@code +} <i> 357 * {@code y}</i><sup>{@code 2}</sup>{@code )}. The final result is without 358 * medium underflow or overflow. The returned result is within 1 ulp (unit 359 * in the last place) of the real result. If one parameter remains constant, 360 * the result should be semi-monotonic. 361 * <p> 362 * Special cases: 363 * <ul> 364 * <li>{@code hypot(+infinity, (anything including NaN)) = +infinity}</li> 365 * <li>{@code hypot(-infinity, (anything including NaN)) = +infinity}</li> 366 * <li>{@code hypot((anything including NaN), +infinity) = +infinity}</li> 367 * <li>{@code hypot((anything including NaN), -infinity) = +infinity}</li> 368 * <li>{@code hypot(NaN, NaN) = NaN}</li> 369 * </ul> 370 * 371 * @param x 372 * a double number. 373 * @param y 374 * a double number. 375 * @return the {@code sqrt(}<i>{@code x}</i><sup>{@code 2}</sup>{@code +} 376 * <i> {@code y}</i><sup>{@code 2}</sup>{@code )} value of the 377 * arguments. 378 */ 379 public static native double hypot(double x, double y); 380 381 /** 382 * Returns the remainder of dividing {@code x} by {@code y} using the IEEE 383 * 754 rules. The result is {@code x-round(x/p)*p} where {@code round(x/p)} 384 * is the nearest integer (rounded to even), but without numerical 385 * cancellation problems. 386 * <p> 387 * Special cases: 388 * <ul> 389 * <li>{@code IEEEremainder((anything), 0) = NaN}</li> 390 * <li>{@code IEEEremainder(+infinity, (anything)) = NaN}</li> 391 * <li>{@code IEEEremainder(-infinity, (anything)) = NaN}</li> 392 * <li>{@code IEEEremainder(NaN, (anything)) = NaN}</li> 393 * <li>{@code IEEEremainder((anything), NaN) = NaN}</li> 394 * <li>{@code IEEEremainder(x, +infinity) = x } where x is anything but 395 * +/-infinity</li> 396 * <li>{@code IEEEremainder(x, -infinity) = x } where x is anything but 397 * +/-infinity</li> 398 * </ul> 399 * 400 * @param x 401 * the numerator of the operation. 402 * @param y 403 * the denominator of the operation. 404 * @return the IEEE754 floating point reminder of of {@code x/y}. 405 */ 406 public static native double IEEEremainder(double x, double y); 407 408 /** 409 * Returns the closest double approximation of the natural logarithm of the 410 * argument. The returned result is within 1 ulp (unit in the last place) of 411 * the real result. 412 * <p> 413 * Special cases: 414 * <ul> 415 * <li>{@code log(+0.0) = -infinity}</li> 416 * <li>{@code log(-0.0) = -infinity}</li> 417 * <li>{@code log((anything < 0) = NaN}</li> 418 * <li>{@code log(+infinity) = +infinity}</li> 419 * <li>{@code log(-infinity) = NaN}</li> 420 * <li>{@code log(NaN) = NaN}</li> 421 * </ul> 422 * 423 * @param d 424 * the value whose log has to be computed. 425 * @return the natural logarithm of the argument. 426 */ 427 public static native double log(double d); 428 429 /** 430 * Returns the closest double approximation of the base 10 logarithm of the 431 * argument. The returned result is within 1 ulp (unit in the last place) of 432 * the real result. 433 * <p> 434 * Special cases: 435 * <ul> 436 * <li>{@code log10(+0.0) = -infinity}</li> 437 * <li>{@code log10(-0.0) = -infinity}</li> 438 * <li>{@code log10((anything < 0) = NaN}</li> 439 * <li>{@code log10(+infinity) = +infinity}</li> 440 * <li>{@code log10(-infinity) = NaN}</li> 441 * <li>{@code log10(NaN) = NaN}</li> 442 * </ul> 443 * 444 * @param d 445 * the value whose base 10 log has to be computed. 446 * @return the natural logarithm of the argument. 447 */ 448 public static native double log10(double d); 449 450 /** 451 * Returns the closest double approximation of the natural logarithm of the 452 * sum of the argument and 1. If the argument is very close to 0, it is much 453 * more accurate to use {@code log1p(d)} than {@code log(1.0+d)} (due to 454 * numerical cancellation). The returned result is within 1 ulp (unit in the 455 * last place) of the real result and is semi-monotonic. 456 * <p> 457 * Special cases: 458 * <ul> 459 * <li>{@code log1p(+0.0) = +0.0}</li> 460 * <li>{@code log1p(-0.0) = -0.0}</li> 461 * <li>{@code log1p((anything < 1)) = NaN}</li> 462 * <li>{@code log1p(-1.0) = -infinity}</li> 463 * <li>{@code log1p(+infinity) = +infinity}</li> 464 * <li>{@code log1p(-infinity) = NaN}</li> 465 * <li>{@code log1p(NaN) = NaN}</li> 466 * </ul> 467 * 468 * @param d 469 * the value to compute the {@code ln(1+d)} of. 470 * @return the natural logarithm of the sum of the argument and 1. 471 */ 472 public static native double log1p(double d); 473 474 /** 475 * Returns the most positive (closest to positive infinity) of the two 476 * arguments. 477 * <p> 478 * Special cases: 479 * <ul> 480 * <li>{@code max(NaN, (anything)) = NaN}</li> 481 * <li>{@code max((anything), NaN) = NaN}</li> 482 * <li>{@code max(+0.0, -0.0) = +0.0}</li> 483 * <li>{@code max(-0.0, +0.0) = +0.0}</li> 484 * </ul> 485 * 486 * @param d1 487 * the first argument. 488 * @param d2 489 * the second argument. 490 * @return the larger of {@code d1} and {@code d2}. 491 */ 492 public static double max(double d1, double d2) { 493 if (d1 > d2) { 494 return d1; 495 } 496 if (d1 < d2) { 497 return d2; 498 } 499 /* if either arg is NaN, return NaN */ 500 if (d1 != d2) { 501 return Double.NaN; 502 } 503 /* max(+0.0,-0.0) == +0.0 */ 504 /* 0 == Double.doubleToRawLongBits(0.0d) */ 505 if (Double.doubleToRawLongBits(d1) != 0) { 506 return d2; 507 } 508 return 0.0d; 509 } 510 511 /** 512 * Returns the most positive (closest to positive infinity) of the two 513 * arguments. 514 * <p> 515 * Special cases: 516 * <ul> 517 * <li>{@code max(NaN, (anything)) = NaN}</li> 518 * <li>{@code max((anything), NaN) = NaN}</li> 519 * <li>{@code max(+0.0, -0.0) = +0.0}</li> 520 * <li>{@code max(-0.0, +0.0) = +0.0}</li> 521 * </ul> 522 * 523 * @param f1 524 * the first argument. 525 * @param f2 526 * the second argument. 527 * @return the larger of {@code f1} and {@code f2}. 528 */ 529 public static float max(float f1, float f2) { 530 if (f1 > f2) { 531 return f1; 532 } 533 if (f1 < f2) { 534 return f2; 535 } 536 /* if either arg is NaN, return NaN */ 537 if (f1 != f2) { 538 return Float.NaN; 539 } 540 /* max(+0.0,-0.0) == +0.0 */ 541 /* 0 == Float.floatToRawIntBits(0.0f) */ 542 if (Float.floatToRawIntBits(f1) != 0) { 543 return f2; 544 } 545 return 0.0f; 546 } 547 548 /** 549 * Returns the most positive (closest to positive infinity) of the two 550 * arguments. 551 * 552 * @param i1 553 * the first argument. 554 * @param i2 555 * the second argument. 556 * @return the larger of {@code i1} and {@code i2}. 557 */ 558 public static int max(int i1, int i2) { 559 return i1 > i2 ? i1 : i2; 560 } 561 562 /** 563 * Returns the most positive (closest to positive infinity) of the two 564 * arguments. 565 * 566 * @param l1 567 * the first argument. 568 * @param l2 569 * the second argument. 570 * @return the larger of {@code l1} and {@code l2}. 571 */ 572 public static long max(long l1, long l2) { 573 return l1 > l2 ? l1 : l2; 574 } 575 576 /** 577 * Returns the most negative (closest to negative infinity) of the two 578 * arguments. 579 * <p> 580 * Special cases: 581 * <ul> 582 * <li>{@code min(NaN, (anything)) = NaN}</li> 583 * <li>{@code min((anything), NaN) = NaN}</li> 584 * <li>{@code min(+0.0, -0.0) = -0.0}</li> 585 * <li>{@code min(-0.0, +0.0) = -0.0}</li> 586 * </ul> 587 * 588 * @param d1 589 * the first argument. 590 * @param d2 591 * the second argument. 592 * @return the smaller of {@code d1} and {@code d2}. 593 */ 594 public static double min(double d1, double d2) { 595 if (d1 > d2) { 596 return d2; 597 } 598 if (d1 < d2) { 599 return d1; 600 } 601 /* if either arg is NaN, return NaN */ 602 if (d1 != d2) { 603 return Double.NaN; 604 } 605 /* min(+0.0,-0.0) == -0.0 */ 606 /* 0x8000000000000000L == Double.doubleToRawLongBits(-0.0d) */ 607 if (Double.doubleToRawLongBits(d1) == 0x8000000000000000L) { 608 return -0.0d; 609 } 610 return d2; 611 } 612 613 /** 614 * Returns the most negative (closest to negative infinity) of the two 615 * arguments. 616 * <p> 617 * Special cases: 618 * <ul> 619 * <li>{@code min(NaN, (anything)) = NaN}</li> 620 * <li>{@code min((anything), NaN) = NaN}</li> 621 * <li>{@code min(+0.0, -0.0) = -0.0}</li> 622 * <li>{@code min(-0.0, +0.0) = -0.0}</li> 623 * </ul> 624 * 625 * @param f1 626 * the first argument. 627 * @param f2 628 * the second argument. 629 * @return the smaller of {@code f1} and {@code f2}. 630 */ 631 public static float min(float f1, float f2) { 632 if (f1 > f2) { 633 return f2; 634 } 635 if (f1 < f2) { 636 return f1; 637 } 638 /* if either arg is NaN, return NaN */ 639 if (f1 != f2) { 640 return Float.NaN; 641 } 642 /* min(+0.0,-0.0) == -0.0 */ 643 /* 0x80000000 == Float.floatToRawIntBits(-0.0f) */ 644 if (Float.floatToRawIntBits(f1) == 0x80000000) { 645 return -0.0f; 646 } 647 return f2; 648 } 649 650 /** 651 * Returns the most negative (closest to negative infinity) of the two 652 * arguments. 653 * 654 * @param i1 655 * the first argument. 656 * @param i2 657 * the second argument. 658 * @return the smaller of {@code i1} and {@code i2}. 659 */ 660 public static int min(int i1, int i2) { 661 return i1 < i2 ? i1 : i2; 662 } 663 664 /** 665 * Returns the most negative (closest to negative infinity) of the two 666 * arguments. 667 * 668 * @param l1 669 * the first argument. 670 * @param l2 671 * the second argument. 672 * @return the smaller of {@code l1} and {@code l2}. 673 */ 674 public static long min(long l1, long l2) { 675 return l1 < l2 ? l1 : l2; 676 } 677 678 /** 679 * Returns the closest double approximation of the result of raising {@code 680 * x} to the power of {@code y}. 681 * <p> 682 * Special cases: 683 * <ul> 684 * <li>{@code pow((anything), +0.0) = 1.0}</li> 685 * <li>{@code pow((anything), -0.0) = 1.0}</li> 686 * <li>{@code pow(x, 1.0) = x}</li> 687 * <li>{@code pow((anything), NaN) = NaN}</li> 688 * <li>{@code pow(NaN, (anything except 0)) = NaN}</li> 689 * <li>{@code pow(+/-(|x| > 1), +infinity) = +infinity}</li> 690 * <li>{@code pow(+/-(|x| > 1), -infinity) = +0.0}</li> 691 * <li>{@code pow(+/-(|x| < 1), +infinity) = +0.0}</li> 692 * <li>{@code pow(+/-(|x| < 1), -infinity) = +infinity}</li> 693 * <li>{@code pow(+/-1.0 , +infinity) = NaN}</li> 694 * <li>{@code pow(+/-1.0 , -infinity) = NaN}</li> 695 * <li>{@code pow(+0.0, (+anything except 0, NaN)) = +0.0}</li> 696 * <li>{@code pow(-0.0, (+anything except 0, NaN, odd integer)) = +0.0}</li> 697 * <li>{@code pow(+0.0, (-anything except 0, NaN)) = +infinity}</li> 698 * <li>{@code pow(-0.0, (-anything except 0, NAN, odd integer))} {@code =} 699 * {@code +infinity}</li> 700 * <li>{@code pow(-0.0, (odd integer)) = -pow( +0 , (odd integer) )}</li> 701 * <li>{@code pow(+infinity, (+anything except 0, NaN)) = +infinity}</li> 702 * <li>{@code pow(+infinity, (-anything except 0, NaN)) = +0.0}</li> 703 * <li>{@code pow(-infinity, (anything)) = -pow(0, (-anything))}</li> 704 * <li>{@code pow((-anything), (integer))} {@code =} {@code 705 * pow(-1,(integer))*pow(+anything,integer) }</li> 706 * <li>{@code pow((-anything except 0 and inf), (non-integer)) = 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 the double conversion of the result of rounding the argument to 719 * an integer. Tie breaks are rounded towards even. 720 * <p> 721 * Special cases: 722 * <ul> 723 * <li>{@code rint(+0.0) = +0.0}</li> 724 * <li>{@code rint(-0.0) = -0.0}</li> 725 * <li>{@code rint(+infinity) = +infinity}</li> 726 * <li>{@code rint(-infinity) = -infinity}</li> 727 * <li>{@code rint(NaN) = NaN}</li> 728 * </ul> 729 * 730 * @param d 731 * the value to be rounded. 732 * @return the closest integer to the argument (as a double). 733 */ 734 // BEGIN android-changed 735 public static native double rint(double d); 736 // END android-changed 737 738 /** 739 * Returns the result of rounding the argument to an integer. The result is 740 * equivalent to {@code (long) Math.floor(d+0.5)}. 741 * <p> 742 * Special cases: 743 * <ul> 744 * <li>{@code round(+0.0) = +0.0}</li> 745 * <li>{@code round(-0.0) = +0.0}</li> 746 * <li>{@code round((anything > Long.MAX_VALUE) = Long.MAX_VALUE}</li> 747 * <li>{@code round((anything < Long.MIN_VALUE) = Long.MIN_VALUE}</li> 748 * <li>{@code round(+infintiy) = Long.MAX_VALUE}</li> 749 * <li>{@code round(-infintiy) = Long.MIN_VALUE}</li> 750 * <li>{@code round(NaN) = +0.0}</li> 751 * </ul> 752 * 753 * @param d 754 * the value to be rounded. 755 * @return the closest integer to the argument. 756 */ 757 public static long round(double d) { 758 // check for NaN 759 if (d != d) { 760 return 0L; 761 } 762 return (long) floor(d + 0.5d); 763 } 764 765 /** 766 * Returns the result of rounding the argument to an integer. The result is 767 * equivalent to {@code (int) Math.floor(f+0.5)}. 768 * <p> 769 * Special cases: 770 * <ul> 771 * <li>{@code round(+0.0) = +0.0}</li> 772 * <li>{@code round(-0.0) = +0.0}</li> 773 * <li>{@code round((anything > Integer.MAX_VALUE) = Integer.MAX_VALUE}</li> 774 * <li>{@code round((anything < Integer.MIN_VALUE) = Integer.MIN_VALUE}</li> 775 * <li>{@code round(+infintiy) = Integer.MAX_VALUE}</li> 776 * <li>{@code round(-infintiy) = Integer.MIN_VALUE}</li> 777 * <li>{@code round(NaN) = +0.0}</li> 778 * </ul> 779 * 780 * @param f 781 * the value to be rounded. 782 * @return the closest integer to the argument. 783 */ 784 public static int round(float f) { 785 // check for NaN 786 if (f != f) { 787 return 0; 788 } 789 return (int) floor(f + 0.5f); 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 StrictMath.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 StrictMath.signum(f); 836 } 837 838 /** 839 * Returns the closest double approximation of the sine of the argument. The 840 * returned result is within 1 ulp (unit in the last place) of the real 841 * result. 842 * <p> 843 * Special cases: 844 * <ul> 845 * <li>{@code sin(+0.0) = +0.0}</li> 846 * <li>{@code sin(-0.0) = -0.0}</li> 847 * <li>{@code sin(+infinity) = NaN}</li> 848 * <li>{@code sin(-infinity) = NaN}</li> 849 * <li>{@code sin(NaN) = NaN}</li> 850 * </ul> 851 * 852 * @param d 853 * the angle whose sin has to be computed, in radians. 854 * @return the sine of the argument. 855 */ 856 public static native double sin(double d); 857 858 /** 859 * Returns the closest double approximation of the hyperbolic sine of the 860 * argument. The returned result is within 2.5 ulps (units in the last 861 * place) of the real result. 862 * <p> 863 * Special cases: 864 * <ul> 865 * <li>{@code sinh(+0.0) = +0.0}</li> 866 * <li>{@code sinh(-0.0) = -0.0}</li> 867 * <li>{@code sinh(+infinity) = +infinity}</li> 868 * <li>{@code sinh(-infinity) = -infinity}</li> 869 * <li>{@code sinh(NaN) = NaN}</li> 870 * </ul> 871 * 872 * @param d 873 * the value whose hyperbolic sine has to be computed. 874 * @return the hyperbolic sine of the argument. 875 */ 876 public static native double sinh(double d); 877 878 /** 879 * Returns the closest double approximation of the square root of the 880 * argument. 881 * <p> 882 * Special cases: 883 * <ul> 884 * <li>{@code sqrt(+0.0) = +0.0}</li> 885 * <li>{@code sqrt(-0.0) = -0.0}</li> 886 * <li>{@code sqrt( (anything < 0) ) = NaN}</li> 887 * <li>{@code sqrt(+infinity) = +infinity}</li> 888 * <li>{@code sqrt(NaN) = NaN}</li> 889 * </ul> 890 * 891 * @param d 892 * the value whose square root has to be computed. 893 * @return the square root of the argument. 894 */ 895 public static native double sqrt(double d); 896 897 /** 898 * Returns the closest double approximation of the tangent of the argument. 899 * The returned result is within 1 ulp (unit in the last place) of the real 900 * result. 901 * <p> 902 * Special cases: 903 * <ul> 904 * <li>{@code tan(+0.0) = +0.0}</li> 905 * <li>{@code tan(-0.0) = -0.0}</li> 906 * <li>{@code tan(+infinity) = NaN}</li> 907 * <li>{@code tan(-infinity) = NaN}</li> 908 * <li>{@code tan(NaN) = NaN}</li> 909 * </ul> 910 * 911 * @param d 912 * the angle whose tangens has to be computed, in radians. 913 * @return the tangent of the argument. 914 */ 915 public static native double tan(double d); 916 917 /** 918 * Returns the closest double approximation of the hyperbolic tangent of the 919 * argument. The absolute value is always less than 1. The returned result 920 * is within 2.5 ulps (units in the last place) of the real result. If the 921 * real result is within 0.5ulp of 1 or -1, it should return exactly +1 or 922 * -1. 923 * <p> 924 * Special cases: 925 * <ul> 926 * <li>{@code tanh(+0.0) = +0.0}</li> 927 * <li>{@code tanh(-0.0) = -0.0}</li> 928 * <li>{@code tanh(+infinity) = +1.0}</li> 929 * <li>{@code tanh(-infinity) = -1.0}</li> 930 * <li>{@code tanh(NaN) = NaN}</li> 931 * </ul> 932 * 933 * @param d 934 * the value whose hyperbolic tangent has to be computed. 935 * @return the hyperbolic tangent of the argument. 936 */ 937 public static native double tanh(double d); 938 939 /** 940 * Returns a pseudo-random number between 0.0 (inclusive) and 1.0 941 * (exclusive). 942 * 943 * @return a pseudo-random number. 944 */ 945 public static double random() { 946 if (random == null) { 947 random = new java.util.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 */ 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 */ 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(+infintiy) = infinity}</li> 1005 * <li>{@code ulp(-infintiy) = 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 */ 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 1024 /** 1025 * Returns the argument's ulp (unit in the last place). The size of a ulp of 1026 * a float value is the positive distance between this value and the float 1027 * value next larger in magnitude. For non-NaN {@code x}, {@code ulp(-x) == 1028 * ulp(x)}. 1029 * <p> 1030 * Special cases: 1031 * <ul> 1032 * <li>{@code ulp(+0.0) = Float.MIN_VALUE}</li> 1033 * <li>{@code ulp(-0.0) = Float.MIN_VALUE}</li> 1034 * <li>{@code ulp(+infintiy) = infinity}</li> 1035 * <li>{@code ulp(-infintiy) = infinity}</li> 1036 * <li>{@code ulp(NaN) = NaN}</li> 1037 * </ul> 1038 * 1039 * @param f 1040 * the floating-point value to compute ulp of. 1041 * @return the size of a ulp of the argument. 1042 */ 1043 public static float ulp(float f) { 1044 // special cases 1045 if (Float.isNaN(f)) { 1046 return Float.NaN; 1047 } else if (Float.isInfinite(f)) { 1048 return Float.POSITIVE_INFINITY; 1049 } else if (f == Float.MAX_VALUE || f == -Float.MAX_VALUE) { 1050 return (float) pow(2, 104); 1051 } 1052 f = abs(f); 1053 return nextafterf(f, Float.MAX_VALUE) - f; 1054 } 1055 1056 private native static double nextafter(double x, double y); 1057 1058 private native static float nextafterf(float x, float y); 1059} 1060