StrictMath.java revision f5597e626ecf7949d249dea08c1a2964d890ec11
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// BEGIN android-removed 21// import java.util.Random; 22// END android-removed 23 24/** 25 * Class StrictMath provides basic math constants and operations such as 26 * trigonometric functions, hyperbolic functions, exponential, logarithms, etc. 27 * <p> 28 * In contrast to class {@link Math}, the methods in this class return exactly 29 * the same results on all platforms. Algorithms based on these methods thus 30 * behave the same (e.g. regarding numerical convergence) on all platforms, 31 * complying with the slogan "write once, run everywhere". On the other side, 32 * the implementation of class StrictMath may be less efficient than that of 33 * class Math, as class StrictMath cannot utilize platform specific features 34 * such as an extended precision math co-processors. 35 * <p> 36 * The methods in this class are specified using the "Freely Distributable Math 37 * Library" (fdlibm), version 5.3. 38 * <p> 39 * <a href="http://www.netlib.org/fdlibm/">http://www.netlib.org/fdlibm/</a> 40 */ 41public final class StrictMath { 42 43 /** 44 * The double value closest to e, the base of the natural logarithm. 45 */ 46 public final static double E = Math.E; 47 48 /** 49 * The double value closest to pi, the ratio of a circle's circumference to 50 * its diameter. 51 */ 52 public final static double PI = Math.PI; 53 54 private static java.util.Random random; 55 56 /** 57 * Prevents this class from being instantiated. 58 */ 59 private StrictMath() { 60 } 61 62 /** 63 * Returns the absolute value of the argument. 64 * <p> 65 * Special cases: 66 * <ul> 67 * <li>{@code abs(-0.0) = +0.0}</li> 68 * <li>{@code abs(+infinity) = +infinity}</li> 69 * <li>{@code abs(-infinity) = +infinity}</li> 70 * <li>{@code abs(NaN) = NaN}</li> 71 * </ul> 72 * 73 * @param d 74 * the value whose absolute value has to be computed. 75 * @return the absolute value of the argument. 76 */ 77 public static double abs(double d) { 78 long bits = Double.doubleToLongBits(d); 79 bits &= 0x7fffffffffffffffL; 80 return Double.longBitsToDouble(bits); 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 f 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 float abs(float f) { 100 int bits = Float.floatToIntBits(f); 101 bits &= 0x7fffffff; 102 return Float.intBitsToFloat(bits); 103 } 104 105 /** 106 * Returns the absolute value of the argument. 107 * <p> 108 * If the argument is {@code Integer.MIN_VALUE}, {@code Integer.MIN_VALUE} 109 * is returned. 110 * 111 * @param i 112 * the value whose absolute value has to be computed. 113 * @return the argument if it is positive, otherwise the negation of the 114 * argument. 115 */ 116 public static int abs(int i) { 117 return i >= 0 ? i : -i; 118 } 119 120 /** 121 * Returns the absolute value of the argument. 122 * <p> 123 * If the argument is {@code Long.MIN_VALUE}, {@code Long.MIN_VALUE} is 124 * returned. 125 * 126 * @param l 127 * the value whose absolute value has to be computed. 128 * @return the argument if it is positive, otherwise the negation of the 129 * argument. 130 */ 131 public static long abs(long l) { 132 return l >= 0 ? l : -l; 133 } 134 135 /** 136 * Returns the closest double approximation of the arc cosine of the 137 * argument within the range {@code [0..pi]}. 138 * <p> 139 * Special cases: 140 * <ul> 141 * <li>{@code acos((anything > 1) = NaN}</li> 142 * <li>{@code acos((anything < -1) = NaN}</li> 143 * <li>{@code acos(NaN) = NaN}</li> 144 * </ul> 145 * 146 * @param d 147 * the value to compute arc cosine of. 148 * @return the arc cosine of the argument. 149 */ 150 public static native double acos(double d); 151 152 /** 153 * Returns the closest double approximation of the arc sine of the argument 154 * within the range {@code [-pi/2..pi/2]}. 155 * <p> 156 * Special cases: 157 * <ul> 158 * <li>{@code asin((anything > 1)) = NaN}</li> 159 * <li>{@code asin((anything < -1)) = NaN}</li> 160 * <li>{@code asin(NaN) = NaN}</li> 161 * </ul> 162 * 163 * @param d 164 * the value whose arc sine has to be computed. 165 * @return the arc sine of the argument. 166 */ 167 public static native double asin(double d); 168 169 /** 170 * Returns the closest double approximation of the arc tangent of the 171 * argument within the range {@code [-pi/2..pi/2]}. 172 * <p> 173 * Special cases: 174 * <ul> 175 * <li>{@code atan(+0.0) = +0.0}</li> 176 * <li>{@code atan(-0.0) = -0.0}</li> 177 * <li>{@code atan(+infinity) = +pi/2}</li> 178 * <li>{@code atan(-infinity) = -pi/2}</li> 179 * <li>{@code atan(NaN) = NaN}</li> 180 * </ul> 181 * 182 * @param d 183 * the value whose arc tangent has to be computed. 184 * @return the arc tangent of the argument. 185 */ 186 public static native double atan(double d); 187 188 /** 189 * Returns the closest double approximation of the arc tangent of 190 * {@code y/x} within the range {@code [-pi..pi]}. This is the angle of the 191 * polar representation of the rectangular coordinates (x,y). 192 * <p> 193 * Special cases: 194 * <ul> 195 * <li>{@code atan2((anything), NaN ) = NaN;}</li> 196 * <li>{@code atan2(NaN , (anything) ) = NaN;}</li> 197 * <li>{@code atan2(+0.0, +(anything but NaN)) = +0.0}</li> 198 * <li>{@code atan2(-0.0, +(anything but NaN)) = -0.0}</li> 199 * <li>{@code atan2(+0.0, -(anything but NaN)) = +pi}</li> 200 * <li>{@code atan2(-0.0, -(anything but NaN)) = -pi}</li> 201 * <li>{@code atan2(+(anything but 0 and NaN), 0) = +pi/2}</li> 202 * <li>{@code atan2(-(anything but 0 and NaN), 0) = -pi/2}</li> 203 * <li>{@code atan2(+(anything but infinity and NaN), +infinity)} {@code =} 204 * {@code +0.0}</li> 205 * <li>{@code atan2(-(anything but infinity and NaN), +infinity)} {@code =} 206 * {@code -0.0}</li> 207 * <li>{@code atan2(+(anything but infinity and NaN), -infinity) = +pi}</li> 208 * <li>{@code atan2(-(anything but infinity and NaN), -infinity) = -pi}</li> 209 * <li>{@code atan2(+infinity, +infinity ) = +pi/4}</li> 210 * <li>{@code atan2(-infinity, +infinity ) = -pi/4}</li> 211 * <li>{@code atan2(+infinity, -infinity ) = +3pi/4}</li> 212 * <li>{@code atan2(-infinity, -infinity ) = -3pi/4}</li> 213 * <li>{@code atan2(+infinity, (anything but,0, NaN, and infinity))} 214 * {@code =} {@code +pi/2}</li> 215 * <li>{@code atan2(-infinity, (anything but,0, NaN, and infinity))} 216 * {@code =} {@code -pi/2}</li> 217 * </ul> 218 * 219 * @param y 220 * the numerator of the value whose atan has to be computed. 221 * @param x 222 * the denominator of the value whose atan has to be computed. 223 * @return the arc tangent of {@code y/x}. 224 */ 225 public static native double atan2(double y, double x); 226 227 /** 228 * Returns the closest double approximation of the cube root of the 229 * argument. 230 * <p> 231 * Special cases: 232 * <ul> 233 * <li>{@code cbrt(+0.0) = +0.0}</li> 234 * <li>{@code cbrt(-0.0) = -0.0}</li> 235 * <li>{@code cbrt(+infinity) = +infinity}</li> 236 * <li>{@code cbrt(-infinity) = -infinity}</li> 237 * <li>{@code cbrt(NaN) = NaN}</li> 238 * </ul> 239 * 240 * @param d 241 * the value whose cube root has to be computed. 242 * @return the cube root of the argument. 243 */ 244 public static native double cbrt(double d); 245 246 /** 247 * Returns the double conversion of the most negative (closest to negative 248 * infinity) integer value which is greater than the argument. 249 * <p> 250 * Special cases: 251 * <ul> 252 * <li>{@code ceil(+0.0) = +0.0}</li> 253 * <li>{@code ceil(-0.0) = -0.0}</li> 254 * <li>{@code ceil((anything in range (-1,0)) = -0.0}</li> 255 * <li>{@code ceil(+infinity) = +infinity}</li> 256 * <li>{@code ceil(-infinity) = -infinity}</li> 257 * <li>{@code ceil(NaN) = NaN}</li> 258 * </ul> 259 * 260 * @param d 261 * the value whose closest integer value has to be computed. 262 * @return the ceiling of the argument. 263 */ 264 public static native double ceil(double d); 265 266 267 /** 268 * Returns the closest double approximation of the hyperbolic cosine of the 269 * argument. 270 * <p> 271 * Special cases: 272 * <ul> 273 * <li>{@code cosh(+infinity) = +infinity}</li> 274 * <li>{@code cosh(-infinity) = +infinity}</li> 275 * <li>{@code cosh(NaN) = NaN}</li> 276 * </ul> 277 * 278 * @param d 279 * the value whose hyperbolic cosine has to be computed. 280 * @return the hyperbolic cosine of the argument. 281 */ 282 public static native double cosh(double d); 283 284 /** 285 * Returns the closest double approximation of the cosine of the argument. 286 * <p> 287 * Special cases: 288 * <ul> 289 * <li>{@code cos(+infinity) = NaN}</li> 290 * <li>{@code cos(-infinity) = NaN}</li> 291 * <li>{@code cos(NaN) = NaN}</li> 292 * </ul> 293 * 294 * @param d 295 * the angle whose cosine has to be computed, in radians. 296 * @return the cosine of the argument. 297 */ 298 public static native double cos(double d); 299 300 /** 301 * Returns the closest double approximation of the raising "e" to the power 302 * of the argument. 303 * <p> 304 * Special cases: 305 * <ul> 306 * <li>{@code exp(+infinity) = +infinity}</li> 307 * <li>{@code exp(-infinity) = +0.0}</li> 308 * <li>{@code exp(NaN) = NaN}</li> 309 * </ul> 310 * 311 * @param d 312 * the value whose exponential has to be computed. 313 * @return the exponential of the argument. 314 */ 315 public static native double exp(double d); 316 317 /** 318 * Returns the closest double approximation of <i>{@code e}</i><sup> 319 * {@code d}</sup>{@code - 1}. If the argument is very close to 0, it is 320 * much more accurate to use {@code expm1(d)+1} than {@code exp(d)} (due to 321 * cancellation of significant digits). 322 * <p> 323 * Special cases: 324 * <ul> 325 * <li>{@code expm1(+0.0) = +0.0}</li> 326 * <li>{@code expm1(-0.0) = -0.0}</li> 327 * <li>{@code expm1(+infinity) = +infinity}</li> 328 * <li>{@code expm1(-infinity) = -1.0}</li> 329 * <li>{@code expm1(NaN) = NaN}</li> 330 * </ul> 331 * 332 * @param d 333 * the value to compute the <i>{@code e}</i><sup>{@code d}</sup> 334 * {@code - 1} of. 335 * @return the <i>{@code e}</i><sup>{@code d}</sup>{@code - 1} value 336 * of the argument. 337 */ 338 public static native double expm1(double d); 339 340 /** 341 * Returns the double conversion of the most positive (closest to 342 * positive infinity) integer value which is less than the argument. 343 * <p> 344 * Special cases: 345 * <ul> 346 * <li>{@code floor(+0.0) = +0.0}</li> 347 * <li>{@code floor(-0.0) = -0.0}</li> 348 * <li>{@code floor(+infinity) = +infinity}</li> 349 * <li>{@code floor(-infinity) = -infinity}</li> 350 * <li>{@code floor(NaN) = NaN}</li> 351 * </ul> 352 * 353 * @param d the value whose closest integer value has to be computed. 354 * @return the floor of the argument. 355 */ 356 public static native double floor(double d); 357 358 /** 359 * Returns {@code sqrt(}<i>{@code x}</i><sup>{@code 2}</sup>{@code +} 360 * <i> {@code y}</i><sup>{@code 2}</sup>{@code )}. The final result is 361 * without medium underflow or overflow. 362 * <p> 363 * Special cases: 364 * <ul> 365 * <li>{@code hypot(+infinity, (anything including NaN)) = +infinity}</li> 366 * <li>{@code hypot(-infinity, (anything including NaN)) = +infinity}</li> 367 * <li>{@code hypot((anything including NaN), +infinity) = +infinity}</li> 368 * <li>{@code hypot((anything including NaN), -infinity) = +infinity}</li> 369 * <li>{@code hypot(NaN, NaN) = NaN}</li> 370 * </ul> 371 * 372 * @param x 373 * a double number. 374 * @param y 375 * a double number. 376 * @return the {@code sqrt(}<i>{@code x}</i><sup>{@code 2}</sup>{@code +} 377 * <i> {@code y}</i><sup>{@code 2}</sup>{@code )} value of the 378 * arguments. 379 */ 380 public static native double hypot(double x, double y); 381 382 /** 383 * Returns the remainder of dividing {@code x} by {@code y} using the IEEE 384 * 754 rules. The result is {@code x-round(x/p)*p} where {@code round(x/p)} 385 * is the nearest integer (rounded to even), but without numerical 386 * cancellation problems. 387 * <p> 388 * Special cases: 389 * <ul> 390 * <li>{@code IEEEremainder((anything), 0) = NaN}</li> 391 * <li>{@code IEEEremainder(+infinity, (anything)) = NaN}</li> 392 * <li>{@code IEEEremainder(-infinity, (anything)) = NaN}</li> 393 * <li>{@code IEEEremainder(NaN, (anything)) = NaN}</li> 394 * <li>{@code IEEEremainder((anything), NaN) = NaN}</li> 395 * <li>{@code IEEEremainder(x, +infinity) = x } where x is anything but 396 * +/-infinity</li> 397 * <li>{@code IEEEremainder(x, -infinity) = x } where x is anything but 398 * +/-infinity</li> 399 * </ul> 400 * 401 * @param x 402 * the numerator of the operation. 403 * @param y 404 * the denominator of the operation. 405 * @return the IEEE754 floating point reminder of of {@code x/y}. 406 */ 407 public static native double IEEEremainder(double x, double y); 408 409 /** 410 * Returns the closest double approximation of the natural logarithm of the 411 * argument. 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. 432 * <p> 433 * Special cases: 434 * <ul> 435 * <li>{@code log10(+0.0) = -infinity}</li> 436 * <li>{@code log10(-0.0) = -infinity}</li> 437 * <li>{@code log10((anything < 0) = NaN}</li> 438 * <li>{@code log10(+infinity) = +infinity}</li> 439 * <li>{@code log10(-infinity) = NaN}</li> 440 * <li>{@code log10(NaN) = NaN}</li> 441 * </ul> 442 * 443 * @param d 444 * the value whose base 10 log has to be computed. 445 * @return the natural logarithm of the argument. 446 */ 447 public static native double log10(double d); 448 449 /** 450 * Returns the closest double approximation of the natural logarithm of the 451 * sum of the argument and 1. If the argument is very close to 0, it is much 452 * more accurate to use {@code log1p(d)} than {@code log(1.0+d)} (due to 453 * numerical cancellation). 454 * <p> 455 * Special cases: 456 * <ul> 457 * <li>{@code log1p(+0.0) = +0.0}</li> 458 * <li>{@code log1p(-0.0) = -0.0}</li> 459 * <li>{@code log1p((anything < 1)) = NaN}</li> 460 * <li>{@code log1p(-1.0) = -infinity}</li> 461 * <li>{@code log1p(+infinity) = +infinity}</li> 462 * <li>{@code log1p(-infinity) = NaN}</li> 463 * <li>{@code log1p(NaN) = NaN}</li> 464 * </ul> 465 * 466 * @param d 467 * the value to compute the {@code ln(1+d)} of. 468 * @return the natural logarithm of the sum of the argument and 1. 469 */ 470 public static native double log1p(double d); 471 472 /** 473 * Returns the most positive (closest to positive infinity) of the two 474 * arguments. 475 * <p> 476 * Special cases: 477 * <ul> 478 * <li>{@code max(NaN, (anything)) = NaN}</li> 479 * <li>{@code max((anything), NaN) = NaN}</li> 480 * <li>{@code max(+0.0, -0.0) = +0.0}</li> 481 * <li>{@code max(-0.0, +0.0) = +0.0}</li> 482 * </ul> 483 * 484 * @param d1 485 * the first argument. 486 * @param d2 487 * the second argument. 488 * @return the larger of {@code d1} and {@code d2}. 489 */ 490 public static double max(double d1, double d2) { 491 if (d1 > d2) 492 return d1; 493 if (d1 < d2) 494 return d2; 495 /* if either arg is NaN, return NaN */ 496 if (d1 != d2) 497 return Double.NaN; 498 /* max( +0.0,-0.0) == +0.0 */ 499 if (d1 == 0.0 500 && ((Double.doubleToLongBits(d1) & Double.doubleToLongBits(d2)) & 0x8000000000000000L) == 0) 501 return 0.0; 502 return d1; 503 } 504 505 /** 506 * Returns the most positive (closest to positive infinity) of the two 507 * arguments. 508 * <p> 509 * Special cases: 510 * <ul> 511 * <li>{@code max(NaN, (anything)) = NaN}</li> 512 * <li>{@code max((anything), NaN) = NaN}</li> 513 * <li>{@code max(+0.0, -0.0) = +0.0}</li> 514 * <li>{@code max(-0.0, +0.0) = +0.0}</li> 515 * </ul> 516 * 517 * @param f1 518 * the first argument. 519 * @param f2 520 * the second argument. 521 * @return the larger of {@code f1} and {@code f2}. 522 */ 523 public static float max(float f1, float f2) { 524 if (f1 > f2) 525 return f1; 526 if (f1 < f2) 527 return f2; 528 /* if either arg is NaN, return NaN */ 529 if (f1 != f2) 530 return Float.NaN; 531 /* max( +0.0,-0.0) == +0.0 */ 532 if (f1 == 0.0f 533 && ((Float.floatToIntBits(f1) & Float.floatToIntBits(f2)) & 0x80000000) == 0) 534 return 0.0f; 535 return f1; 536 } 537 538 /** 539 * Returns the most positive (closest to positive infinity) of the two 540 * arguments. 541 * 542 * @param i1 543 * the first argument. 544 * @param i2 545 * the second argument. 546 * @return the larger of {@code i1} and {@code i2}. 547 */ 548 public static int max(int i1, int i2) { 549 return i1 > i2 ? i1 : i2; 550 } 551 552 /** 553 * Returns the most positive (closest to positive infinity) of the two 554 * arguments. 555 * 556 * @param l1 557 * the first argument. 558 * @param l2 559 * the second argument. 560 * @return the larger of {@code l1} and {@code l2}. 561 */ 562 public static long max(long l1, long l2) { 563 return l1 > l2 ? l1 : l2; 564 } 565 566 /** 567 * Returns the most negative (closest to negative infinity) of the two 568 * arguments. 569 * <p> 570 * Special cases: 571 * <ul> 572 * <li>{@code min(NaN, (anything)) = NaN}</li> 573 * <li>{@code min((anything), NaN) = NaN}</li> 574 * <li>{@code min(+0.0, -0.0) = -0.0}</li> 575 * <li>{@code min(-0.0, +0.0) = -0.0}</li> 576 * </ul> 577 * 578 * @param d1 579 * the first argument. 580 * @param d2 581 * the second argument. 582 * @return the smaller of {@code d1} and {@code d2}. 583 */ 584 public static double min(double d1, double d2) { 585 if (d1 > d2) 586 return d2; 587 if (d1 < d2) 588 return d1; 589 /* if either arg is NaN, return NaN */ 590 if (d1 != d2) 591 return Double.NaN; 592 /* min( +0.0,-0.0) == -0.0 */ 593 if (d1 == 0.0 594 && ((Double.doubleToLongBits(d1) | Double.doubleToLongBits(d2)) & 0x8000000000000000l) != 0) 595 return 0.0 * (-1.0); 596 return d1; 597 } 598 599 /** 600 * Returns the most negative (closest to negative infinity) of the two 601 * arguments. 602 * <p> 603 * Special cases: 604 * <ul> 605 * <li>{@code min(NaN, (anything)) = NaN}</li> 606 * <li>{@code min((anything), NaN) = NaN}</li> 607 * <li>{@code min(+0.0, -0.0) = -0.0}</li> 608 * <li>{@code min(-0.0, +0.0) = -0.0}</li> 609 * </ul> 610 * 611 * @param f1 612 * the first argument. 613 * @param f2 614 * the second argument. 615 * @return the smaller of {@code f1} and {@code f2}. 616 */ 617 public static float min(float f1, float f2) { 618 if (f1 > f2) 619 return f2; 620 if (f1 < f2) 621 return f1; 622 /* if either arg is NaN, return NaN */ 623 if (f1 != f2) 624 return Float.NaN; 625 /* min( +0.0,-0.0) == -0.0 */ 626 if (f1 == 0.0f 627 && ((Float.floatToIntBits(f1) | Float.floatToIntBits(f2)) & 0x80000000) != 0) 628 return 0.0f * (-1.0f); 629 return f1; 630 } 631 632 /** 633 * Returns the most negative (closest to negative infinity) of the two 634 * arguments. 635 * 636 * @param i1 637 * the first argument. 638 * @param i2 639 * the second argument. 640 * @return the smaller of {@code i1} and {@code i2}. 641 */ 642 public static int min(int i1, int i2) { 643 return i1 < i2 ? i1 : i2; 644 } 645 646 /** 647 * Returns the most negative (closest to negative infinity) of the two 648 * arguments. 649 * 650 * @param l1 651 * the first argument. 652 * @param l2 653 * the second argument. 654 * @return the smaller of {@code l1} and {@code l2}. 655 */ 656 public static long min(long l1, long l2) { 657 return l1 < l2 ? l1 : l2; 658 } 659 660 /** 661 * Returns the closest double approximation of the result of raising 662 * {@code x} to the power of {@code y}. 663 * <p> 664 * Special cases: 665 * <ul> 666 * <li>{@code pow((anything), +0.0) = 1.0}</li> 667 * <li>{@code pow((anything), -0.0) = 1.0}</li> 668 * <li>{@code pow(x, 1.0) = x}</li> 669 * <li>{@code pow((anything), NaN) = NaN}</li> 670 * <li>{@code pow(NaN, (anything except 0)) = NaN}</li> 671 * <li>{@code pow(+/-(|x| > 1), +infinity) = +infinity}</li> 672 * <li>{@code pow(+/-(|x| > 1), -infinity) = +0.0}</li> 673 * <li>{@code pow(+/-(|x| < 1), +infinity) = +0.0}</li> 674 * <li>{@code pow(+/-(|x| < 1), -infinity) = +infinity}</li> 675 * <li>{@code pow(+/-1.0 , +infinity) = NaN}</li> 676 * <li>{@code pow(+/-1.0 , -infinity) = NaN}</li> 677 * <li>{@code pow(+0.0, (+anything except 0, NaN)) = +0.0}</li> 678 * <li>{@code pow(-0.0, (+anything except 0, NaN, odd integer)) = +0.0}</li> 679 * <li>{@code pow(+0.0, (-anything except 0, NaN)) = +infinity}</li> 680 * <li>{@code pow(-0.0, (-anything except 0, NAN, odd integer))} {@code =} 681 * {@code +infinity}</li> 682 * <li>{@code pow(-0.0, (odd integer)) = -pow( +0 , (odd integer) )}</li> 683 * <li>{@code pow(+infinity, (+anything except 0, NaN)) = +infinity}</li> 684 * <li>{@code pow(+infinity, (-anything except 0, NaN)) = +0.0}</li> 685 * <li>{@code pow(-infinity, (anything)) = -pow(0, (-anything))}</li> 686 * <li>{@code pow((-anything), (integer))} {@code =} 687 * {@code pow(-1,(integer))*pow(+anything,integer)}</li> 688 * <li>{@code pow((-anything except 0 and infinity), (non-integer))} 689 * {@code =} {@code NAN}</li> 690 * </ul> 691 * 692 * @param x 693 * the base of the operation. 694 * @param y 695 * the exponent of the operation. 696 * @return {@code x} to the power of {@code y}. 697 */ 698 public static native double pow(double x, double y); 699 700 /** 701 * Returns a pseudo-random number between 0.0 (inclusive) and 1.0 702 * (exclusive). 703 * 704 * @return a pseudo-random number. 705 */ 706 public static double random() { 707 // BEGIN android-changed 708 if (random == null) { 709 random = new java.util.Random(); 710 } 711 // END android-changed 712 return random.nextDouble(); 713 } 714 715 /** 716 * Returns the double conversion of the result of rounding the argument to 717 * an integer. Tie breaks are rounded towards even. 718 * <p> 719 * Special cases: 720 * <ul> 721 * <li>{@code rint(+0.0) = +0.0}</li> 722 * <li>{@code rint(-0.0) = -0.0}</li> 723 * <li>{@code rint(+infinity) = +infinity}</li> 724 * <li>{@code rint(-infinity) = -infinity}</li> 725 * <li>{@code rint(NaN) = NaN}</li> 726 * </ul> 727 * 728 * @param d 729 * the value to be rounded. 730 * @return the closest integer to the argument (as a double). 731 */ 732 public static native double rint(double d); 733 734 /** 735 * Returns the result of rounding the argument to an integer. The result is 736 * equivalent to {@code (long) Math.floor(d+0.5)}. 737 * <p> 738 * Special cases: 739 * <ul> 740 * <li>{@code round(+0.0) = +0.0}</li> 741 * <li>{@code round(-0.0) = +0.0}</li> 742 * <li>{@code round((anything > Long.MAX_VALUE) = Long.MAX_VALUE}</li> 743 * <li>{@code round((anything < Long.MIN_VALUE) = Long.MIN_VALUE}</li> 744 * <li>{@code round(+infinity) = Long.MAX_VALUE}</li> 745 * <li>{@code round(-infinity) = Long.MIN_VALUE}</li> 746 * <li>{@code round(NaN) = +0.0}</li> 747 * </ul> 748 * 749 * @param d 750 * the value to be rounded. 751 * @return the closest integer to the argument. 752 */ 753 public static long round(double d) { 754 // check for NaN 755 if (d != d) 756 return 0L; 757 return (long) Math.floor(d + 0.5d); 758 } 759 760 /** 761 * Returns the result of rounding the argument to an integer. The result is 762 * equivalent to {@code (int) Math.floor(f+0.5)}. 763 * <p> 764 * Special cases: 765 * <ul> 766 * <li>{@code round(+0.0) = +0.0}</li> 767 * <li>{@code round(-0.0) = +0.0}</li> 768 * <li>{@code round((anything > Integer.MAX_VALUE) = Integer.MAX_VALUE}</li> 769 * <li>{@code round((anything < Integer.MIN_VALUE) = Integer.MIN_VALUE}</li> 770 * <li>{@code round(+infinity) = Integer.MAX_VALUE}</li> 771 * <li>{@code round(-infinity) = Integer.MIN_VALUE}</li> 772 * <li>{@code round(NaN) = +0.0}</li> 773 * </ul> 774 * 775 * @param f 776 * the value to be rounded. 777 * @return the closest integer to the argument. 778 */ 779 public static int round(float f) { 780 // check for NaN 781 if (f != f) 782 return 0; 783 return (int) Math.floor(f + 0.5f); 784 } 785 786 /** 787 * Returns the signum function of the argument. If the argument is less than 788 * zero, it returns -1.0. If the argument is greater than zero, 1.0 is 789 * returned. If the argument is either positive or negative zero, the 790 * argument is returned as result. 791 * <p> 792 * Special cases: 793 * <ul> 794 * <li>{@code signum(+0.0) = +0.0}</li> 795 * <li>{@code signum(-0.0) = -0.0}</li> 796 * <li>{@code signum(+infinity) = +1.0}</li> 797 * <li>{@code signum(-infinity) = -1.0}</li> 798 * <li>{@code signum(NaN) = NaN}</li> 799 * </ul> 800 * 801 * @param d 802 * the value whose signum has to be computed. 803 * @return the value of the signum function. 804 */ 805 public static double signum(double d){ 806 if(Double.isNaN(d)){ 807 return Double.NaN; 808 } 809 double sig = d; 810 if(d > 0){ 811 sig = 1.0; 812 }else if (d < 0){ 813 sig = -1.0; 814 } 815 return sig; 816 } 817 818 /** 819 * Returns the signum function of the argument. If the argument is less than 820 * zero, it returns -1.0. If the argument is greater than zero, 1.0 is 821 * returned. If the argument is either positive or negative zero, the 822 * argument is returned as result. 823 * <p> 824 * Special cases: 825 * <ul> 826 * <li>{@code signum(+0.0) = +0.0}</li> 827 * <li>{@code signum(-0.0) = -0.0}</li> 828 * <li>{@code signum(+infinity) = +1.0}</li> 829 * <li>{@code signum(-infinity) = -1.0}</li> 830 * <li>{@code signum(NaN) = NaN}</li> 831 * </ul> 832 * 833 * @param f 834 * the value whose signum has to be computed. 835 * @return the value of the signum function. 836 */ 837 public static float signum(float f){ 838 if(Float.isNaN(f)){ 839 return Float.NaN; 840 } 841 float sig = f; 842 if(f > 0){ 843 sig = 1.0f; 844 }else if (f < 0){ 845 sig = -1.0f; 846 } 847 return sig; 848 } 849 850 /** 851 * Returns the closest double approximation of the hyperbolic sine of the 852 * argument. 853 * <p> 854 * Special cases: 855 * <ul> 856 * <li>{@code sinh(+0.0) = +0.0}</li> 857 * <li>{@code sinh(-0.0) = -0.0}</li> 858 * <li>{@code sinh(+infinity) = +infinity}</li> 859 * <li>{@code sinh(-infinity) = -infinity}</li> 860 * <li>{@code sinh(NaN) = NaN}</li> 861 * </ul> 862 * 863 * @param d 864 * the value whose hyperbolic sine has to be computed. 865 * @return the hyperbolic sine of the argument. 866 */ 867 public static native double sinh(double d); 868 869 /** 870 * Returns the closest double approximation of the sine of the argument. 871 * <p> 872 * Special cases: 873 * <ul> 874 * <li>{@code sin(+0.0) = +0.0}</li> 875 * <li>{@code sin(-0.0) = -0.0}</li> 876 * <li>{@code sin(+infinity) = NaN}</li> 877 * <li>{@code sin(-infinity) = NaN}</li> 878 * <li>{@code sin(NaN) = NaN}</li> 879 * </ul> 880 * 881 * @param d 882 * the angle whose sin has to be computed, in radians. 883 * @return the sine of the argument. 884 */ 885 public static native double sin(double d); 886 887 /** 888 * Returns the closest double approximation of the square root of the 889 * argument. 890 * <p> 891 * Special cases: 892 * <ul> 893 * <li>{@code sqrt(+0.0) = +0.0}</li> 894 * <li>{@code sqrt(-0.0) = -0.0}</li> 895 * <li>{@code sqrt( (anything < 0) ) = NaN}</li> 896 * <li>{@code sqrt(+infinity) = +infinity}</li> 897 * <li>{@code sqrt(NaN) = NaN}</li> 898 * </ul> 899 * 900 * @param d 901 * the value whose square root has to be computed. 902 * @return the square root of the argument. 903 */ 904 public static native double sqrt(double d); 905 906 /** 907 * Returns the closest double approximation of the tangent of the argument. 908 * <p> 909 * Special cases: 910 * <ul> 911 * <li>{@code tan(+0.0) = +0.0}</li> 912 * <li>{@code tan(-0.0) = -0.0}</li> 913 * <li>{@code tan(+infinity) = NaN}</li> 914 * <li>{@code tan(-infinity) = NaN}</li> 915 * <li>{@code tan(NaN) = NaN}</li> 916 * </ul> 917 * 918 * @param d 919 * the angle whose tangens has to be computed, in radians. 920 * @return the tangent of the argument. 921 */ 922 public static native double tan(double d); 923 924 /** 925 * Returns the closest double approximation of the hyperbolic tangent of the 926 * argument. The absolute value is always less than 1. 927 * <p> 928 * Special cases: 929 * <ul> 930 * <li>{@code tanh(+0.0) = +0.0}</li> 931 * <li>{@code tanh(-0.0) = -0.0}</li> 932 * <li>{@code tanh(+infinity) = +1.0}</li> 933 * <li>{@code tanh(-infinity) = -1.0}</li> 934 * <li>{@code tanh(NaN) = NaN}</li> 935 * </ul> 936 * 937 * @param d 938 * the value whose hyperbolic tangent has to be computed. 939 * @return the hyperbolic tangent of the argument 940 */ 941 public static native double tanh(double d); 942 943 /** 944 * Returns the measure in degrees of the supplied radian angle. The result 945 * is {@code angrad * 180 / pi}. 946 * <p> 947 * Special cases: 948 * <ul> 949 * <li>{@code toDegrees(+0.0) = +0.0}</li> 950 * <li>{@code toDegrees(-0.0) = -0.0}</li> 951 * <li>{@code toDegrees(+infinity) = +infinity}</li> 952 * <li>{@code toDegrees(-infinity) = -infinity}</li> 953 * <li>{@code toDegrees(NaN) = NaN}</li> 954 * </ul> 955 * 956 * @param angrad 957 * an angle in radians. 958 * @return the degree measure of the angle. 959 */ 960 public static double toDegrees(double angrad) { 961 return angrad * 180d / PI; 962 } 963 964 /** 965 * Returns the measure in radians of the supplied degree angle. The result 966 * is {@code angdeg / 180 * pi}. 967 * <p> 968 * Special cases: 969 * <ul> 970 * <li>{@code toRadians(+0.0) = +0.0}</li> 971 * <li>{@code toRadians(-0.0) = -0.0}</li> 972 * <li>{@code toRadians(+infinity) = +infinity}</li> 973 * <li>{@code toRadians(-infinity) = -infinity}</li> 974 * <li>{@code toRadians(NaN) = NaN}</li> 975 * </ul> 976 * 977 * @param angdeg 978 * an angle in degrees. 979 * @return the radian measure of the angle. 980 */ 981 public static double toRadians(double angdeg) { 982 return angdeg / 180d * PI; 983 } 984 985 /** 986 * Returns the argument's ulp (unit in the last place). The size of a ulp of 987 * a double value is the positive distance between this value and the double 988 * value next larger in magnitude. For non-NaN {@code x}, 989 * {@code ulp(-x) == ulp(x)}. 990 * <p> 991 * Special cases: 992 * <ul> 993 * <li>{@code ulp(+0.0) = Double.MIN_VALUE}</li> 994 * <li>{@code ulp(-0.0) = Double.MIN_VALUE}</li> 995 * <li>{@code ulp(+infintiy) = infinity}</li> 996 * <li>{@code ulp(-infintiy) = infinity}</li> 997 * <li>{@code ulp(NaN) = NaN}</li> 998 * </ul> 999 * 1000 * @param d 1001 * the floating-point value to compute ulp of. 1002 * @return the size of a ulp of the argument. 1003 */ 1004 public static double ulp(double d) { 1005 // special cases 1006 if (Double.isInfinite(d)) { 1007 return Double.POSITIVE_INFINITY; 1008 } else if (d == Double.MAX_VALUE || d == -Double.MAX_VALUE) { 1009 return pow(2, 971); 1010 } 1011 d = Math.abs(d); 1012 return nextafter(d, Double.MAX_VALUE) - d; 1013 } 1014 1015 /** 1016 * Returns the argument's ulp (unit in the last place). The size of a ulp of 1017 * a float value is the positive distance between this value and the float 1018 * value next larger in magnitude. For non-NaN {@code x}, 1019 * {@code ulp(-x) == ulp(x)}. 1020 * <p> 1021 * Special cases: 1022 * <ul> 1023 * <li>{@code ulp(+0.0) = Float.MIN_VALUE}</li> 1024 * <li>{@code ulp(-0.0) = Float.MIN_VALUE}</li> 1025 * <li>{@code ulp(+infintiy) = infinity}</li> 1026 * <li>{@code ulp(-infintiy) = infinity}</li> 1027 * <li>{@code ulp(NaN) = NaN}</li> 1028 * </ul> 1029 * 1030 * @param f 1031 * the floating-point value to compute ulp of. 1032 * @return the size of a ulp of the argument. 1033 */ 1034 public static float ulp(float f) { 1035 // special cases 1036 if (Float.isNaN(f)) { 1037 return Float.NaN; 1038 } else if (Float.isInfinite(f)) { 1039 return Float.POSITIVE_INFINITY; 1040 } else if (f == Float.MAX_VALUE || f == -Float.MAX_VALUE) { 1041 return (float) pow(2, 104); 1042 } 1043 f = Math.abs(f); 1044 return nextafterf(f, Float.MAX_VALUE) - f; 1045 } 1046 1047 private native static double nextafter(double x, double y); 1048 1049 private native static float nextafterf(float x, float y); 1050} 1051