1 /* 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang; 27 import java.util.Random; 28 import sun.misc.DoubleConsts; 29 30 /** 31 * The class {@code StrictMath} contains methods for performing basic 32 * numeric operations such as the elementary exponential, logarithm, 33 * square root, and trigonometric functions. 34 * 35 * <p>To help ensure portability of Java programs, the definitions of 36 * some of the numeric functions in this package require that they 37 * produce the same results as certain published algorithms. These 38 * algorithms are available from the well-known network library 39 * {@code netlib} as the package "Freely Distributable Math 40 * Library," <a 41 * href="ftp://ftp.netlib.org/fdlibm.tar">{@code fdlibm}</a>. These 42 * algorithms, which are written in the C programming language, are 43 * then to be understood as executed with all floating-point 44 * operations following the rules of Java floating-point arithmetic. 45 * 46 * <p>The Java math library is defined with respect to 47 * {@code fdlibm} version 5.3. Where {@code fdlibm} provides 48 * more than one definition for a function (such as 49 * {@code acos}), use the "IEEE 754 core function" version 50 * (residing in a file whose name begins with the letter 51 * {@code e}). The methods which require {@code fdlibm} 52 * semantics are {@code sin}, {@code cos}, {@code tan}, 53 * {@code asin}, {@code acos}, {@code atan}, 54 * {@code exp}, {@code log}, {@code log10}, 55 * {@code cbrt}, {@code atan2}, {@code pow}, 56 * {@code sinh}, {@code cosh}, {@code tanh}, 57 * {@code hypot}, {@code expm1}, and {@code log1p}. 58 * 59 * <p> 60 * The platform uses signed two's complement integer arithmetic with 61 * int and long primitive types. The developer should choose 62 * the primitive type to ensure that arithmetic operations consistently 63 * produce correct results, which in some cases means the operations 64 * will not overflow the range of values of the computation. 65 * The best practice is to choose the primitive type and algorithm to avoid 66 * overflow. In cases where the size is {@code int} or {@code long} and 67 * overflow errors need to be detected, the methods {@code addExact}, 68 * {@code subtractExact}, {@code multiplyExact}, and {@code toIntExact} 69 * throw an {@code ArithmeticException} when the results overflow. 70 * For other arithmetic operations such as divide, absolute value, 71 * increment, decrement, and negation overflow occurs only with 72 * a specific minimum or maximum value and should be checked against 73 * the minimum or maximum as appropriate. 74 * 75 * @author unascribed 76 * @author Joseph D. Darcy 77 * @since 1.3 78 */ 79 80 public final class StrictMath { 81 82 /** 83 * Don't let anyone instantiate this class. 84 */ 85 private StrictMath() {} 86 87 /** 88 * The {@code double} value that is closer than any other to 89 * <i>e</i>, the base of the natural logarithms. 90 */ 91 public static final double E = 2.7182818284590452354; 92 93 /** 94 * The {@code double} value that is closer than any other to 95 * <i>pi</i>, the ratio of the circumference of a circle to its 96 * diameter. 97 */ 98 public static final double PI = 3.14159265358979323846; 99 100 /** 101 * Constant by which to multiply an angular value in degrees to obtain an 102 * angular value in radians. 103 */ 104 private static final double DEGREES_TO_RADIANS = 0.017453292519943295; 105 106 /** 107 * Constant by which to multiply an angular value in radians to obtain an 108 * angular value in degrees. 109 */ 110 111 private static final double RADIANS_TO_DEGREES = 57.29577951308232; 112 /** 113 * Returns the trigonometric sine of an angle. Special cases: 114 * <ul><li>If the argument is NaN or an infinity, then the 115 * result is NaN. 116 * <li>If the argument is zero, then the result is a zero with the 117 * same sign as the argument.</ul> 118 * 119 * @param a an angle, in radians. 120 * @return the sine of the argument. 121 */ 122 public static native double sin(double a); 123 124 /** 125 * Returns the trigonometric cosine of an angle. Special cases: 126 * <ul><li>If the argument is NaN or an infinity, then the 127 * result is NaN.</ul> 128 * 129 * @param a an angle, in radians. 130 * @return the cosine of the argument. 131 */ 132 public static native double cos(double a); 133 134 /** 135 * Returns the trigonometric tangent of an angle. Special cases: 136 * <ul><li>If the argument is NaN or an infinity, then the result 137 * is NaN. 138 * <li>If the argument is zero, then the result is a zero with the 139 * same sign as the argument.</ul> 140 * 141 * @param a an angle, in radians. 142 * @return the tangent of the argument. 143 */ 144 public static native double tan(double a); 145 146 /** 147 * Returns the arc sine of a value; the returned angle is in the 148 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases: 149 * <ul><li>If the argument is NaN or its absolute value is greater 150 * than 1, then the result is NaN. 151 * <li>If the argument is zero, then the result is a zero with the 152 * same sign as the argument.</ul> 153 * 154 * @param a the value whose arc sine is to be returned. 155 * @return the arc sine of the argument. 156 */ 157 public static native double asin(double a); 158 159 /** 160 * Returns the arc cosine of a value; the returned angle is in the 161 * range 0.0 through <i>pi</i>. Special case: 162 * <ul><li>If the argument is NaN or its absolute value is greater 163 * than 1, then the result is NaN.</ul> 164 * 165 * @param a the value whose arc cosine is to be returned. 166 * @return the arc cosine of the argument. 167 */ 168 public static native double acos(double a); 169 170 /** 171 * Returns the arc tangent of a value; the returned angle is in the 172 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases: 173 * <ul><li>If the argument is NaN, then the result is NaN. 174 * <li>If the argument is zero, then the result is a zero with the 175 * same sign as the argument.</ul> 176 * 177 * @param a the value whose arc tangent is to be returned. 178 * @return the arc tangent of the argument. 179 */ 180 public static native double atan(double a); 181 182 /** 183 * Converts an angle measured in degrees to an approximately 184 * equivalent angle measured in radians. The conversion from 185 * degrees to radians is generally inexact. 186 * 187 * @param angdeg an angle, in degrees 188 * @return the measurement of the angle {@code angdeg} 189 * in radians. 190 */ 191 public static strictfp double toRadians(double angdeg) { 192 // Do not delegate to Math.toRadians(angdeg) because 193 // this method has the strictfp modifier. 194 return angdeg * DEGREES_TO_RADIANS; 195 } 196 197 /** 198 * Converts an angle measured in radians to an approximately 199 * equivalent angle measured in degrees. The conversion from 200 * radians to degrees is generally inexact; users should 201 * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly 202 * equal {@code 0.0}. 203 * 204 * @param angrad an angle, in radians 205 * @return the measurement of the angle {@code angrad} 206 * in degrees. 207 */ 208 public static strictfp double toDegrees(double angrad) { 209 // Do not delegate to Math.toDegrees(angrad) because 210 // this method has the strictfp modifier. 211 return angrad * RADIANS_TO_DEGREES; 212 } 213 214 /** 215 * Returns Euler's number <i>e</i> raised to the power of a 216 * {@code double} value. Special cases: 217 * <ul><li>If the argument is NaN, the result is NaN. 218 * <li>If the argument is positive infinity, then the result is 219 * positive infinity. 220 * <li>If the argument is negative infinity, then the result is 221 * positive zero.</ul> 222 * 223 * @param a the exponent to raise <i>e</i> to. 224 * @return the value <i>e</i><sup>{@code a}</sup>, 225 * where <i>e</i> is the base of the natural logarithms. 226 */ 227 public static native double exp(double a); 228 229 /** 230 * Returns the natural logarithm (base <i>e</i>) of a {@code double} 231 * value. Special cases: 232 * <ul><li>If the argument is NaN or less than zero, then the result 233 * is NaN. 234 * <li>If the argument is positive infinity, then the result is 235 * positive infinity. 236 * <li>If the argument is positive zero or negative zero, then the 237 * result is negative infinity.</ul> 238 * 239 * @param a a value 240 * @return the value ln {@code a}, the natural logarithm of 241 * {@code a}. 242 */ 243 public static native double log(double a); 244 245 246 /** 247 * Returns the base 10 logarithm of a {@code double} value. 248 * Special cases: 249 * 250 * <ul><li>If the argument is NaN or less than zero, then the result 251 * is NaN. 252 * <li>If the argument is positive infinity, then the result is 253 * positive infinity. 254 * <li>If the argument is positive zero or negative zero, then the 255 * result is negative infinity. 256 * <li> If the argument is equal to 10<sup><i>n</i></sup> for 257 * integer <i>n</i>, then the result is <i>n</i>. 258 * </ul> 259 * 260 * @param a a value 261 * @return the base 10 logarithm of {@code a}. 262 * @since 1.5 263 */ 264 public static native double log10(double a); 265 266 /** 267 * Returns the correctly rounded positive square root of a 268 * {@code double} value. 269 * Special cases: 270 * <ul><li>If the argument is NaN or less than zero, then the result 271 * is NaN. 272 * <li>If the argument is positive infinity, then the result is positive 273 * infinity. 274 * <li>If the argument is positive zero or negative zero, then the 275 * result is the same as the argument.</ul> 276 * Otherwise, the result is the {@code double} value closest to 277 * the true mathematical square root of the argument value. 278 * 279 * @param a a value. 280 * @return the positive square root of {@code a}. 281 */ 282 public static native double sqrt(double a); 283 284 /** 285 * Returns the cube root of a {@code double} value. For 286 * positive finite {@code x}, {@code cbrt(-x) == 287 * -cbrt(x)}; that is, the cube root of a negative value is 288 * the negative of the cube root of that value's magnitude. 289 * Special cases: 290 * 291 * <ul> 292 * 293 * <li>If the argument is NaN, then the result is NaN. 294 * 295 * <li>If the argument is infinite, then the result is an infinity 296 * with the same sign as the argument. 297 * 298 * <li>If the argument is zero, then the result is a zero with the 299 * same sign as the argument. 300 * 301 * </ul> 302 * 303 * @param a a value. 304 * @return the cube root of {@code a}. 305 * @since 1.5 306 */ 307 public static native double cbrt(double a); 308 309 /** 310 * Computes the remainder operation on two arguments as prescribed 311 * by the IEEE 754 standard. 312 * The remainder value is mathematically equal to 313 * <code>f1 - f2</code> × <i>n</i>, 314 * where <i>n</i> is the mathematical integer closest to the exact 315 * mathematical value of the quotient {@code f1/f2}, and if two 316 * mathematical integers are equally close to {@code f1/f2}, 317 * then <i>n</i> is the integer that is even. If the remainder is 318 * zero, its sign is the same as the sign of the first argument. 319 * Special cases: 320 * <ul><li>If either argument is NaN, or the first argument is infinite, 321 * or the second argument is positive zero or negative zero, then the 322 * result is NaN. 323 * <li>If the first argument is finite and the second argument is 324 * infinite, then the result is the same as the first argument.</ul> 325 * 326 * @param f1 the dividend. 327 * @param f2 the divisor. 328 * @return the remainder when {@code f1} is divided by 329 * {@code f2}. 330 */ 331 public static native double IEEEremainder(double f1, double f2); 332 333 /** 334 * Returns the smallest (closest to negative infinity) 335 * {@code double} value that is greater than or equal to the 336 * argument and is equal to a mathematical integer. Special cases: 337 * <ul><li>If the argument value is already equal to a 338 * mathematical integer, then the result is the same as the 339 * argument. <li>If the argument is NaN or an infinity or 340 * positive zero or negative zero, then the result is the same as 341 * the argument. <li>If the argument value is less than zero but 342 * greater than -1.0, then the result is negative zero.</ul> Note 343 * that the value of {@code StrictMath.ceil(x)} is exactly the 344 * value of {@code -StrictMath.floor(-x)}. 345 * 346 * @param a a value. 347 * @return the smallest (closest to negative infinity) 348 * floating-point value that is greater than or equal to 349 * the argument and is equal to a mathematical integer. 350 */ 351 public static double ceil(double a) { 352 return floorOrCeil(a, -0.0, 1.0, 1.0); 353 } 354 355 /** 356 * Returns the largest (closest to positive infinity) 357 * {@code double} value that is less than or equal to the 358 * argument and is equal to a mathematical integer. Special cases: 359 * <ul><li>If the argument value is already equal to a 360 * mathematical integer, then the result is the same as the 361 * argument. <li>If the argument is NaN or an infinity or 362 * positive zero or negative zero, then the result is the same as 363 * the argument.</ul> 364 * 365 * @param a a value. 366 * @return the largest (closest to positive infinity) 367 * floating-point value that less than or equal to the argument 368 * and is equal to a mathematical integer. 369 */ 370 public static double floor(double a) { 371 return floorOrCeil(a, -1.0, 0.0, -1.0); 372 } 373 374 /** 375 * Internal method to share logic between floor and ceil. 376 * 377 * @param a the value to be floored or ceiled 378 * @param negativeBoundary result for values in (-1, 0) 379 * @param positiveBoundary result for values in (0, 1) 380 * @param increment value to add when the argument is non-integral 381 */ 382 private static double floorOrCeil(double a, 383 double negativeBoundary, 384 double positiveBoundary, 385 double sign) { 386 int exponent = Math.getExponent(a); 387 388 if (exponent < 0) { 389 /* 390 * Absolute value of argument is less than 1. 391 * floorOrceil(-0.0) => -0.0 392 * floorOrceil(+0.0) => +0.0 393 */ 394 return ((a == 0.0) ? a : 395 ( (a < 0.0) ? negativeBoundary : positiveBoundary) ); 396 } else if (exponent >= 52) { 397 /* 398 * Infinity, NaN, or a value so large it must be integral. 399 */ 400 return a; 401 } 402 // Else the argument is either an integral value already XOR it 403 // has to be rounded to one. 404 assert exponent >= 0 && exponent <= 51; 405 406 long doppel = Double.doubleToRawLongBits(a); 407 long mask = DoubleConsts.SIGNIF_BIT_MASK >> exponent; 408 409 if ( (mask & doppel) == 0L ) 410 return a; // integral value 411 else { 412 double result = Double.longBitsToDouble(doppel & (~mask)); 413 if (sign*a > 0.0) 414 result = result + sign; 415 return result; 416 } 417 } 418 419 /** 420 * Returns the {@code double} value that is closest in value 421 * to the argument and is equal to a mathematical integer. If two 422 * {@code double} values that are mathematical integers are 423 * equally close to the value of the argument, the result is the 424 * integer value that is even. Special cases: 425 * <ul><li>If the argument value is already equal to a mathematical 426 * integer, then the result is the same as the argument. 427 * <li>If the argument is NaN or an infinity or positive zero or negative 428 * zero, then the result is the same as the argument.</ul> 429 * 430 * @param a a value. 431 * @return the closest floating-point value to {@code a} that is 432 * equal to a mathematical integer. 433 * @author Joseph D. Darcy 434 */ 435 public static double rint(double a) { 436 /* 437 * If the absolute value of a is not less than 2^52, it 438 * is either a finite integer (the double format does not have 439 * enough significand bits for a number that large to have any 440 * fractional portion), an infinity, or a NaN. In any of 441 * these cases, rint of the argument is the argument. 442 * 443 * Otherwise, the sum (twoToThe52 + a ) will properly round 444 * away any fractional portion of a since ulp(twoToThe52) == 445 * 1.0; subtracting out twoToThe52 from this sum will then be 446 * exact and leave the rounded integer portion of a. 447 * 448 * This method does *not* need to be declared strictfp to get 449 * fully reproducible results. Whether or not a method is 450 * declared strictfp can only make a difference in the 451 * returned result if some operation would overflow or 452 * underflow with strictfp semantics. The operation 453 * (twoToThe52 + a ) cannot overflow since large values of a 454 * are screened out; the add cannot underflow since twoToThe52 455 * is too large. The subtraction ((twoToThe52 + a ) - 456 * twoToThe52) will be exact as discussed above and thus 457 * cannot overflow or meaningfully underflow. Finally, the 458 * last multiply in the return statement is by plus or minus 459 * 1.0, which is exact too. 460 */ 461 double twoToThe52 = (double)(1L << 52); // 2^52 462 double sign = Math.copySign(1.0, a); // preserve sign info 463 a = Math.abs(a); 464 465 if (a < twoToThe52) { // E_min <= ilogb(a) <= 51 466 a = ((twoToThe52 + a ) - twoToThe52); 467 } 468 469 return sign * a; // restore original sign 470 } 471 472 /** 473 * Returns the angle <i>theta</i> from the conversion of rectangular 474 * coordinates ({@code x}, {@code y}) to polar 475 * coordinates (r, <i>theta</i>). 476 * This method computes the phase <i>theta</i> by computing an arc tangent 477 * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special 478 * cases: 479 * <ul><li>If either argument is NaN, then the result is NaN. 480 * <li>If the first argument is positive zero and the second argument 481 * is positive, or the first argument is positive and finite and the 482 * second argument is positive infinity, then the result is positive 483 * zero. 484 * <li>If the first argument is negative zero and the second argument 485 * is positive, or the first argument is negative and finite and the 486 * second argument is positive infinity, then the result is negative zero. 487 * <li>If the first argument is positive zero and the second argument 488 * is negative, or the first argument is positive and finite and the 489 * second argument is negative infinity, then the result is the 490 * {@code double} value closest to <i>pi</i>. 491 * <li>If the first argument is negative zero and the second argument 492 * is negative, or the first argument is negative and finite and the 493 * second argument is negative infinity, then the result is the 494 * {@code double} value closest to -<i>pi</i>. 495 * <li>If the first argument is positive and the second argument is 496 * positive zero or negative zero, or the first argument is positive 497 * infinity and the second argument is finite, then the result is the 498 * {@code double} value closest to <i>pi</i>/2. 499 * <li>If the first argument is negative and the second argument is 500 * positive zero or negative zero, or the first argument is negative 501 * infinity and the second argument is finite, then the result is the 502 * {@code double} value closest to -<i>pi</i>/2. 503 * <li>If both arguments are positive infinity, then the result is the 504 * {@code double} value closest to <i>pi</i>/4. 505 * <li>If the first argument is positive infinity and the second argument 506 * is negative infinity, then the result is the {@code double} 507 * value closest to 3*<i>pi</i>/4. 508 * <li>If the first argument is negative infinity and the second argument 509 * is positive infinity, then the result is the {@code double} value 510 * closest to -<i>pi</i>/4. 511 * <li>If both arguments are negative infinity, then the result is the 512 * {@code double} value closest to -3*<i>pi</i>/4.</ul> 513 * 514 * @param y the ordinate coordinate 515 * @param x the abscissa coordinate 516 * @return the <i>theta</i> component of the point 517 * (<i>r</i>, <i>theta</i>) 518 * in polar coordinates that corresponds to the point 519 * (<i>x</i>, <i>y</i>) in Cartesian coordinates. 520 */ 521 public static native double atan2(double y, double x); 522 523 524 /** 525 * Returns the value of the first argument raised to the power of the 526 * second argument. Special cases: 527 * 528 * <ul><li>If the second argument is positive or negative zero, then the 529 * result is 1.0. 530 * <li>If the second argument is 1.0, then the result is the same as the 531 * first argument. 532 * <li>If the second argument is NaN, then the result is NaN. 533 * <li>If the first argument is NaN and the second argument is nonzero, 534 * then the result is NaN. 535 * 536 * <li>If 537 * <ul> 538 * <li>the absolute value of the first argument is greater than 1 539 * and the second argument is positive infinity, or 540 * <li>the absolute value of the first argument is less than 1 and 541 * the second argument is negative infinity, 542 * </ul> 543 * then the result is positive infinity. 544 * 545 * <li>If 546 * <ul> 547 * <li>the absolute value of the first argument is greater than 1 and 548 * the second argument is negative infinity, or 549 * <li>the absolute value of the 550 * first argument is less than 1 and the second argument is positive 551 * infinity, 552 * </ul> 553 * then the result is positive zero. 554 * 555 * <li>If the absolute value of the first argument equals 1 and the 556 * second argument is infinite, then the result is NaN. 557 * 558 * <li>If 559 * <ul> 560 * <li>the first argument is positive zero and the second argument 561 * is greater than zero, or 562 * <li>the first argument is positive infinity and the second 563 * argument is less than zero, 564 * </ul> 565 * then the result is positive zero. 566 * 567 * <li>If 568 * <ul> 569 * <li>the first argument is positive zero and the second argument 570 * is less than zero, or 571 * <li>the first argument is positive infinity and the second 572 * argument is greater than zero, 573 * </ul> 574 * then the result is positive infinity. 575 * 576 * <li>If 577 * <ul> 578 * <li>the first argument is negative zero and the second argument 579 * is greater than zero but not a finite odd integer, or 580 * <li>the first argument is negative infinity and the second 581 * argument is less than zero but not a finite odd integer, 582 * </ul> 583 * then the result is positive zero. 584 * 585 * <li>If 586 * <ul> 587 * <li>the first argument is negative zero and the second argument 588 * is a positive finite odd integer, or 589 * <li>the first argument is negative infinity and the second 590 * argument is a negative finite odd integer, 591 * </ul> 592 * then the result is negative zero. 593 * 594 * <li>If 595 * <ul> 596 * <li>the first argument is negative zero and the second argument 597 * is less than zero but not a finite odd integer, or 598 * <li>the first argument is negative infinity and the second 599 * argument is greater than zero but not a finite odd integer, 600 * </ul> 601 * then the result is positive infinity. 602 * 603 * <li>If 604 * <ul> 605 * <li>the first argument is negative zero and the second argument 606 * is a negative finite odd integer, or 607 * <li>the first argument is negative infinity and the second 608 * argument is a positive finite odd integer, 609 * </ul> 610 * then the result is negative infinity. 611 * 612 * <li>If the first argument is finite and less than zero 613 * <ul> 614 * <li> if the second argument is a finite even integer, the 615 * result is equal to the result of raising the absolute value of 616 * the first argument to the power of the second argument 617 * 618 * <li>if the second argument is a finite odd integer, the result 619 * is equal to the negative of the result of raising the absolute 620 * value of the first argument to the power of the second 621 * argument 622 * 623 * <li>if the second argument is finite and not an integer, then 624 * the result is NaN. 625 * </ul> 626 * 627 * <li>If both arguments are integers, then the result is exactly equal 628 * to the mathematical result of raising the first argument to the power 629 * of the second argument if that result can in fact be represented 630 * exactly as a {@code double} value.</ul> 631 * 632 * <p>(In the foregoing descriptions, a floating-point value is 633 * considered to be an integer if and only if it is finite and a 634 * fixed point of the method {@link #ceil ceil} or, 635 * equivalently, a fixed point of the method {@link #floor 636 * floor}. A value is a fixed point of a one-argument 637 * method if and only if the result of applying the method to the 638 * value is equal to the value.) 639 * 640 * @param a base. 641 * @param b the exponent. 642 * @return the value {@code a}<sup>{@code b}</sup>. 643 */ 644 public static native double pow(double a, double b); 645 646 /** 647 * Returns the closest {@code int} to the argument, with ties 648 * rounding to positive infinity. 649 * 650 * <p>Special cases: 651 * <ul><li>If the argument is NaN, the result is 0. 652 * <li>If the argument is negative infinity or any value less than or 653 * equal to the value of {@code Integer.MIN_VALUE}, the result is 654 * equal to the value of {@code Integer.MIN_VALUE}. 655 * <li>If the argument is positive infinity or any value greater than or 656 * equal to the value of {@code Integer.MAX_VALUE}, the result is 657 * equal to the value of {@code Integer.MAX_VALUE}.</ul> 658 * 659 * @param a a floating-point value to be rounded to an integer. 660 * @return the value of the argument rounded to the nearest 661 * {@code int} value. 662 * @see java.lang.Integer#MAX_VALUE 663 * @see java.lang.Integer#MIN_VALUE 664 */ 665 public static int round(float a) { 666 return Math.round(a); 667 } 668 669 /** 670 * Returns the closest {@code long} to the argument, with ties 671 * rounding to positive infinity. 672 * 673 * <p>Special cases: 674 * <ul><li>If the argument is NaN, the result is 0. 675 * <li>If the argument is negative infinity or any value less than or 676 * equal to the value of {@code Long.MIN_VALUE}, the result is 677 * equal to the value of {@code Long.MIN_VALUE}. 678 * <li>If the argument is positive infinity or any value greater than or 679 * equal to the value of {@code Long.MAX_VALUE}, the result is 680 * equal to the value of {@code Long.MAX_VALUE}.</ul> 681 * 682 * @param a a floating-point value to be rounded to a 683 * {@code long}. 684 * @return the value of the argument rounded to the nearest 685 * {@code long} value. 686 * @see java.lang.Long#MAX_VALUE 687 * @see java.lang.Long#MIN_VALUE 688 */ 689 public static long round(double a) { 690 return Math.round(a); 691 } 692 693 private static final class RandomNumberGeneratorHolder { 694 static final Random randomNumberGenerator = new Random(); 695 } 696 697 /** 698 * Returns a {@code double} value with a positive sign, greater 699 * than or equal to {@code 0.0} and less than {@code 1.0}. 700 * Returned values are chosen pseudorandomly with (approximately) 701 * uniform distribution from that range. 702 * 703 * <p>When this method is first called, it creates a single new 704 * pseudorandom-number generator, exactly as if by the expression 705 * 706 * <blockquote>{@code new java.util.Random()}</blockquote> 707 * 708 * This new pseudorandom-number generator is used thereafter for 709 * all calls to this method and is used nowhere else. 710 * 711 * <p>This method is properly synchronized to allow correct use by 712 * more than one thread. However, if many threads need to generate 713 * pseudorandom numbers at a great rate, it may reduce contention 714 * for each thread to have its own pseudorandom-number generator. 715 * 716 * @return a pseudorandom {@code double} greater than or equal 717 * to {@code 0.0} and less than {@code 1.0}. 718 * @see Random#nextDouble() 719 */ 720 public static double random() { 721 return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble(); 722 } 723 724 /** 725 * Returns the sum of its arguments, 726 * throwing an exception if the result overflows an {@code int}. 727 * 728 * @param x the first value 729 * @param y the second value 730 * @return the result 731 * @throws ArithmeticException if the result overflows an int 732 * @see Math#addExact(int,int) 733 * @since 1.8 734 */ 735 public static int addExact(int x, int y) { 736 return Math.addExact(x, y); 737 } 738 739 /** 740 * Returns the sum of its arguments, 741 * throwing an exception if the result overflows a {@code long}. 742 * 743 * @param x the first value 744 * @param y the second value 745 * @return the result 746 * @throws ArithmeticException if the result overflows a long 747 * @see Math#addExact(long,long) 748 * @since 1.8 749 */ 750 public static long addExact(long x, long y) { 751 return Math.addExact(x, y); 752 } 753 754 /** 755 * Returns the difference of the arguments, 756 * throwing an exception if the result overflows an {@code int}. 757 * 758 * @param x the first value 759 * @param y the second value to subtract from the first 760 * @return the result 761 * @throws ArithmeticException if the result overflows an int 762 * @see Math#subtractExact(int,int) 763 * @since 1.8 764 */ 765 public static int subtractExact(int x, int y) { 766 return Math.subtractExact(x, y); 767 } 768 769 /** 770 * Returns the difference of the arguments, 771 * throwing an exception if the result overflows a {@code long}. 772 * 773 * @param x the first value 774 * @param y the second value to subtract from the first 775 * @return the result 776 * @throws ArithmeticException if the result overflows a long 777 * @see Math#subtractExact(long,long) 778 * @since 1.8 779 */ 780 public static long subtractExact(long x, long y) { 781 return Math.subtractExact(x, y); 782 } 783 784 /** 785 * Returns the product of the arguments, 786 * throwing an exception if the result overflows an {@code int}. 787 * 788 * @param x the first value 789 * @param y the second value 790 * @return the result 791 * @throws ArithmeticException if the result overflows an int 792 * @see Math#multiplyExact(int,int) 793 * @since 1.8 794 */ 795 public static int multiplyExact(int x, int y) { 796 return Math.multiplyExact(x, y); 797 } 798 799 /** 800 * Returns the product of the arguments, 801 * throwing an exception if the result overflows a {@code long}. 802 * 803 * @param x the first value 804 * @param y the second value 805 * @return the result 806 * @throws ArithmeticException if the result overflows a long 807 * @see Math#multiplyExact(long,long) 808 * @since 1.8 809 */ 810 public static long multiplyExact(long x, long y) { 811 return Math.multiplyExact(x, y); 812 } 813 814 /** 815 * Returns the value of the {@code long} argument; 816 * throwing an exception if the value overflows an {@code int}. 817 * 818 * @param value the long value 819 * @return the argument as an int 820 * @throws ArithmeticException if the {@code argument} overflows an int 821 * @see Math#toIntExact(long) 822 * @since 1.8 823 */ 824 public static int toIntExact(long value) { 825 return Math.toIntExact(value); 826 } 827 828 /** 829 * Returns the largest (closest to positive infinity) 830 * {@code int} value that is less than or equal to the algebraic quotient. 831 * There is one special case, if the dividend is the 832 * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1}, 833 * then integer overflow occurs and 834 * the result is equal to the {@code Integer.MIN_VALUE}. 835 * <p> 836 * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and 837 * a comparison to the integer division {@code /} operator. 838 * 839 * @param x the dividend 840 * @param y the divisor 841 * @return the largest (closest to positive infinity) 842 * {@code int} value that is less than or equal to the algebraic quotient. 843 * @throws ArithmeticException if the divisor {@code y} is zero 844 * @see Math#floorDiv(int, int) 845 * @see Math#floor(double) 846 * @since 1.8 847 */ 848 public static int floorDiv(int x, int y) { 849 return Math.floorDiv(x, y); 850 } 851 852 /** 853 * Returns the largest (closest to positive infinity) 854 * {@code long} value that is less than or equal to the algebraic quotient. 855 * There is one special case, if the dividend is the 856 * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1}, 857 * then integer overflow occurs and 858 * the result is equal to the {@code Long.MIN_VALUE}. 859 * <p> 860 * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and 861 * a comparison to the integer division {@code /} operator. 862 * 863 * @param x the dividend 864 * @param y the divisor 865 * @return the largest (closest to positive infinity) 866 * {@code long} value that is less than or equal to the algebraic quotient. 867 * @throws ArithmeticException if the divisor {@code y} is zero 868 * @see Math#floorDiv(long, long) 869 * @see Math#floor(double) 870 * @since 1.8 871 */ 872 public static long floorDiv(long x, long y) { 873 return Math.floorDiv(x, y); 874 } 875 876 /** 877 * Returns the floor modulus of the {@code int} arguments. 878 * <p> 879 * The floor modulus is {@code x - (floorDiv(x, y) * y)}, 880 * has the same sign as the divisor {@code y}, and 881 * is in the range of {@code -abs(y) < r < +abs(y)}. 882 * <p> 883 * The relationship between {@code floorDiv} and {@code floorMod} is such that: 884 * <ul> 885 * <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x} 886 * </ul> 887 * <p> 888 * See {@link Math#floorMod(int, int) Math.floorMod} for examples and 889 * a comparison to the {@code %} operator. 890 * 891 * @param x the dividend 892 * @param y the divisor 893 * @return the floor modulus {@code x - (floorDiv(x, y) * y)} 894 * @throws ArithmeticException if the divisor {@code y} is zero 895 * @see Math#floorMod(int, int) 896 * @see StrictMath#floorDiv(int, int) 897 * @since 1.8 898 */ 899 public static int floorMod(int x, int y) { 900 return Math.floorMod(x , y); 901 } 902 /** 903 * Returns the floor modulus of the {@code long} arguments. 904 * <p> 905 * The floor modulus is {@code x - (floorDiv(x, y) * y)}, 906 * has the same sign as the divisor {@code y}, and 907 * is in the range of {@code -abs(y) < r < +abs(y)}. 908 * <p> 909 * The relationship between {@code floorDiv} and {@code floorMod} is such that: 910 * <ul> 911 * <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x} 912 * </ul> 913 * <p> 914 * See {@link Math#floorMod(int, int) Math.floorMod} for examples and 915 * a comparison to the {@code %} operator. 916 * 917 * @param x the dividend 918 * @param y the divisor 919 * @return the floor modulus {@code x - (floorDiv(x, y) * y)} 920 * @throws ArithmeticException if the divisor {@code y} is zero 921 * @see Math#floorMod(long, long) 922 * @see StrictMath#floorDiv(long, long) 923 * @since 1.8 924 */ 925 public static long floorMod(long x, long y) { 926 return Math.floorMod(x, y); 927 } 928 929 /** 930 * Returns the absolute value of an {@code int} value. 931 * If the argument is not negative, the argument is returned. 932 * If the argument is negative, the negation of the argument is returned. 933 * 934 * <p>Note that if the argument is equal to the value of 935 * {@link Integer#MIN_VALUE}, the most negative representable 936 * {@code int} value, the result is that same value, which is 937 * negative. 938 * 939 * @param a the argument whose absolute value is to be determined. 940 * @return the absolute value of the argument. 941 */ 942 public static int abs(int a) { 943 return Math.abs(a); 944 } 945 946 /** 947 * Returns the absolute value of a {@code long} value. 948 * If the argument is not negative, the argument is returned. 949 * If the argument is negative, the negation of the argument is returned. 950 * 951 * <p>Note that if the argument is equal to the value of 952 * {@link Long#MIN_VALUE}, the most negative representable 953 * {@code long} value, the result is that same value, which 954 * is negative. 955 * 956 * @param a the argument whose absolute value is to be determined. 957 * @return the absolute value of the argument. 958 */ 959 public static long abs(long a) { 960 return Math.abs(a); 961 } 962 963 /** 964 * Returns the absolute value of a {@code float} value. 965 * If the argument is not negative, the argument is returned. 966 * If the argument is negative, the negation of the argument is returned. 967 * Special cases: 968 * <ul><li>If the argument is positive zero or negative zero, the 969 * result is positive zero. 970 * <li>If the argument is infinite, the result is positive infinity. 971 * <li>If the argument is NaN, the result is NaN.</ul> 972 * In other words, the result is the same as the value of the expression: 973 * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))} 974 * 975 * @param a the argument whose absolute value is to be determined 976 * @return the absolute value of the argument. 977 */ 978 public static float abs(float a) { 979 return Math.abs(a); 980 } 981 982 /** 983 * Returns the absolute value of a {@code double} value. 984 * If the argument is not negative, the argument is returned. 985 * If the argument is negative, the negation of the argument is returned. 986 * Special cases: 987 * <ul><li>If the argument is positive zero or negative zero, the result 988 * is positive zero. 989 * <li>If the argument is infinite, the result is positive infinity. 990 * <li>If the argument is NaN, the result is NaN.</ul> 991 * In other words, the result is the same as the value of the expression: 992 * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)} 993 * 994 * @param a the argument whose absolute value is to be determined 995 * @return the absolute value of the argument. 996 */ 997 public static double abs(double a) { 998 return Math.abs(a); 999 } 1000 1001 /** 1002 * Returns the greater of two {@code int} values. That is, the 1003 * result is the argument closer to the value of 1004 * {@link Integer#MAX_VALUE}. If the arguments have the same value, 1005 * the result is that same value. 1006 * 1007 * @param a an argument. 1008 * @param b another argument. 1009 * @return the larger of {@code a} and {@code b}. 1010 */ 1011 public static int max(int a, int b) { 1012 return Math.max(a, b); 1013 } 1014 1015 /** 1016 * Returns the greater of two {@code long} values. That is, the 1017 * result is the argument closer to the value of 1018 * {@link Long#MAX_VALUE}. If the arguments have the same value, 1019 * the result is that same value. 1020 * 1021 * @param a an argument. 1022 * @param b another argument. 1023 * @return the larger of {@code a} and {@code b}. 1024 */ 1025 public static long max(long a, long b) { 1026 return Math.max(a, b); 1027 } 1028 1029 /** 1030 * Returns the greater of two {@code float} values. That is, 1031 * the result is the argument closer to positive infinity. If the 1032 * arguments have the same value, the result is that same 1033 * value. If either value is NaN, then the result is NaN. Unlike 1034 * the numerical comparison operators, this method considers 1035 * negative zero to be strictly smaller than positive zero. If one 1036 * argument is positive zero and the other negative zero, the 1037 * result is positive zero. 1038 * 1039 * @param a an argument. 1040 * @param b another argument. 1041 * @return the larger of {@code a} and {@code b}. 1042 */ 1043 public static float max(float a, float b) { 1044 return Math.max(a, b); 1045 } 1046 1047 /** 1048 * Returns the greater of two {@code double} values. That 1049 * is, the result is the argument closer to positive infinity. If 1050 * the arguments have the same value, the result is that same 1051 * value. If either value is NaN, then the result is NaN. Unlike 1052 * the numerical comparison operators, this method considers 1053 * negative zero to be strictly smaller than positive zero. If one 1054 * argument is positive zero and the other negative zero, the 1055 * result is positive zero. 1056 * 1057 * @param a an argument. 1058 * @param b another argument. 1059 * @return the larger of {@code a} and {@code b}. 1060 */ 1061 public static double max(double a, double b) { 1062 return Math.max(a, b); 1063 } 1064 1065 /** 1066 * Returns the smaller of two {@code int} values. That is, 1067 * the result the argument closer to the value of 1068 * {@link Integer#MIN_VALUE}. If the arguments have the same 1069 * value, the result is that same value. 1070 * 1071 * @param a an argument. 1072 * @param b another argument. 1073 * @return the smaller of {@code a} and {@code b}. 1074 */ 1075 public static int min(int a, int b) { 1076 return Math.min(a, b); 1077 } 1078 1079 /** 1080 * Returns the smaller of two {@code long} values. That is, 1081 * the result is the argument closer to the value of 1082 * {@link Long#MIN_VALUE}. If the arguments have the same 1083 * value, the result is that same value. 1084 * 1085 * @param a an argument. 1086 * @param b another argument. 1087 * @return the smaller of {@code a} and {@code b}. 1088 */ 1089 public static long min(long a, long b) { 1090 return Math.min(a, b); 1091 } 1092 1093 /** 1094 * Returns the smaller of two {@code float} values. That is, 1095 * the result is the value closer to negative infinity. If the 1096 * arguments have the same value, the result is that same 1097 * value. If either value is NaN, then the result is NaN. Unlike 1098 * the numerical comparison operators, this method considers 1099 * negative zero to be strictly smaller than positive zero. If 1100 * one argument is positive zero and the other is negative zero, 1101 * the result is negative zero. 1102 * 1103 * @param a an argument. 1104 * @param b another argument. 1105 * @return the smaller of {@code a} and {@code b.} 1106 */ 1107 public static float min(float a, float b) { 1108 return Math.min(a, b); 1109 } 1110 1111 /** 1112 * Returns the smaller of two {@code double} values. That 1113 * is, the result is the value closer to negative infinity. If the 1114 * arguments have the same value, the result is that same 1115 * value. If either value is NaN, then the result is NaN. Unlike 1116 * the numerical comparison operators, this method considers 1117 * negative zero to be strictly smaller than positive zero. If one 1118 * argument is positive zero and the other is negative zero, the 1119 * result is negative zero. 1120 * 1121 * @param a an argument. 1122 * @param b another argument. 1123 * @return the smaller of {@code a} and {@code b}. 1124 */ 1125 public static double min(double a, double b) { 1126 return Math.min(a, b); 1127 } 1128 1129 /** 1130 * Returns the size of an ulp of the argument. An ulp, unit in 1131 * the last place, of a {@code double} value is the positive 1132 * distance between this floating-point value and the {@code 1133 * double} value next larger in magnitude. Note that for non-NaN 1134 * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 1135 * 1136 * <p>Special Cases: 1137 * <ul> 1138 * <li> If the argument is NaN, then the result is NaN. 1139 * <li> If the argument is positive or negative infinity, then the 1140 * result is positive infinity. 1141 * <li> If the argument is positive or negative zero, then the result is 1142 * {@code Double.MIN_VALUE}. 1143 * <li> If the argument is ±{@code Double.MAX_VALUE}, then 1144 * the result is equal to 2<sup>971</sup>. 1145 * </ul> 1146 * 1147 * @param d the floating-point value whose ulp is to be returned 1148 * @return the size of an ulp of the argument 1149 * @author Joseph D. Darcy 1150 * @since 1.5 1151 */ 1152 public static double ulp(double d) { 1153 return Math.ulp(d); 1154 } 1155 1156 /** 1157 * Returns the size of an ulp of the argument. An ulp, unit in 1158 * the last place, of a {@code float} value is the positive 1159 * distance between this floating-point value and the {@code 1160 * float} value next larger in magnitude. Note that for non-NaN 1161 * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 1162 * 1163 * <p>Special Cases: 1164 * <ul> 1165 * <li> If the argument is NaN, then the result is NaN. 1166 * <li> If the argument is positive or negative infinity, then the 1167 * result is positive infinity. 1168 * <li> If the argument is positive or negative zero, then the result is 1169 * {@code Float.MIN_VALUE}. 1170 * <li> If the argument is ±{@code Float.MAX_VALUE}, then 1171 * the result is equal to 2<sup>104</sup>. 1172 * </ul> 1173 * 1174 * @param f the floating-point value whose ulp is to be returned 1175 * @return the size of an ulp of the argument 1176 * @author Joseph D. Darcy 1177 * @since 1.5 1178 */ 1179 public static float ulp(float f) { 1180 return Math.ulp(f); 1181 } 1182 1183 /** 1184 * Returns the signum function of the argument; zero if the argument 1185 * is zero, 1.0 if the argument is greater than zero, -1.0 if the 1186 * argument is less than zero. 1187 * 1188 * <p>Special Cases: 1189 * <ul> 1190 * <li> If the argument is NaN, then the result is NaN. 1191 * <li> If the argument is positive zero or negative zero, then the 1192 * result is the same as the argument. 1193 * </ul> 1194 * 1195 * @param d the floating-point value whose signum is to be returned 1196 * @return the signum function of the argument 1197 * @author Joseph D. Darcy 1198 * @since 1.5 1199 */ 1200 public static double signum(double d) { 1201 return Math.signum(d); 1202 } 1203 1204 /** 1205 * Returns the signum function of the argument; zero if the argument 1206 * is zero, 1.0f if the argument is greater than zero, -1.0f if the 1207 * argument is less than zero. 1208 * 1209 * <p>Special Cases: 1210 * <ul> 1211 * <li> If the argument is NaN, then the result is NaN. 1212 * <li> If the argument is positive zero or negative zero, then the 1213 * result is the same as the argument. 1214 * </ul> 1215 * 1216 * @param f the floating-point value whose signum is to be returned 1217 * @return the signum function of the argument 1218 * @author Joseph D. Darcy 1219 * @since 1.5 1220 */ 1221 public static float signum(float f) { 1222 return Math.signum(f); 1223 } 1224 1225 /** 1226 * Returns the hyperbolic sine of a {@code double} value. 1227 * The hyperbolic sine of <i>x</i> is defined to be 1228 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2 1229 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1230 * 1231 * <p>Special cases: 1232 * <ul> 1233 * 1234 * <li>If the argument is NaN, then the result is NaN. 1235 * 1236 * <li>If the argument is infinite, then the result is an infinity 1237 * with the same sign as the argument. 1238 * 1239 * <li>If the argument is zero, then the result is a zero with the 1240 * same sign as the argument. 1241 * 1242 * </ul> 1243 * 1244 * @param x The number whose hyperbolic sine is to be returned. 1245 * @return The hyperbolic sine of {@code x}. 1246 * @since 1.5 1247 */ 1248 public static native double sinh(double x); 1249 1250 /** 1251 * Returns the hyperbolic cosine of a {@code double} value. 1252 * The hyperbolic cosine of <i>x</i> is defined to be 1253 * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2 1254 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1255 * 1256 * <p>Special cases: 1257 * <ul> 1258 * 1259 * <li>If the argument is NaN, then the result is NaN. 1260 * 1261 * <li>If the argument is infinite, then the result is positive 1262 * infinity. 1263 * 1264 * <li>If the argument is zero, then the result is {@code 1.0}. 1265 * 1266 * </ul> 1267 * 1268 * @param x The number whose hyperbolic cosine is to be returned. 1269 * @return The hyperbolic cosine of {@code x}. 1270 * @since 1.5 1271 */ 1272 public static native double cosh(double x); 1273 1274 /** 1275 * Returns the hyperbolic tangent of a {@code double} value. 1276 * The hyperbolic tangent of <i>x</i> is defined to be 1277 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>), 1278 * in other words, {@linkplain Math#sinh 1279 * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note 1280 * that the absolute value of the exact tanh is always less than 1281 * 1. 1282 * 1283 * <p>Special cases: 1284 * <ul> 1285 * 1286 * <li>If the argument is NaN, then the result is NaN. 1287 * 1288 * <li>If the argument is zero, then the result is a zero with the 1289 * same sign as the argument. 1290 * 1291 * <li>If the argument is positive infinity, then the result is 1292 * {@code +1.0}. 1293 * 1294 * <li>If the argument is negative infinity, then the result is 1295 * {@code -1.0}. 1296 * 1297 * </ul> 1298 * 1299 * @param x The number whose hyperbolic tangent is to be returned. 1300 * @return The hyperbolic tangent of {@code x}. 1301 * @since 1.5 1302 */ 1303 public static native double tanh(double x); 1304 1305 /** 1306 * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1307 * without intermediate overflow or underflow. 1308 * 1309 * <p>Special cases: 1310 * <ul> 1311 * 1312 * <li> If either argument is infinite, then the result 1313 * is positive infinity. 1314 * 1315 * <li> If either argument is NaN and neither argument is infinite, 1316 * then the result is NaN. 1317 * 1318 * </ul> 1319 * 1320 * @param x a value 1321 * @param y a value 1322 * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1323 * without intermediate overflow or underflow 1324 * @since 1.5 1325 */ 1326 public static native double hypot(double x, double y); 1327 1328 /** 1329 * Returns <i>e</i><sup>x</sup> -1. Note that for values of 1330 * <i>x</i> near 0, the exact sum of 1331 * {@code expm1(x)} + 1 is much closer to the true 1332 * result of <i>e</i><sup>x</sup> than {@code exp(x)}. 1333 * 1334 * <p>Special cases: 1335 * <ul> 1336 * <li>If the argument is NaN, the result is NaN. 1337 * 1338 * <li>If the argument is positive infinity, then the result is 1339 * positive infinity. 1340 * 1341 * <li>If the argument is negative infinity, then the result is 1342 * -1.0. 1343 * 1344 * <li>If the argument is zero, then the result is a zero with the 1345 * same sign as the argument. 1346 * 1347 * </ul> 1348 * 1349 * @param x the exponent to raise <i>e</i> to in the computation of 1350 * <i>e</i><sup>{@code x}</sup> -1. 1351 * @return the value <i>e</i><sup>{@code x}</sup> - 1. 1352 * @since 1.5 1353 */ 1354 public static native double expm1(double x); 1355 1356 /** 1357 * Returns the natural logarithm of the sum of the argument and 1. 1358 * Note that for small values {@code x}, the result of 1359 * {@code log1p(x)} is much closer to the true result of ln(1 1360 * + {@code x}) than the floating-point evaluation of 1361 * {@code log(1.0+x)}. 1362 * 1363 * <p>Special cases: 1364 * <ul> 1365 * 1366 * <li>If the argument is NaN or less than -1, then the result is 1367 * NaN. 1368 * 1369 * <li>If the argument is positive infinity, then the result is 1370 * positive infinity. 1371 * 1372 * <li>If the argument is negative one, then the result is 1373 * negative infinity. 1374 * 1375 * <li>If the argument is zero, then the result is a zero with the 1376 * same sign as the argument. 1377 * 1378 * </ul> 1379 * 1380 * @param x a value 1381 * @return the value ln({@code x} + 1), the natural 1382 * log of {@code x} + 1 1383 * @since 1.5 1384 */ 1385 public static native double log1p(double x); 1386 1387 /** 1388 * Returns the first floating-point argument with the sign of the 1389 * second floating-point argument. For this method, a NaN 1390 * {@code sign} argument is always treated as if it were 1391 * positive. 1392 * 1393 * @param magnitude the parameter providing the magnitude of the result 1394 * @param sign the parameter providing the sign of the result 1395 * @return a value with the magnitude of {@code magnitude} 1396 * and the sign of {@code sign}. 1397 * @since 1.6 1398 */ 1399 public static double copySign(double magnitude, double sign) { 1400 return Math.copySign(magnitude, (Double.isNaN(sign)?1.0d:sign)); 1401 } 1402 1403 /** 1404 * Returns the first floating-point argument with the sign of the 1405 * second floating-point argument. For this method, a NaN 1406 * {@code sign} argument is always treated as if it were 1407 * positive. 1408 * 1409 * @param magnitude the parameter providing the magnitude of the result 1410 * @param sign the parameter providing the sign of the result 1411 * @return a value with the magnitude of {@code magnitude} 1412 * and the sign of {@code sign}. 1413 * @since 1.6 1414 */ 1415 public static float copySign(float magnitude, float sign) { 1416 return Math.copySign(magnitude, (Float.isNaN(sign)?1.0f:sign)); 1417 } 1418 /** 1419 * Returns the unbiased exponent used in the representation of a 1420 * {@code float}. Special cases: 1421 * 1422 * <ul> 1423 * <li>If the argument is NaN or infinite, then the result is 1424 * {@link Float#MAX_EXPONENT} + 1. 1425 * <li>If the argument is zero or subnormal, then the result is 1426 * {@link Float#MIN_EXPONENT} -1. 1427 * </ul> 1428 * @param f a {@code float} value 1429 * @return the unbiased exponent of the argument 1430 * @since 1.6 1431 */ 1432 public static int getExponent(float f) { 1433 return Math.getExponent(f); 1434 } 1435 1436 /** 1437 * Returns the unbiased exponent used in the representation of a 1438 * {@code double}. Special cases: 1439 * 1440 * <ul> 1441 * <li>If the argument is NaN or infinite, then the result is 1442 * {@link Double#MAX_EXPONENT} + 1. 1443 * <li>If the argument is zero or subnormal, then the result is 1444 * {@link Double#MIN_EXPONENT} -1. 1445 * </ul> 1446 * @param d a {@code double} value 1447 * @return the unbiased exponent of the argument 1448 * @since 1.6 1449 */ 1450 public static int getExponent(double d) { 1451 return Math.getExponent(d); 1452 } 1453 1454 /** 1455 * Returns the floating-point number adjacent to the first 1456 * argument in the direction of the second argument. If both 1457 * arguments compare as equal the second argument is returned. 1458 * 1459 * <p>Special cases: 1460 * <ul> 1461 * <li> If either argument is a NaN, then NaN is returned. 1462 * 1463 * <li> If both arguments are signed zeros, {@code direction} 1464 * is returned unchanged (as implied by the requirement of 1465 * returning the second argument if the arguments compare as 1466 * equal). 1467 * 1468 * <li> If {@code start} is 1469 * ±{@link Double#MIN_VALUE} and {@code direction} 1470 * has a value such that the result should have a smaller 1471 * magnitude, then a zero with the same sign as {@code start} 1472 * is returned. 1473 * 1474 * <li> If {@code start} is infinite and 1475 * {@code direction} has a value such that the result should 1476 * have a smaller magnitude, {@link Double#MAX_VALUE} with the 1477 * same sign as {@code start} is returned. 1478 * 1479 * <li> If {@code start} is equal to ± 1480 * {@link Double#MAX_VALUE} and {@code direction} has a 1481 * value such that the result should have a larger magnitude, an 1482 * infinity with same sign as {@code start} is returned. 1483 * </ul> 1484 * 1485 * @param start starting floating-point value 1486 * @param direction value indicating which of 1487 * {@code start}'s neighbors or {@code start} should 1488 * be returned 1489 * @return The floating-point number adjacent to {@code start} in the 1490 * direction of {@code direction}. 1491 * @since 1.6 1492 */ 1493 public static double nextAfter(double start, double direction) { 1494 return Math.nextAfter(start, direction); 1495 } 1496 1497 /** 1498 * Returns the floating-point number adjacent to the first 1499 * argument in the direction of the second argument. If both 1500 * arguments compare as equal a value equivalent to the second argument 1501 * is returned. 1502 * 1503 * <p>Special cases: 1504 * <ul> 1505 * <li> If either argument is a NaN, then NaN is returned. 1506 * 1507 * <li> If both arguments are signed zeros, a value equivalent 1508 * to {@code direction} is returned. 1509 * 1510 * <li> If {@code start} is 1511 * ±{@link Float#MIN_VALUE} and {@code direction} 1512 * has a value such that the result should have a smaller 1513 * magnitude, then a zero with the same sign as {@code start} 1514 * is returned. 1515 * 1516 * <li> If {@code start} is infinite and 1517 * {@code direction} has a value such that the result should 1518 * have a smaller magnitude, {@link Float#MAX_VALUE} with the 1519 * same sign as {@code start} is returned. 1520 * 1521 * <li> If {@code start} is equal to ± 1522 * {@link Float#MAX_VALUE} and {@code direction} has a 1523 * value such that the result should have a larger magnitude, an 1524 * infinity with same sign as {@code start} is returned. 1525 * </ul> 1526 * 1527 * @param start starting floating-point value 1528 * @param direction value indicating which of 1529 * {@code start}'s neighbors or {@code start} should 1530 * be returned 1531 * @return The floating-point number adjacent to {@code start} in the 1532 * direction of {@code direction}. 1533 * @since 1.6 1534 */ 1535 public static float nextAfter(float start, double direction) { 1536 return Math.nextAfter(start, direction); 1537 } 1538 1539 /** 1540 * Returns the floating-point value adjacent to {@code d} in 1541 * the direction of positive infinity. This method is 1542 * semantically equivalent to {@code nextAfter(d, 1543 * Double.POSITIVE_INFINITY)}; however, a {@code nextUp} 1544 * implementation may run faster than its equivalent 1545 * {@code nextAfter} call. 1546 * 1547 * <p>Special Cases: 1548 * <ul> 1549 * <li> If the argument is NaN, the result is NaN. 1550 * 1551 * <li> If the argument is positive infinity, the result is 1552 * positive infinity. 1553 * 1554 * <li> If the argument is zero, the result is 1555 * {@link Double#MIN_VALUE} 1556 * 1557 * </ul> 1558 * 1559 * @param d starting floating-point value 1560 * @return The adjacent floating-point value closer to positive 1561 * infinity. 1562 * @since 1.6 1563 */ 1564 public static double nextUp(double d) { 1565 return Math.nextUp(d); 1566 } 1567 1568 /** 1569 * Returns the floating-point value adjacent to {@code f} in 1570 * the direction of positive infinity. This method is 1571 * semantically equivalent to {@code nextAfter(f, 1572 * Float.POSITIVE_INFINITY)}; however, a {@code nextUp} 1573 * implementation may run faster than its equivalent 1574 * {@code nextAfter} call. 1575 * 1576 * <p>Special Cases: 1577 * <ul> 1578 * <li> If the argument is NaN, the result is NaN. 1579 * 1580 * <li> If the argument is positive infinity, the result is 1581 * positive infinity. 1582 * 1583 * <li> If the argument is zero, the result is 1584 * {@link Float#MIN_VALUE} 1585 * 1586 * </ul> 1587 * 1588 * @param f starting floating-point value 1589 * @return The adjacent floating-point value closer to positive 1590 * infinity. 1591 * @since 1.6 1592 */ 1593 public static float nextUp(float f) { 1594 return Math.nextUp(f); 1595 } 1596 1597 /** 1598 * Returns the floating-point value adjacent to {@code d} in 1599 * the direction of negative infinity. This method is 1600 * semantically equivalent to {@code nextAfter(d, 1601 * Double.NEGATIVE_INFINITY)}; however, a 1602 * {@code nextDown} implementation may run faster than its 1603 * equivalent {@code nextAfter} call. 1604 * 1605 * <p>Special Cases: 1606 * <ul> 1607 * <li> If the argument is NaN, the result is NaN. 1608 * 1609 * <li> If the argument is negative infinity, the result is 1610 * negative infinity. 1611 * 1612 * <li> If the argument is zero, the result is 1613 * {@code -Double.MIN_VALUE} 1614 * 1615 * </ul> 1616 * 1617 * @param d starting floating-point value 1618 * @return The adjacent floating-point value closer to negative 1619 * infinity. 1620 * @since 1.8 1621 */ 1622 public static double nextDown(double d) { 1623 return Math.nextDown(d); 1624 } 1625 1626 /** 1627 * Returns the floating-point value adjacent to {@code f} in 1628 * the direction of negative infinity. This method is 1629 * semantically equivalent to {@code nextAfter(f, 1630 * Float.NEGATIVE_INFINITY)}; however, a 1631 * {@code nextDown} implementation may run faster than its 1632 * equivalent {@code nextAfter} call. 1633 * 1634 * <p>Special Cases: 1635 * <ul> 1636 * <li> If the argument is NaN, the result is NaN. 1637 * 1638 * <li> If the argument is negative infinity, the result is 1639 * negative infinity. 1640 * 1641 * <li> If the argument is zero, the result is 1642 * {@code -Float.MIN_VALUE} 1643 * 1644 * </ul> 1645 * 1646 * @param f starting floating-point value 1647 * @return The adjacent floating-point value closer to negative 1648 * infinity. 1649 * @since 1.8 1650 */ 1651 public static float nextDown(float f) { 1652 return Math.nextDown(f); 1653 } 1654 1655 /** 1656 * Returns {@code d} × 1657 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1658 * by a single correctly rounded floating-point multiply to a 1659 * member of the double value set. See the Java 1660 * Language Specification for a discussion of floating-point 1661 * value sets. If the exponent of the result is between {@link 1662 * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the 1663 * answer is calculated exactly. If the exponent of the result 1664 * would be larger than {@code Double.MAX_EXPONENT}, an 1665 * infinity is returned. Note that if the result is subnormal, 1666 * precision may be lost; that is, when {@code scalb(x, n)} 1667 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1668 * <i>x</i>. When the result is non-NaN, the result has the same 1669 * sign as {@code d}. 1670 * 1671 * <p>Special cases: 1672 * <ul> 1673 * <li> If the first argument is NaN, NaN is returned. 1674 * <li> If the first argument is infinite, then an infinity of the 1675 * same sign is returned. 1676 * <li> If the first argument is zero, then a zero of the same 1677 * sign is returned. 1678 * </ul> 1679 * 1680 * @param d number to be scaled by a power of two. 1681 * @param scaleFactor power of 2 used to scale {@code d} 1682 * @return {@code d} × 2<sup>{@code scaleFactor}</sup> 1683 * @since 1.6 1684 */ 1685 public static double scalb(double d, int scaleFactor) { 1686 return Math.scalb(d, scaleFactor); 1687 } 1688 1689 /** 1690 * Returns {@code f} × 1691 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1692 * by a single correctly rounded floating-point multiply to a 1693 * member of the float value set. See the Java 1694 * Language Specification for a discussion of floating-point 1695 * value sets. If the exponent of the result is between {@link 1696 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the 1697 * answer is calculated exactly. If the exponent of the result 1698 * would be larger than {@code Float.MAX_EXPONENT}, an 1699 * infinity is returned. Note that if the result is subnormal, 1700 * precision may be lost; that is, when {@code scalb(x, n)} 1701 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1702 * <i>x</i>. When the result is non-NaN, the result has the same 1703 * sign as {@code f}. 1704 * 1705 * <p>Special cases: 1706 * <ul> 1707 * <li> If the first argument is NaN, NaN is returned. 1708 * <li> If the first argument is infinite, then an infinity of the 1709 * same sign is returned. 1710 * <li> If the first argument is zero, then a zero of the same 1711 * sign is returned. 1712 * </ul> 1713 * 1714 * @param f number to be scaled by a power of two. 1715 * @param scaleFactor power of 2 used to scale {@code f} 1716 * @return {@code f} × 2<sup>{@code scaleFactor}</sup> 1717 * @since 1.6 1718 */ 1719 public static float scalb(float f, int scaleFactor) { 1720 return Math.scalb(f, scaleFactor); 1721 } 1722 }