1 /* 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang; 27 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 native double pow(double a, double b); 647 648 /** 649 * Returns the closest {@code int} to the argument, with ties 650 * rounding to positive infinity. 651 * 652 * <p>Special cases: 653 * <ul><li>If the argument is NaN, the result is 0. 654 * <li>If the argument is negative infinity or any value less than or 655 * equal to the value of {@code Integer.MIN_VALUE}, the result is 656 * equal to the value of {@code Integer.MIN_VALUE}. 657 * <li>If the argument is positive infinity or any value greater than or 658 * equal to the value of {@code Integer.MAX_VALUE}, the result is 659 * equal to the value of {@code Integer.MAX_VALUE}.</ul> 660 * 661 * @param a a floating-point value to be rounded to an integer. 662 * @return the value of the argument rounded to the nearest 663 * {@code int} value. 664 * @see java.lang.Integer#MAX_VALUE 665 * @see java.lang.Integer#MIN_VALUE 666 */ 667 public static int round(float a) { 668 return Math.round(a); 669 } 670 671 /** 672 * Returns the closest {@code long} to the argument, with ties 673 * rounding to positive infinity. 674 * 675 * <p>Special cases: 676 * <ul><li>If the argument is NaN, the result is 0. 677 * <li>If the argument is negative infinity or any value less than or 678 * equal to the value of {@code Long.MIN_VALUE}, the result is 679 * equal to the value of {@code Long.MIN_VALUE}. 680 * <li>If the argument is positive infinity or any value greater than or 681 * equal to the value of {@code Long.MAX_VALUE}, the result is 682 * equal to the value of {@code Long.MAX_VALUE}.</ul> 683 * 684 * @param a a floating-point value to be rounded to a 685 * {@code long}. 686 * @return the value of the argument rounded to the nearest 687 * {@code long} value. 688 * @see java.lang.Long#MAX_VALUE 689 * @see java.lang.Long#MIN_VALUE 690 */ 691 public static long round(double a) { 692 return Math.round(a); 693 } 694 695 private static final class RandomNumberGeneratorHolder { 696 static final Random randomNumberGenerator = new Random(); 697 } 698 699 /** 700 * Returns a {@code double} value with a positive sign, greater 701 * than or equal to {@code 0.0} and less than {@code 1.0}. 702 * Returned values are chosen pseudorandomly with (approximately) 703 * uniform distribution from that range. 704 * 705 * <p>When this method is first called, it creates a single new 706 * pseudorandom-number generator, exactly as if by the expression 707 * 708 * <blockquote>{@code new java.util.Random()}</blockquote> 709 * 710 * This new pseudorandom-number generator is used thereafter for 711 * all calls to this method and is used nowhere else. 712 * 713 * <p>This method is properly synchronized to allow correct use by 714 * more than one thread. However, if many threads need to generate 715 * pseudorandom numbers at a great rate, it may reduce contention 716 * for each thread to have its own pseudorandom-number generator. 717 * 718 * @return a pseudorandom {@code double} greater than or equal 719 * to {@code 0.0} and less than {@code 1.0}. 720 * @see Random#nextDouble() 721 */ 722 public static double random() { 723 return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble(); 724 } 725 726 /** 727 * Returns the sum of its arguments, 728 * throwing an exception if the result overflows an {@code int}. 729 * 730 * @param x the first value 731 * @param y the second value 732 * @return the result 733 * @throws ArithmeticException if the result overflows an int 734 * @see Math#addExact(int,int) 735 * @since 1.8 736 */ 737 public static int addExact(int x, int y) { 738 return Math.addExact(x, y); 739 } 740 741 /** 742 * Returns the sum of its arguments, 743 * throwing an exception if the result overflows a {@code long}. 744 * 745 * @param x the first value 746 * @param y the second value 747 * @return the result 748 * @throws ArithmeticException if the result overflows a long 749 * @see Math#addExact(long,long) 750 * @since 1.8 751 */ 752 public static long addExact(long x, long y) { 753 return Math.addExact(x, y); 754 } 755 756 /** 757 * Returns the difference of the arguments, 758 * throwing an exception if the result overflows an {@code int}. 759 * 760 * @param x the first value 761 * @param y the second value to subtract from the first 762 * @return the result 763 * @throws ArithmeticException if the result overflows an int 764 * @see Math#subtractExact(int,int) 765 * @since 1.8 766 */ 767 public static int subtractExact(int x, int y) { 768 return Math.subtractExact(x, y); 769 } 770 771 /** 772 * Returns the difference of the arguments, 773 * throwing an exception if the result overflows a {@code long}. 774 * 775 * @param x the first value 776 * @param y the second value to subtract from the first 777 * @return the result 778 * @throws ArithmeticException if the result overflows a long 779 * @see Math#subtractExact(long,long) 780 * @since 1.8 781 */ 782 public static long subtractExact(long x, long y) { 783 return Math.subtractExact(x, y); 784 } 785 786 /** 787 * Returns the product of the arguments, 788 * throwing an exception if the result overflows an {@code int}. 789 * 790 * @param x the first value 791 * @param y the second value 792 * @return the result 793 * @throws ArithmeticException if the result overflows an int 794 * @see Math#multiplyExact(int,int) 795 * @since 1.8 796 */ 797 public static int multiplyExact(int x, int y) { 798 return Math.multiplyExact(x, y); 799 } 800 801 /** 802 * Returns the product of the arguments, 803 * throwing an exception if the result overflows a {@code long}. 804 * 805 * @param x the first value 806 * @param y the second value 807 * @return the result 808 * @throws ArithmeticException if the result overflows a long 809 * @see Math#multiplyExact(long,long) 810 * @since 1.8 811 */ 812 public static long multiplyExact(long x, long y) { 813 return Math.multiplyExact(x, y); 814 } 815 816 /** 817 * Returns the value of the {@code long} argument; 818 * throwing an exception if the value overflows an {@code int}. 819 * 820 * @param value the long value 821 * @return the argument as an int 822 * @throws ArithmeticException if the {@code argument} overflows an int 823 * @see Math#toIntExact(long) 824 * @since 1.8 825 */ 826 public static int toIntExact(long value) { 827 return Math.toIntExact(value); 828 } 829 830 /** 831 * Returns the largest (closest to positive infinity) 832 * {@code int} value that is less than or equal to the algebraic quotient. 833 * There is one special case, if the dividend is the 834 * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1}, 835 * then integer overflow occurs and 836 * the result is equal to the {@code Integer.MIN_VALUE}. 837 * <p> 838 * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and 839 * a comparison to the integer division {@code /} operator. 840 * 841 * @param x the dividend 842 * @param y the divisor 843 * @return the largest (closest to positive infinity) 844 * {@code int} value that is less than or equal to the algebraic quotient. 845 * @throws ArithmeticException if the divisor {@code y} is zero 846 * @see Math#floorDiv(int, int) 847 * @see Math#floor(double) 848 * @since 1.8 849 */ 850 public static int floorDiv(int x, int y) { 851 return Math.floorDiv(x, y); 852 } 853 854 /** 855 * Returns the largest (closest to positive infinity) 856 * {@code long} value that is less than or equal to the algebraic quotient. 857 * There is one special case, if the dividend is the 858 * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1}, 859 * then integer overflow occurs and 860 * the result is equal to the {@code Long.MIN_VALUE}. 861 * <p> 862 * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and 863 * a comparison to the integer division {@code /} operator. 864 * 865 * @param x the dividend 866 * @param y the divisor 867 * @return the largest (closest to positive infinity) 868 * {@code long} value that is less than or equal to the algebraic quotient. 869 * @throws ArithmeticException if the divisor {@code y} is zero 870 * @see Math#floorDiv(long, long) 871 * @see Math#floor(double) 872 * @since 1.8 873 */ 874 public static long floorDiv(long x, long y) { 875 return Math.floorDiv(x, y); 876 } 877 878 /** 879 * Returns the floor modulus of the {@code int} arguments. 880 * <p> 881 * The floor modulus is {@code x - (floorDiv(x, y) * y)}, 882 * has the same sign as the divisor {@code y}, and 883 * is in the range of {@code -abs(y) < r < +abs(y)}. 884 * <p> 885 * The relationship between {@code floorDiv} and {@code floorMod} is such that: 886 * <ul> 887 * <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x} 888 * </ul> 889 * <p> 890 * See {@link Math#floorMod(int, int) Math.floorMod} for examples and 891 * a comparison to the {@code %} operator. 892 * 893 * @param x the dividend 894 * @param y the divisor 895 * @return the floor modulus {@code x - (floorDiv(x, y) * y)} 896 * @throws ArithmeticException if the divisor {@code y} is zero 897 * @see Math#floorMod(int, int) 898 * @see StrictMath#floorDiv(int, int) 899 * @since 1.8 900 */ 901 public static int floorMod(int x, int y) { 902 return Math.floorMod(x , y); 903 } 904 /** 905 * Returns the floor modulus of the {@code long} arguments. 906 * <p> 907 * The floor modulus is {@code x - (floorDiv(x, y) * y)}, 908 * has the same sign as the divisor {@code y}, and 909 * is in the range of {@code -abs(y) < r < +abs(y)}. 910 * <p> 911 * The relationship between {@code floorDiv} and {@code floorMod} is such that: 912 * <ul> 913 * <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x} 914 * </ul> 915 * <p> 916 * See {@link Math#floorMod(int, int) Math.floorMod} for examples and 917 * a comparison to the {@code %} operator. 918 * 919 * @param x the dividend 920 * @param y the divisor 921 * @return the floor modulus {@code x - (floorDiv(x, y) * y)} 922 * @throws ArithmeticException if the divisor {@code y} is zero 923 * @see Math#floorMod(long, long) 924 * @see StrictMath#floorDiv(long, long) 925 * @since 1.8 926 */ 927 public static long floorMod(long x, long y) { 928 return Math.floorMod(x, y); 929 } 930 931 /** 932 * Returns the absolute value of an {@code int} value. 933 * If the argument is not negative, the argument is returned. 934 * If the argument is negative, the negation of the argument is returned. 935 * 936 * <p>Note that if the argument is equal to the value of 937 * {@link Integer#MIN_VALUE}, the most negative representable 938 * {@code int} value, the result is that same value, which is 939 * negative. 940 * 941 * @param a the argument whose absolute value is to be determined. 942 * @return the absolute value of the argument. 943 */ 944 public static int abs(int a) { 945 return Math.abs(a); 946 } 947 948 /** 949 * Returns the absolute value of a {@code long} value. 950 * If the argument is not negative, the argument is returned. 951 * If the argument is negative, the negation of the argument is returned. 952 * 953 * <p>Note that if the argument is equal to the value of 954 * {@link Long#MIN_VALUE}, the most negative representable 955 * {@code long} value, the result is that same value, which 956 * is negative. 957 * 958 * @param a the argument whose absolute value is to be determined. 959 * @return the absolute value of the argument. 960 */ 961 public static long abs(long a) { 962 return Math.abs(a); 963 } 964 965 /** 966 * Returns the absolute value of a {@code float} value. 967 * If the argument is not negative, the argument is returned. 968 * If the argument is negative, the negation of the argument is returned. 969 * Special cases: 970 * <ul><li>If the argument is positive zero or negative zero, the 971 * result is positive zero. 972 * <li>If the argument is infinite, the result is positive infinity. 973 * <li>If the argument is NaN, the result is NaN.</ul> 974 * In other words, the result is the same as the value of the expression: 975 * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))} 976 * 977 * @param a the argument whose absolute value is to be determined 978 * @return the absolute value of the argument. 979 */ 980 public static float abs(float a) { 981 return Math.abs(a); 982 } 983 984 /** 985 * Returns the absolute value of a {@code double} value. 986 * If the argument is not negative, the argument is returned. 987 * If the argument is negative, the negation of the argument is returned. 988 * Special cases: 989 * <ul><li>If the argument is positive zero or negative zero, the result 990 * is positive zero. 991 * <li>If the argument is infinite, the result is positive infinity. 992 * <li>If the argument is NaN, the result is NaN.</ul> 993 * In other words, the result is the same as the value of the expression: 994 * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)} 995 * 996 * @param a the argument whose absolute value is to be determined 997 * @return the absolute value of the argument. 998 */ 999 public static double abs(double a) { 1000 return Math.abs(a); 1001 } 1002 1003 /** 1004 * Returns the greater of two {@code int} values. That is, the 1005 * result is the argument closer to the value of 1006 * {@link Integer#MAX_VALUE}. If the arguments have the same value, 1007 * the result is that same value. 1008 * 1009 * @param a an argument. 1010 * @param b another argument. 1011 * @return the larger of {@code a} and {@code b}. 1012 */ 1013 @HotSpotIntrinsicCandidate 1014 public static int max(int a, int b) { 1015 return Math.max(a, b); 1016 } 1017 1018 /** 1019 * Returns the greater of two {@code long} values. That is, the 1020 * result is the argument closer to the value of 1021 * {@link Long#MAX_VALUE}. If the arguments have the same value, 1022 * the result is that same value. 1023 * 1024 * @param a an argument. 1025 * @param b another argument. 1026 * @return the larger of {@code a} and {@code b}. 1027 */ 1028 public static long max(long a, long b) { 1029 return Math.max(a, b); 1030 } 1031 1032 /** 1033 * Returns the greater of two {@code float} values. That is, 1034 * the result is the argument closer to positive infinity. If the 1035 * arguments have the same value, the result is that same 1036 * value. If either value is NaN, then the result is NaN. Unlike 1037 * the numerical comparison operators, this method considers 1038 * negative zero to be strictly smaller than positive zero. If one 1039 * argument is positive zero and the other negative zero, the 1040 * result is positive zero. 1041 * 1042 * @param a an argument. 1043 * @param b another argument. 1044 * @return the larger of {@code a} and {@code b}. 1045 */ 1046 public static float max(float a, float b) { 1047 return Math.max(a, b); 1048 } 1049 1050 /** 1051 * Returns the greater of two {@code double} values. That 1052 * is, the result is the argument closer to positive infinity. If 1053 * the arguments have the same value, the result is that same 1054 * value. If either value is NaN, then the result is NaN. Unlike 1055 * the numerical comparison operators, this method considers 1056 * negative zero to be strictly smaller than positive zero. If one 1057 * argument is positive zero and the other negative zero, the 1058 * result is positive zero. 1059 * 1060 * @param a an argument. 1061 * @param b another argument. 1062 * @return the larger of {@code a} and {@code b}. 1063 */ 1064 public static double max(double a, double b) { 1065 return Math.max(a, b); 1066 } 1067 1068 /** 1069 * Returns the smaller of two {@code int} values. That is, 1070 * the result the argument closer to the value of 1071 * {@link Integer#MIN_VALUE}. If the arguments have the same 1072 * value, the result is that same value. 1073 * 1074 * @param a an argument. 1075 * @param b another argument. 1076 * @return the smaller of {@code a} and {@code b}. 1077 */ 1078 @HotSpotIntrinsicCandidate 1079 public static int min(int a, int b) { 1080 return Math.min(a, b); 1081 } 1082 1083 /** 1084 * Returns the smaller of two {@code long} values. That is, 1085 * the result is the argument closer to the value of 1086 * {@link Long#MIN_VALUE}. If the arguments have the same 1087 * value, the result is that same value. 1088 * 1089 * @param a an argument. 1090 * @param b another argument. 1091 * @return the smaller of {@code a} and {@code b}. 1092 */ 1093 public static long min(long a, long b) { 1094 return Math.min(a, b); 1095 } 1096 1097 /** 1098 * Returns the smaller of two {@code float} values. That is, 1099 * the result is the value closer to negative infinity. If the 1100 * arguments have the same value, the result is that same 1101 * value. If either value is NaN, then the result is NaN. Unlike 1102 * the numerical comparison operators, this method considers 1103 * negative zero to be strictly smaller than positive zero. If 1104 * one argument is positive zero and the other is negative zero, 1105 * the result is negative zero. 1106 * 1107 * @param a an argument. 1108 * @param b another argument. 1109 * @return the smaller of {@code a} and {@code b.} 1110 */ 1111 public static float min(float a, float b) { 1112 return Math.min(a, b); 1113 } 1114 1115 /** 1116 * Returns the smaller of two {@code double} values. That 1117 * is, the result is the value closer to negative infinity. If the 1118 * arguments have the same value, the result is that same 1119 * value. If either value is NaN, then the result is NaN. Unlike 1120 * the numerical comparison operators, this method considers 1121 * negative zero to be strictly smaller than positive zero. If one 1122 * argument is positive zero and the other is negative zero, the 1123 * result is negative zero. 1124 * 1125 * @param a an argument. 1126 * @param b another argument. 1127 * @return the smaller of {@code a} and {@code b}. 1128 */ 1129 public static double min(double a, double b) { 1130 return Math.min(a, b); 1131 } 1132 1133 /** 1134 * Returns the size of an ulp of the argument. An ulp, unit in 1135 * the last place, of a {@code double} value is the positive 1136 * distance between this floating-point value and the {@code 1137 * double} value next larger in magnitude. Note that for non-NaN 1138 * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 1139 * 1140 * <p>Special Cases: 1141 * <ul> 1142 * <li> If the argument is NaN, then the result is NaN. 1143 * <li> If the argument is positive or negative infinity, then the 1144 * result is positive infinity. 1145 * <li> If the argument is positive or negative zero, then the result is 1146 * {@code Double.MIN_VALUE}. 1147 * <li> If the argument is ±{@code Double.MAX_VALUE}, then 1148 * the result is equal to 2<sup>971</sup>. 1149 * </ul> 1150 * 1151 * @param d the floating-point value whose ulp is to be returned 1152 * @return the size of an ulp of the argument 1153 * @author Joseph D. Darcy 1154 * @since 1.5 1155 */ 1156 public static double ulp(double d) { 1157 return Math.ulp(d); 1158 } 1159 1160 /** 1161 * Returns the size of an ulp of the argument. An ulp, unit in 1162 * the last place, of a {@code float} value is the positive 1163 * distance between this floating-point value and the {@code 1164 * float} value next larger in magnitude. Note that for non-NaN 1165 * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 1166 * 1167 * <p>Special Cases: 1168 * <ul> 1169 * <li> If the argument is NaN, then the result is NaN. 1170 * <li> If the argument is positive or negative infinity, then the 1171 * result is positive infinity. 1172 * <li> If the argument is positive or negative zero, then the result is 1173 * {@code Float.MIN_VALUE}. 1174 * <li> If the argument is ±{@code Float.MAX_VALUE}, then 1175 * the result is equal to 2<sup>104</sup>. 1176 * </ul> 1177 * 1178 * @param f the floating-point value whose ulp is to be returned 1179 * @return the size of an ulp of the argument 1180 * @author Joseph D. Darcy 1181 * @since 1.5 1182 */ 1183 public static float ulp(float f) { 1184 return Math.ulp(f); 1185 } 1186 1187 /** 1188 * Returns the signum function of the argument; zero if the argument 1189 * is zero, 1.0 if the argument is greater than zero, -1.0 if the 1190 * argument is less than zero. 1191 * 1192 * <p>Special Cases: 1193 * <ul> 1194 * <li> If the argument is NaN, then the result is NaN. 1195 * <li> If the argument is positive zero or negative zero, then the 1196 * result is the same as the argument. 1197 * </ul> 1198 * 1199 * @param d the floating-point value whose signum is to be returned 1200 * @return the signum function of the argument 1201 * @author Joseph D. Darcy 1202 * @since 1.5 1203 */ 1204 public static double signum(double d) { 1205 return Math.signum(d); 1206 } 1207 1208 /** 1209 * Returns the signum function of the argument; zero if the argument 1210 * is zero, 1.0f if the argument is greater than zero, -1.0f if the 1211 * argument is less than zero. 1212 * 1213 * <p>Special Cases: 1214 * <ul> 1215 * <li> If the argument is NaN, then the result is NaN. 1216 * <li> If the argument is positive zero or negative zero, then the 1217 * result is the same as the argument. 1218 * </ul> 1219 * 1220 * @param f the floating-point value whose signum is to be returned 1221 * @return the signum function of the argument 1222 * @author Joseph D. Darcy 1223 * @since 1.5 1224 */ 1225 public static float signum(float f) { 1226 return Math.signum(f); 1227 } 1228 1229 /** 1230 * Returns the hyperbolic sine of a {@code double} value. 1231 * The hyperbolic sine of <i>x</i> is defined to be 1232 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2 1233 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1234 * 1235 * <p>Special cases: 1236 * <ul> 1237 * 1238 * <li>If the argument is NaN, then the result is NaN. 1239 * 1240 * <li>If the argument is infinite, then the result is an infinity 1241 * with the same sign as the argument. 1242 * 1243 * <li>If the argument is zero, then the result is a zero with the 1244 * same sign as the argument. 1245 * 1246 * </ul> 1247 * 1248 * @param x The number whose hyperbolic sine is to be returned. 1249 * @return The hyperbolic sine of {@code x}. 1250 * @since 1.5 1251 */ 1252 public static native double sinh(double x); 1253 1254 /** 1255 * Returns the hyperbolic cosine of a {@code double} value. 1256 * The hyperbolic cosine of <i>x</i> is defined to be 1257 * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2 1258 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1259 * 1260 * <p>Special cases: 1261 * <ul> 1262 * 1263 * <li>If the argument is NaN, then the result is NaN. 1264 * 1265 * <li>If the argument is infinite, then the result is positive 1266 * infinity. 1267 * 1268 * <li>If the argument is zero, then the result is {@code 1.0}. 1269 * 1270 * </ul> 1271 * 1272 * @param x The number whose hyperbolic cosine is to be returned. 1273 * @return The hyperbolic cosine of {@code x}. 1274 * @since 1.5 1275 */ 1276 public static native double cosh(double x); 1277 1278 /** 1279 * Returns the hyperbolic tangent of a {@code double} value. 1280 * The hyperbolic tangent of <i>x</i> is defined to be 1281 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>), 1282 * in other words, {@linkplain Math#sinh 1283 * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note 1284 * that the absolute value of the exact tanh is always less than 1285 * 1. 1286 * 1287 * <p>Special cases: 1288 * <ul> 1289 * 1290 * <li>If the argument is NaN, then the result is NaN. 1291 * 1292 * <li>If the argument is zero, then the result is a zero with the 1293 * same sign as the argument. 1294 * 1295 * <li>If the argument is positive infinity, then the result is 1296 * {@code +1.0}. 1297 * 1298 * <li>If the argument is negative infinity, then the result is 1299 * {@code -1.0}. 1300 * 1301 * </ul> 1302 * 1303 * @param x The number whose hyperbolic tangent is to be returned. 1304 * @return The hyperbolic tangent of {@code x}. 1305 * @since 1.5 1306 */ 1307 public static native double tanh(double x); 1308 1309 /** 1310 * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1311 * without intermediate overflow or underflow. 1312 * 1313 * <p>Special cases: 1314 * <ul> 1315 * 1316 * <li> If either argument is infinite, then the result 1317 * is positive infinity. 1318 * 1319 * <li> If either argument is NaN and neither argument is infinite, 1320 * then the result is NaN. 1321 * 1322 * </ul> 1323 * 1324 * @param x a value 1325 * @param y a value 1326 * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1327 * without intermediate overflow or underflow 1328 * @since 1.5 1329 */ 1330 public static native double hypot(double x, double y); 1331 1332 /** 1333 * Returns <i>e</i><sup>x</sup> -1. Note that for values of 1334 * <i>x</i> near 0, the exact sum of 1335 * {@code expm1(x)} + 1 is much closer to the true 1336 * result of <i>e</i><sup>x</sup> than {@code exp(x)}. 1337 * 1338 * <p>Special cases: 1339 * <ul> 1340 * <li>If the argument is NaN, the result is NaN. 1341 * 1342 * <li>If the argument is positive infinity, then the result is 1343 * positive infinity. 1344 * 1345 * <li>If the argument is negative infinity, then the result is 1346 * -1.0. 1347 * 1348 * <li>If the argument is zero, then the result is a zero with the 1349 * same sign as the argument. 1350 * 1351 * </ul> 1352 * 1353 * @param x the exponent to raise <i>e</i> to in the computation of 1354 * <i>e</i><sup>{@code x}</sup> -1. 1355 * @return the value <i>e</i><sup>{@code x}</sup> - 1. 1356 * @since 1.5 1357 */ 1358 public static native double expm1(double x); 1359 1360 /** 1361 * Returns the natural logarithm of the sum of the argument and 1. 1362 * Note that for small values {@code x}, the result of 1363 * {@code log1p(x)} is much closer to the true result of ln(1 1364 * + {@code x}) than the floating-point evaluation of 1365 * {@code log(1.0+x)}. 1366 * 1367 * <p>Special cases: 1368 * <ul> 1369 * 1370 * <li>If the argument is NaN or less than -1, then the result is 1371 * NaN. 1372 * 1373 * <li>If the argument is positive infinity, then the result is 1374 * positive infinity. 1375 * 1376 * <li>If the argument is negative one, then the result is 1377 * negative infinity. 1378 * 1379 * <li>If the argument is zero, then the result is a zero with the 1380 * same sign as the argument. 1381 * 1382 * </ul> 1383 * 1384 * @param x a value 1385 * @return the value ln({@code x} + 1), the natural 1386 * log of {@code x} + 1 1387 * @since 1.5 1388 */ 1389 public static native double log1p(double x); 1390 1391 /** 1392 * Returns the first floating-point argument with the sign of the 1393 * second floating-point argument. For this method, a NaN 1394 * {@code sign} argument is always treated as if it were 1395 * positive. 1396 * 1397 * @param magnitude the parameter providing the magnitude of the result 1398 * @param sign the parameter providing the sign of the result 1399 * @return a value with the magnitude of {@code magnitude} 1400 * and the sign of {@code sign}. 1401 * @since 1.6 1402 */ 1403 public static double copySign(double magnitude, double sign) { 1404 return Math.copySign(magnitude, (Double.isNaN(sign)?1.0d:sign)); 1405 } 1406 1407 /** 1408 * Returns the first floating-point argument with the sign of the 1409 * second floating-point argument. For this method, a NaN 1410 * {@code sign} argument is always treated as if it were 1411 * positive. 1412 * 1413 * @param magnitude the parameter providing the magnitude of the result 1414 * @param sign the parameter providing the sign of the result 1415 * @return a value with the magnitude of {@code magnitude} 1416 * and the sign of {@code sign}. 1417 * @since 1.6 1418 */ 1419 public static float copySign(float magnitude, float sign) { 1420 return Math.copySign(magnitude, (Float.isNaN(sign)?1.0f:sign)); 1421 } 1422 /** 1423 * Returns the unbiased exponent used in the representation of a 1424 * {@code float}. Special cases: 1425 * 1426 * <ul> 1427 * <li>If the argument is NaN or infinite, then the result is 1428 * {@link Float#MAX_EXPONENT} + 1. 1429 * <li>If the argument is zero or subnormal, then the result is 1430 * {@link Float#MIN_EXPONENT} -1. 1431 * </ul> 1432 * @param f a {@code float} value 1433 * @return the unbiased exponent of the argument 1434 * @since 1.6 1435 */ 1436 public static int getExponent(float f) { 1437 return Math.getExponent(f); 1438 } 1439 1440 /** 1441 * Returns the unbiased exponent used in the representation of a 1442 * {@code double}. Special cases: 1443 * 1444 * <ul> 1445 * <li>If the argument is NaN or infinite, then the result is 1446 * {@link Double#MAX_EXPONENT} + 1. 1447 * <li>If the argument is zero or subnormal, then the result is 1448 * {@link Double#MIN_EXPONENT} -1. 1449 * </ul> 1450 * @param d a {@code double} value 1451 * @return the unbiased exponent of the argument 1452 * @since 1.6 1453 */ 1454 public static int getExponent(double d) { 1455 return Math.getExponent(d); 1456 } 1457 1458 /** 1459 * Returns the floating-point number adjacent to the first 1460 * argument in the direction of the second argument. If both 1461 * arguments compare as equal the second argument is returned. 1462 * 1463 * <p>Special cases: 1464 * <ul> 1465 * <li> If either argument is a NaN, then NaN is returned. 1466 * 1467 * <li> If both arguments are signed zeros, {@code direction} 1468 * is returned unchanged (as implied by the requirement of 1469 * returning the second argument if the arguments compare as 1470 * equal). 1471 * 1472 * <li> If {@code start} is 1473 * ±{@link Double#MIN_VALUE} and {@code direction} 1474 * has a value such that the result should have a smaller 1475 * magnitude, then a zero with the same sign as {@code start} 1476 * is returned. 1477 * 1478 * <li> If {@code start} is infinite and 1479 * {@code direction} has a value such that the result should 1480 * have a smaller magnitude, {@link Double#MAX_VALUE} with the 1481 * same sign as {@code start} is returned. 1482 * 1483 * <li> If {@code start} is equal to ± 1484 * {@link Double#MAX_VALUE} and {@code direction} has a 1485 * value such that the result should have a larger magnitude, an 1486 * infinity with same sign as {@code start} is returned. 1487 * </ul> 1488 * 1489 * @param start starting floating-point value 1490 * @param direction value indicating which of 1491 * {@code start}'s neighbors or {@code start} should 1492 * be returned 1493 * @return The floating-point number adjacent to {@code start} in the 1494 * direction of {@code direction}. 1495 * @since 1.6 1496 */ 1497 public static double nextAfter(double start, double direction) { 1498 return Math.nextAfter(start, direction); 1499 } 1500 1501 /** 1502 * Returns the floating-point number adjacent to the first 1503 * argument in the direction of the second argument. If both 1504 * arguments compare as equal a value equivalent to the second argument 1505 * is returned. 1506 * 1507 * <p>Special cases: 1508 * <ul> 1509 * <li> If either argument is a NaN, then NaN is returned. 1510 * 1511 * <li> If both arguments are signed zeros, a value equivalent 1512 * to {@code direction} is returned. 1513 * 1514 * <li> If {@code start} is 1515 * ±{@link Float#MIN_VALUE} and {@code direction} 1516 * has a value such that the result should have a smaller 1517 * magnitude, then a zero with the same sign as {@code start} 1518 * is returned. 1519 * 1520 * <li> If {@code start} is infinite and 1521 * {@code direction} has a value such that the result should 1522 * have a smaller magnitude, {@link Float#MAX_VALUE} with the 1523 * same sign as {@code start} is returned. 1524 * 1525 * <li> If {@code start} is equal to ± 1526 * {@link Float#MAX_VALUE} and {@code direction} has a 1527 * value such that the result should have a larger magnitude, an 1528 * infinity with same sign as {@code start} is returned. 1529 * </ul> 1530 * 1531 * @param start starting floating-point value 1532 * @param direction value indicating which of 1533 * {@code start}'s neighbors or {@code start} should 1534 * be returned 1535 * @return The floating-point number adjacent to {@code start} in the 1536 * direction of {@code direction}. 1537 * @since 1.6 1538 */ 1539 public static float nextAfter(float start, double direction) { 1540 return Math.nextAfter(start, direction); 1541 } 1542 1543 /** 1544 * Returns the floating-point value adjacent to {@code d} in 1545 * the direction of positive infinity. This method is 1546 * semantically equivalent to {@code nextAfter(d, 1547 * Double.POSITIVE_INFINITY)}; however, a {@code nextUp} 1548 * implementation may run faster than its equivalent 1549 * {@code nextAfter} call. 1550 * 1551 * <p>Special Cases: 1552 * <ul> 1553 * <li> If the argument is NaN, the result is NaN. 1554 * 1555 * <li> If the argument is positive infinity, the result is 1556 * positive infinity. 1557 * 1558 * <li> If the argument is zero, the result is 1559 * {@link Double#MIN_VALUE} 1560 * 1561 * </ul> 1562 * 1563 * @param d starting floating-point value 1564 * @return The adjacent floating-point value closer to positive 1565 * infinity. 1566 * @since 1.6 1567 */ 1568 public static double nextUp(double d) { 1569 return Math.nextUp(d); 1570 } 1571 1572 /** 1573 * Returns the floating-point value adjacent to {@code f} in 1574 * the direction of positive infinity. This method is 1575 * semantically equivalent to {@code nextAfter(f, 1576 * Float.POSITIVE_INFINITY)}; however, a {@code nextUp} 1577 * implementation may run faster than its equivalent 1578 * {@code nextAfter} call. 1579 * 1580 * <p>Special Cases: 1581 * <ul> 1582 * <li> If the argument is NaN, the result is NaN. 1583 * 1584 * <li> If the argument is positive infinity, the result is 1585 * positive infinity. 1586 * 1587 * <li> If the argument is zero, the result is 1588 * {@link Float#MIN_VALUE} 1589 * 1590 * </ul> 1591 * 1592 * @param f starting floating-point value 1593 * @return The adjacent floating-point value closer to positive 1594 * infinity. 1595 * @since 1.6 1596 */ 1597 public static float nextUp(float f) { 1598 return Math.nextUp(f); 1599 } 1600 1601 /** 1602 * Returns the floating-point value adjacent to {@code d} in 1603 * the direction of negative infinity. This method is 1604 * semantically equivalent to {@code nextAfter(d, 1605 * Double.NEGATIVE_INFINITY)}; however, a 1606 * {@code nextDown} implementation may run faster than its 1607 * equivalent {@code nextAfter} call. 1608 * 1609 * <p>Special Cases: 1610 * <ul> 1611 * <li> If the argument is NaN, the result is NaN. 1612 * 1613 * <li> If the argument is negative infinity, the result is 1614 * negative infinity. 1615 * 1616 * <li> If the argument is zero, the result is 1617 * {@code -Double.MIN_VALUE} 1618 * 1619 * </ul> 1620 * 1621 * @param d starting floating-point value 1622 * @return The adjacent floating-point value closer to negative 1623 * infinity. 1624 * @since 1.8 1625 */ 1626 public static double nextDown(double d) { 1627 return Math.nextDown(d); 1628 } 1629 1630 /** 1631 * Returns the floating-point value adjacent to {@code f} in 1632 * the direction of negative infinity. This method is 1633 * semantically equivalent to {@code nextAfter(f, 1634 * Float.NEGATIVE_INFINITY)}; however, a 1635 * {@code nextDown} implementation may run faster than its 1636 * equivalent {@code nextAfter} call. 1637 * 1638 * <p>Special Cases: 1639 * <ul> 1640 * <li> If the argument is NaN, the result is NaN. 1641 * 1642 * <li> If the argument is negative infinity, the result is 1643 * negative infinity. 1644 * 1645 * <li> If the argument is zero, the result is 1646 * {@code -Float.MIN_VALUE} 1647 * 1648 * </ul> 1649 * 1650 * @param f starting floating-point value 1651 * @return The adjacent floating-point value closer to negative 1652 * infinity. 1653 * @since 1.8 1654 */ 1655 public static float nextDown(float f) { 1656 return Math.nextDown(f); 1657 } 1658 1659 /** 1660 * Returns {@code d} × 1661 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1662 * by a single correctly rounded floating-point multiply to a 1663 * member of the double value set. See the Java 1664 * Language Specification for a discussion of floating-point 1665 * value sets. If the exponent of the result is between {@link 1666 * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the 1667 * answer is calculated exactly. If the exponent of the result 1668 * would be larger than {@code Double.MAX_EXPONENT}, an 1669 * infinity is returned. Note that if the result is subnormal, 1670 * precision may be lost; that is, when {@code scalb(x, n)} 1671 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1672 * <i>x</i>. When the result is non-NaN, the result has the same 1673 * sign as {@code d}. 1674 * 1675 * <p>Special cases: 1676 * <ul> 1677 * <li> If the first argument is NaN, NaN is returned. 1678 * <li> If the first argument is infinite, then an infinity of the 1679 * same sign is returned. 1680 * <li> If the first argument is zero, then a zero of the same 1681 * sign is returned. 1682 * </ul> 1683 * 1684 * @param d number to be scaled by a power of two. 1685 * @param scaleFactor power of 2 used to scale {@code d} 1686 * @return {@code d} × 2<sup>{@code scaleFactor}</sup> 1687 * @since 1.6 1688 */ 1689 public static double scalb(double d, int scaleFactor) { 1690 return Math.scalb(d, scaleFactor); 1691 } 1692 1693 /** 1694 * Returns {@code f} × 1695 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1696 * by a single correctly rounded floating-point multiply to a 1697 * member of the float value set. See the Java 1698 * Language Specification for a discussion of floating-point 1699 * value sets. If the exponent of the result is between {@link 1700 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the 1701 * answer is calculated exactly. If the exponent of the result 1702 * would be larger than {@code Float.MAX_EXPONENT}, an 1703 * infinity is returned. Note that if the result is subnormal, 1704 * precision may be lost; that is, when {@code scalb(x, n)} 1705 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1706 * <i>x</i>. When the result is non-NaN, the result has the same 1707 * sign as {@code f}. 1708 * 1709 * <p>Special cases: 1710 * <ul> 1711 * <li> If the first argument is NaN, NaN is returned. 1712 * <li> If the first argument is infinite, then an infinity of the 1713 * same sign is returned. 1714 * <li> If the first argument is zero, then a zero of the same 1715 * sign is returned. 1716 * </ul> 1717 * 1718 * @param f number to be scaled by a power of two. 1719 * @param scaleFactor power of 2 used to scale {@code f} 1720 * @return {@code f} × 2<sup>{@code scaleFactor}</sup> 1721 * @since 1.6 1722 */ 1723 public static float scalb(float f, int scaleFactor) { 1724 return Math.scalb(f, scaleFactor); 1725 } 1726 }