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