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 jdk.internal.math.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 fused multiply-accumulate of the three arguments; 1139 * that is, returns the exact product of the first two arguments 1140 * summed with the third argument and then rounded once to the 1141 * nearest {@code double}. 1142 * 1143 * The rounding is done using the {@linkplain 1144 * java.math.RoundingMode#HALF_EVEN round to nearest even 1145 * rounding mode}. 1146 * 1147 * In contrast, if {@code a * b + c} is evaluated as a regular 1148 * floating-point expression, two rounding errors are involved, 1149 * the first for the multiply operation, the second for the 1150 * addition operation. 1151 * 1152 * <p>Special cases: 1153 * <ul> 1154 * <li> If any argument is NaN, the result is NaN. 1155 * 1156 * <li> If one of the first two arguments is infinite and the 1157 * other is zero, the result is NaN. 1158 * 1159 * <li> If the exact product of the first two arguments is infinite 1160 * (in other words, at least one of the arguments is infinite and 1161 * the other is neither zero nor NaN) and the third argument is an 1162 * infinity of the opposite sign, the result is NaN. 1163 * 1164 * </ul> 1165 * 1166 * <p>Note that {@code fusedMac(a, 1.0, c)} returns the same 1167 * result as ({@code a + c}). However, 1168 * {@code fusedMac(a, b, +0.0)} does <em>not</em> always return the 1169 * same result as ({@code a * b}) since 1170 * {@code fusedMac(-0.0, +0.0, +0.0)} is {@code +0.0} while 1171 * ({@code 0.0 * +0.0}) is {@code -0.0}; {@code fusedMac(a, b, -0.0)} is 1172 * equivalent to ({@code a * b}) however. 1173 * 1174 * @param a a value 1175 * @param b a value 1176 * @param c a value 1177 * 1178 * @return (<i>a</i> × <i>b</i> + <i>c</i>) 1179 * computed, as if with unlimited range and precision, and rounded 1180 * once to the nearest {@code double} value 1181 */ 1182 public static double fusedMac(double a, double b, double c) { 1183 return Math.fusedMac(a, b, c); 1184 } 1185 1186 /** 1187 * Returns the fused multiply-accumulate of the three arguments; 1188 * that is, returns the exact product of the first two arguments 1189 * summed with the third argument and then rounded once to the 1190 * nearest {@code float}. 1191 * 1192 * The rounding is done using the {@linkplain 1193 * java.math.RoundingMode#HALF_EVEN round to nearest even 1194 * rounding mode}. 1195 * 1196 * In contrast, if {@code a * b + c} is evaluated as a regular 1197 * floating-point expression, two rounding errors are involved, 1198 * the first for the multiply operation, the second for the 1199 * addition operation. 1200 * 1201 * <p>Special cases: 1202 * <ul> 1203 * <li> If any argument is NaN, the result is NaN. 1204 * 1205 * <li> If one of the first two arguments is infinite and the 1206 * other is zero, the result is NaN. 1207 * 1208 * <li> If the exact product of the first two arguments is infinite 1209 * (in other words, at least one of the arguments is infinite and 1210 * the other is neither zero nor NaN) and the third argument is an 1211 * infinity of the opposite sign, the result is NaN. 1212 * 1213 * </ul> 1214 * 1215 * <p>Note that {@code fusedMac(a, 1.0f, c)} returns the same 1216 * result as ({@code a + c}). However, 1217 * {@code fusedMac(a, b, +0.0f)} does <em>not</em> always return the 1218 * same result as ({@code a * b}) since 1219 * {@code fusedMac(-0.0f, +0.0f, +0.0f)} is {@code +0.0f} while 1220 * ({@code 0.0f * +0.0f}) is {@code -0.0f}; {@code fusedMac(a, b, -0.0f)} is 1221 * equivalent to ({@code a * b}) however. 1222 * 1223 * @param a a value 1224 * @param b a value 1225 * @param c a value 1226 * 1227 * @return (<i>a</i> × <i>b</i> + <i>c</i>) 1228 * computed, as if with unlimited range and precision, and rounded 1229 * once to the nearest {@code float} value 1230 */ 1231 public static float fusedMac(float a, float b, float c) { 1232 return Math.fusedMac(a, b, c); 1233 } 1234 1235 /** 1236 * Returns the size of an ulp of the argument. An ulp, unit in 1237 * the last place, of a {@code double} value is the positive 1238 * distance between this floating-point value and the {@code 1239 * double} value next larger in magnitude. Note that for non-NaN 1240 * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 1241 * 1242 * <p>Special Cases: 1243 * <ul> 1244 * <li> If the argument is NaN, then the result is NaN. 1245 * <li> If the argument is positive or negative infinity, then the 1246 * result is positive infinity. 1247 * <li> If the argument is positive or negative zero, then the result is 1248 * {@code Double.MIN_VALUE}. 1249 * <li> If the argument is ±{@code Double.MAX_VALUE}, then 1250 * the result is equal to 2<sup>971</sup>. 1251 * </ul> 1252 * 1253 * @param d the floating-point value whose ulp is to be returned 1254 * @return the size of an ulp of the argument 1255 * @author Joseph D. Darcy 1256 * @since 1.5 1257 */ 1258 public static double ulp(double d) { 1259 return Math.ulp(d); 1260 } 1261 1262 /** 1263 * Returns the size of an ulp of the argument. An ulp, unit in 1264 * the last place, of a {@code float} value is the positive 1265 * distance between this floating-point value and the {@code 1266 * float} value next larger in magnitude. Note that for non-NaN 1267 * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 1268 * 1269 * <p>Special Cases: 1270 * <ul> 1271 * <li> If the argument is NaN, then the result is NaN. 1272 * <li> If the argument is positive or negative infinity, then the 1273 * result is positive infinity. 1274 * <li> If the argument is positive or negative zero, then the result is 1275 * {@code Float.MIN_VALUE}. 1276 * <li> If the argument is ±{@code Float.MAX_VALUE}, then 1277 * the result is equal to 2<sup>104</sup>. 1278 * </ul> 1279 * 1280 * @param f the floating-point value whose ulp is to be returned 1281 * @return the size of an ulp of the argument 1282 * @author Joseph D. Darcy 1283 * @since 1.5 1284 */ 1285 public static float ulp(float f) { 1286 return Math.ulp(f); 1287 } 1288 1289 /** 1290 * Returns the signum function of the argument; zero if the argument 1291 * is zero, 1.0 if the argument is greater than zero, -1.0 if the 1292 * argument is less than zero. 1293 * 1294 * <p>Special Cases: 1295 * <ul> 1296 * <li> If the argument is NaN, then the result is NaN. 1297 * <li> If the argument is positive zero or negative zero, then the 1298 * result is the same as the argument. 1299 * </ul> 1300 * 1301 * @param d the floating-point value whose signum is to be returned 1302 * @return the signum function of the argument 1303 * @author Joseph D. Darcy 1304 * @since 1.5 1305 */ 1306 public static double signum(double d) { 1307 return Math.signum(d); 1308 } 1309 1310 /** 1311 * Returns the signum function of the argument; zero if the argument 1312 * is zero, 1.0f if the argument is greater than zero, -1.0f if the 1313 * argument is less than zero. 1314 * 1315 * <p>Special Cases: 1316 * <ul> 1317 * <li> If the argument is NaN, then the result is NaN. 1318 * <li> If the argument is positive zero or negative zero, then the 1319 * result is the same as the argument. 1320 * </ul> 1321 * 1322 * @param f the floating-point value whose signum is to be returned 1323 * @return the signum function of the argument 1324 * @author Joseph D. Darcy 1325 * @since 1.5 1326 */ 1327 public static float signum(float f) { 1328 return Math.signum(f); 1329 } 1330 1331 /** 1332 * Returns the hyperbolic sine of a {@code double} value. 1333 * The hyperbolic sine of <i>x</i> is defined to be 1334 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2 1335 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1336 * 1337 * <p>Special cases: 1338 * <ul> 1339 * 1340 * <li>If the argument is NaN, then the result is NaN. 1341 * 1342 * <li>If the argument is infinite, then the result is an infinity 1343 * with the same sign as the argument. 1344 * 1345 * <li>If the argument is zero, then the result is a zero with the 1346 * same sign as the argument. 1347 * 1348 * </ul> 1349 * 1350 * @param x The number whose hyperbolic sine is to be returned. 1351 * @return The hyperbolic sine of {@code x}. 1352 * @since 1.5 1353 */ 1354 public static native double sinh(double x); 1355 1356 /** 1357 * Returns the hyperbolic cosine of a {@code double} value. 1358 * The hyperbolic cosine of <i>x</i> is defined to be 1359 * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2 1360 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1361 * 1362 * <p>Special cases: 1363 * <ul> 1364 * 1365 * <li>If the argument is NaN, then the result is NaN. 1366 * 1367 * <li>If the argument is infinite, then the result is positive 1368 * infinity. 1369 * 1370 * <li>If the argument is zero, then the result is {@code 1.0}. 1371 * 1372 * </ul> 1373 * 1374 * @param x The number whose hyperbolic cosine is to be returned. 1375 * @return The hyperbolic cosine of {@code x}. 1376 * @since 1.5 1377 */ 1378 public static native double cosh(double x); 1379 1380 /** 1381 * Returns the hyperbolic tangent of a {@code double} value. 1382 * The hyperbolic tangent of <i>x</i> is defined to be 1383 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>), 1384 * in other words, {@linkplain Math#sinh 1385 * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note 1386 * that the absolute value of the exact tanh is always less than 1387 * 1. 1388 * 1389 * <p>Special cases: 1390 * <ul> 1391 * 1392 * <li>If the argument is NaN, then the result is NaN. 1393 * 1394 * <li>If the argument is zero, then the result is a zero with the 1395 * same sign as the argument. 1396 * 1397 * <li>If the argument is positive infinity, then the result is 1398 * {@code +1.0}. 1399 * 1400 * <li>If the argument is negative infinity, then the result is 1401 * {@code -1.0}. 1402 * 1403 * </ul> 1404 * 1405 * @param x The number whose hyperbolic tangent is to be returned. 1406 * @return The hyperbolic tangent of {@code x}. 1407 * @since 1.5 1408 */ 1409 public static native double tanh(double x); 1410 1411 /** 1412 * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1413 * without intermediate overflow or underflow. 1414 * 1415 * <p>Special cases: 1416 * <ul> 1417 * 1418 * <li> If either argument is infinite, then the result 1419 * is positive infinity. 1420 * 1421 * <li> If either argument is NaN and neither argument is infinite, 1422 * then the result is NaN. 1423 * 1424 * </ul> 1425 * 1426 * @param x a value 1427 * @param y a value 1428 * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1429 * without intermediate overflow or underflow 1430 * @since 1.5 1431 */ 1432 public static double hypot(double x, double y) { 1433 return FdLibm.Hypot.compute(x, y); 1434 } 1435 1436 /** 1437 * Returns <i>e</i><sup>x</sup> -1. Note that for values of 1438 * <i>x</i> near 0, the exact sum of 1439 * {@code expm1(x)} + 1 is much closer to the true 1440 * result of <i>e</i><sup>x</sup> than {@code exp(x)}. 1441 * 1442 * <p>Special cases: 1443 * <ul> 1444 * <li>If the argument is NaN, the result is NaN. 1445 * 1446 * <li>If the argument is positive infinity, then the result is 1447 * positive infinity. 1448 * 1449 * <li>If the argument is negative infinity, then the result is 1450 * -1.0. 1451 * 1452 * <li>If the argument is zero, then the result is a zero with the 1453 * same sign as the argument. 1454 * 1455 * </ul> 1456 * 1457 * @param x the exponent to raise <i>e</i> to in the computation of 1458 * <i>e</i><sup>{@code x}</sup> -1. 1459 * @return the value <i>e</i><sup>{@code x}</sup> - 1. 1460 * @since 1.5 1461 */ 1462 public static native double expm1(double x); 1463 1464 /** 1465 * Returns the natural logarithm of the sum of the argument and 1. 1466 * Note that for small values {@code x}, the result of 1467 * {@code log1p(x)} is much closer to the true result of ln(1 1468 * + {@code x}) than the floating-point evaluation of 1469 * {@code log(1.0+x)}. 1470 * 1471 * <p>Special cases: 1472 * <ul> 1473 * 1474 * <li>If the argument is NaN or less than -1, then the result is 1475 * NaN. 1476 * 1477 * <li>If the argument is positive infinity, then the result is 1478 * positive infinity. 1479 * 1480 * <li>If the argument is negative one, then the result is 1481 * negative infinity. 1482 * 1483 * <li>If the argument is zero, then the result is a zero with the 1484 * same sign as the argument. 1485 * 1486 * </ul> 1487 * 1488 * @param x a value 1489 * @return the value ln({@code x} + 1), the natural 1490 * log of {@code x} + 1 1491 * @since 1.5 1492 */ 1493 public static native double log1p(double x); 1494 1495 /** 1496 * Returns the first floating-point argument with the sign of the 1497 * second floating-point argument. For this method, a NaN 1498 * {@code sign} argument is always treated as if it were 1499 * positive. 1500 * 1501 * @param magnitude the parameter providing the magnitude of the result 1502 * @param sign the parameter providing the sign of the result 1503 * @return a value with the magnitude of {@code magnitude} 1504 * and the sign of {@code sign}. 1505 * @since 1.6 1506 */ 1507 public static double copySign(double magnitude, double sign) { 1508 return Math.copySign(magnitude, (Double.isNaN(sign)?1.0d:sign)); 1509 } 1510 1511 /** 1512 * Returns the first floating-point argument with the sign of the 1513 * second floating-point argument. For this method, a NaN 1514 * {@code sign} argument is always treated as if it were 1515 * positive. 1516 * 1517 * @param magnitude the parameter providing the magnitude of the result 1518 * @param sign the parameter providing the sign of the result 1519 * @return a value with the magnitude of {@code magnitude} 1520 * and the sign of {@code sign}. 1521 * @since 1.6 1522 */ 1523 public static float copySign(float magnitude, float sign) { 1524 return Math.copySign(magnitude, (Float.isNaN(sign)?1.0f:sign)); 1525 } 1526 /** 1527 * Returns the unbiased exponent used in the representation of a 1528 * {@code float}. Special cases: 1529 * 1530 * <ul> 1531 * <li>If the argument is NaN or infinite, then the result is 1532 * {@link Float#MAX_EXPONENT} + 1. 1533 * <li>If the argument is zero or subnormal, then the result is 1534 * {@link Float#MIN_EXPONENT} -1. 1535 * </ul> 1536 * @param f a {@code float} value 1537 * @return the unbiased exponent of the argument 1538 * @since 1.6 1539 */ 1540 public static int getExponent(float f) { 1541 return Math.getExponent(f); 1542 } 1543 1544 /** 1545 * Returns the unbiased exponent used in the representation of a 1546 * {@code double}. Special cases: 1547 * 1548 * <ul> 1549 * <li>If the argument is NaN or infinite, then the result is 1550 * {@link Double#MAX_EXPONENT} + 1. 1551 * <li>If the argument is zero or subnormal, then the result is 1552 * {@link Double#MIN_EXPONENT} -1. 1553 * </ul> 1554 * @param d a {@code double} value 1555 * @return the unbiased exponent of the argument 1556 * @since 1.6 1557 */ 1558 public static int getExponent(double d) { 1559 return Math.getExponent(d); 1560 } 1561 1562 /** 1563 * Returns the floating-point number adjacent to the first 1564 * argument in the direction of the second argument. If both 1565 * arguments compare as equal the second argument is returned. 1566 * 1567 * <p>Special cases: 1568 * <ul> 1569 * <li> If either argument is a NaN, then NaN is returned. 1570 * 1571 * <li> If both arguments are signed zeros, {@code direction} 1572 * is returned unchanged (as implied by the requirement of 1573 * returning the second argument if the arguments compare as 1574 * equal). 1575 * 1576 * <li> If {@code start} is 1577 * ±{@link Double#MIN_VALUE} and {@code direction} 1578 * has a value such that the result should have a smaller 1579 * magnitude, then a zero with the same sign as {@code start} 1580 * is returned. 1581 * 1582 * <li> If {@code start} is infinite and 1583 * {@code direction} has a value such that the result should 1584 * have a smaller magnitude, {@link Double#MAX_VALUE} with the 1585 * same sign as {@code start} is returned. 1586 * 1587 * <li> If {@code start} is equal to ± 1588 * {@link Double#MAX_VALUE} and {@code direction} has a 1589 * value such that the result should have a larger magnitude, an 1590 * infinity with same sign as {@code start} is returned. 1591 * </ul> 1592 * 1593 * @param start starting floating-point value 1594 * @param direction value indicating which of 1595 * {@code start}'s neighbors or {@code start} should 1596 * be returned 1597 * @return The floating-point number adjacent to {@code start} in the 1598 * direction of {@code direction}. 1599 * @since 1.6 1600 */ 1601 public static double nextAfter(double start, double direction) { 1602 return Math.nextAfter(start, direction); 1603 } 1604 1605 /** 1606 * Returns the floating-point number adjacent to the first 1607 * argument in the direction of the second argument. If both 1608 * arguments compare as equal a value equivalent to the second argument 1609 * is returned. 1610 * 1611 * <p>Special cases: 1612 * <ul> 1613 * <li> If either argument is a NaN, then NaN is returned. 1614 * 1615 * <li> If both arguments are signed zeros, a value equivalent 1616 * to {@code direction} is returned. 1617 * 1618 * <li> If {@code start} is 1619 * ±{@link Float#MIN_VALUE} and {@code direction} 1620 * has a value such that the result should have a smaller 1621 * magnitude, then a zero with the same sign as {@code start} 1622 * is returned. 1623 * 1624 * <li> If {@code start} is infinite and 1625 * {@code direction} has a value such that the result should 1626 * have a smaller magnitude, {@link Float#MAX_VALUE} with the 1627 * same sign as {@code start} is returned. 1628 * 1629 * <li> If {@code start} is equal to ± 1630 * {@link Float#MAX_VALUE} and {@code direction} has a 1631 * value such that the result should have a larger magnitude, an 1632 * infinity with same sign as {@code start} is returned. 1633 * </ul> 1634 * 1635 * @param start starting floating-point value 1636 * @param direction value indicating which of 1637 * {@code start}'s neighbors or {@code start} should 1638 * be returned 1639 * @return The floating-point number adjacent to {@code start} in the 1640 * direction of {@code direction}. 1641 * @since 1.6 1642 */ 1643 public static float nextAfter(float start, double direction) { 1644 return Math.nextAfter(start, direction); 1645 } 1646 1647 /** 1648 * Returns the floating-point value adjacent to {@code d} in 1649 * the direction of positive infinity. This method is 1650 * semantically equivalent to {@code nextAfter(d, 1651 * Double.POSITIVE_INFINITY)}; however, a {@code nextUp} 1652 * implementation may run faster than its equivalent 1653 * {@code nextAfter} call. 1654 * 1655 * <p>Special Cases: 1656 * <ul> 1657 * <li> If the argument is NaN, the result is NaN. 1658 * 1659 * <li> If the argument is positive infinity, the result is 1660 * positive infinity. 1661 * 1662 * <li> If the argument is zero, the result is 1663 * {@link Double#MIN_VALUE} 1664 * 1665 * </ul> 1666 * 1667 * @param d starting floating-point value 1668 * @return The adjacent floating-point value closer to positive 1669 * infinity. 1670 * @since 1.6 1671 */ 1672 public static double nextUp(double d) { 1673 return Math.nextUp(d); 1674 } 1675 1676 /** 1677 * Returns the floating-point value adjacent to {@code f} in 1678 * the direction of positive infinity. This method is 1679 * semantically equivalent to {@code nextAfter(f, 1680 * Float.POSITIVE_INFINITY)}; however, a {@code nextUp} 1681 * implementation may run faster than its equivalent 1682 * {@code nextAfter} call. 1683 * 1684 * <p>Special Cases: 1685 * <ul> 1686 * <li> If the argument is NaN, the result is NaN. 1687 * 1688 * <li> If the argument is positive infinity, the result is 1689 * positive infinity. 1690 * 1691 * <li> If the argument is zero, the result is 1692 * {@link Float#MIN_VALUE} 1693 * 1694 * </ul> 1695 * 1696 * @param f starting floating-point value 1697 * @return The adjacent floating-point value closer to positive 1698 * infinity. 1699 * @since 1.6 1700 */ 1701 public static float nextUp(float f) { 1702 return Math.nextUp(f); 1703 } 1704 1705 /** 1706 * Returns the floating-point value adjacent to {@code d} in 1707 * the direction of negative infinity. This method is 1708 * semantically equivalent to {@code nextAfter(d, 1709 * Double.NEGATIVE_INFINITY)}; however, a 1710 * {@code nextDown} implementation may run faster than its 1711 * equivalent {@code nextAfter} call. 1712 * 1713 * <p>Special Cases: 1714 * <ul> 1715 * <li> If the argument is NaN, the result is NaN. 1716 * 1717 * <li> If the argument is negative infinity, the result is 1718 * negative infinity. 1719 * 1720 * <li> If the argument is zero, the result is 1721 * {@code -Double.MIN_VALUE} 1722 * 1723 * </ul> 1724 * 1725 * @param d starting floating-point value 1726 * @return The adjacent floating-point value closer to negative 1727 * infinity. 1728 * @since 1.8 1729 */ 1730 public static double nextDown(double d) { 1731 return Math.nextDown(d); 1732 } 1733 1734 /** 1735 * Returns the floating-point value adjacent to {@code f} in 1736 * the direction of negative infinity. This method is 1737 * semantically equivalent to {@code nextAfter(f, 1738 * Float.NEGATIVE_INFINITY)}; however, a 1739 * {@code nextDown} implementation may run faster than its 1740 * equivalent {@code nextAfter} call. 1741 * 1742 * <p>Special Cases: 1743 * <ul> 1744 * <li> If the argument is NaN, the result is NaN. 1745 * 1746 * <li> If the argument is negative infinity, the result is 1747 * negative infinity. 1748 * 1749 * <li> If the argument is zero, the result is 1750 * {@code -Float.MIN_VALUE} 1751 * 1752 * </ul> 1753 * 1754 * @param f starting floating-point value 1755 * @return The adjacent floating-point value closer to negative 1756 * infinity. 1757 * @since 1.8 1758 */ 1759 public static float nextDown(float f) { 1760 return Math.nextDown(f); 1761 } 1762 1763 /** 1764 * Returns {@code d} × 1765 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1766 * by a single correctly rounded floating-point multiply to a 1767 * member of the double value set. See the Java 1768 * Language Specification for a discussion of floating-point 1769 * value sets. If the exponent of the result is between {@link 1770 * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the 1771 * answer is calculated exactly. If the exponent of the result 1772 * would be larger than {@code Double.MAX_EXPONENT}, an 1773 * infinity is returned. Note that if the result is subnormal, 1774 * precision may be lost; that is, when {@code scalb(x, n)} 1775 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1776 * <i>x</i>. When the result is non-NaN, the result has the same 1777 * sign as {@code d}. 1778 * 1779 * <p>Special cases: 1780 * <ul> 1781 * <li> If the first argument is NaN, NaN is returned. 1782 * <li> If the first argument is infinite, then an infinity of the 1783 * same sign is returned. 1784 * <li> If the first argument is zero, then a zero of the same 1785 * sign is returned. 1786 * </ul> 1787 * 1788 * @param d number to be scaled by a power of two. 1789 * @param scaleFactor power of 2 used to scale {@code d} 1790 * @return {@code d} × 2<sup>{@code scaleFactor}</sup> 1791 * @since 1.6 1792 */ 1793 public static double scalb(double d, int scaleFactor) { 1794 return Math.scalb(d, scaleFactor); 1795 } 1796 1797 /** 1798 * Returns {@code f} × 1799 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1800 * by a single correctly rounded floating-point multiply to a 1801 * member of the float value set. See the Java 1802 * Language Specification for a discussion of floating-point 1803 * value sets. If the exponent of the result is between {@link 1804 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the 1805 * answer is calculated exactly. If the exponent of the result 1806 * would be larger than {@code Float.MAX_EXPONENT}, an 1807 * infinity is returned. Note that if the result is subnormal, 1808 * precision may be lost; that is, when {@code scalb(x, n)} 1809 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1810 * <i>x</i>. When the result is non-NaN, the result has the same 1811 * sign as {@code f}. 1812 * 1813 * <p>Special cases: 1814 * <ul> 1815 * <li> If the first argument is NaN, NaN is returned. 1816 * <li> If the first argument is infinite, then an infinity of the 1817 * same sign is returned. 1818 * <li> If the first argument is zero, then a zero of the same 1819 * sign is returned. 1820 * </ul> 1821 * 1822 * @param f number to be scaled by a power of two. 1823 * @param scaleFactor power of 2 used to scale {@code f} 1824 * @return {@code f} × 2<sup>{@code scaleFactor}</sup> 1825 * @since 1.6 1826 */ 1827 public static float scalb(float f, int scaleFactor) { 1828 return Math.scalb(f, scaleFactor); 1829 } 1830 }