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