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