1 /* 2 * Copyright (c) 1999, 2016, 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 by one, decrement by one, 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, throwing an exception if the result 807 * 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,int) 814 * @since 9 815 */ 816 public static long multiplyExact(long x, int y) { 817 return Math.multiplyExact(x, y); 818 } 819 820 /** 821 * Returns the product of the arguments, 822 * throwing an exception if the result overflows a {@code long}. 823 * 824 * @param x the first value 825 * @param y the second value 826 * @return the result 827 * @throws ArithmeticException if the result overflows a long 828 * @see Math#multiplyExact(long,long) 829 * @since 1.8 830 */ 831 public static long multiplyExact(long x, long y) { 832 return Math.multiplyExact(x, y); 833 } 834 835 /** 836 * Returns the value of the {@code long} argument; 837 * throwing an exception if the value overflows an {@code int}. 838 * 839 * @param value the long value 840 * @return the argument as an int 841 * @throws ArithmeticException if the {@code argument} overflows an int 842 * @see Math#toIntExact(long) 843 * @since 1.8 844 */ 845 public static int toIntExact(long value) { 846 return Math.toIntExact(value); 847 } 848 849 /** 850 * Returns the exact mathematical product of the arguments. 851 * 852 * @param x the first value 853 * @param y the second value 854 * @return the result 855 * @see Math#multiplyFull(int,int) 856 * @since 9 857 */ 858 public static long multiplyFull(int x, int y) { 859 return Math.multiplyFull(x, y); 860 } 861 862 /** 863 * Returns as a {@code long} the most significant 64 bits of the 128-bit 864 * product of two 64-bit factors. 865 * 866 * @param x the first value 867 * @param y the second value 868 * @return the result 869 * @see Math#multiplyHigh(long,long) 870 * @since 9 871 */ 872 public static long multiplyHigh(long x, long y) { 873 return Math.multiplyHigh(x, y); 874 } 875 876 /** 877 * Returns the largest (closest to positive infinity) 878 * {@code int} value that is less than or equal to the algebraic quotient. 879 * There is one special case, if the dividend is the 880 * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1}, 881 * then integer overflow occurs and 882 * the result is equal to the {@code Integer.MIN_VALUE}. 883 * <p> 884 * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and 885 * a comparison to the integer division {@code /} operator. 886 * 887 * @param x the dividend 888 * @param y the divisor 889 * @return the largest (closest to positive infinity) 890 * {@code int} value that is less than or equal to the algebraic quotient. 891 * @throws ArithmeticException if the divisor {@code y} is zero 892 * @see Math#floorDiv(int, int) 893 * @see Math#floor(double) 894 * @since 1.8 895 */ 896 public static int floorDiv(int x, int y) { 897 return Math.floorDiv(x, y); 898 } 899 900 /** 901 * Returns the largest (closest to positive infinity) 902 * {@code long} value that is less than or equal to the algebraic quotient. 903 * There is one special case, if the dividend is the 904 * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1}, 905 * then integer overflow occurs and 906 * the result is equal to {@code Long.MIN_VALUE}. 907 * <p> 908 * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and 909 * a comparison to the integer division {@code /} operator. 910 * 911 * @param x the dividend 912 * @param y the divisor 913 * @return the largest (closest to positive infinity) 914 * {@code int} value that is less than or equal to the algebraic quotient. 915 * @throws ArithmeticException if the divisor {@code y} is zero 916 * @see Math#floorDiv(long, int) 917 * @see Math#floor(double) 918 * @since 9 919 */ 920 public static long floorDiv(long x, int y) { 921 return Math.floorDiv(x, y); 922 } 923 924 /** 925 * Returns the largest (closest to positive infinity) 926 * {@code long} value that is less than or equal to the algebraic quotient. 927 * There is one special case, if the dividend is the 928 * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1}, 929 * then integer overflow occurs and 930 * the result is equal to the {@code Long.MIN_VALUE}. 931 * <p> 932 * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and 933 * a comparison to the integer division {@code /} operator. 934 * 935 * @param x the dividend 936 * @param y the divisor 937 * @return the largest (closest to positive infinity) 938 * {@code long} value that is less than or equal to the algebraic quotient. 939 * @throws ArithmeticException if the divisor {@code y} is zero 940 * @see Math#floorDiv(long, long) 941 * @see Math#floor(double) 942 * @since 1.8 943 */ 944 public static long floorDiv(long x, long y) { 945 return Math.floorDiv(x, y); 946 } 947 948 /** 949 * Returns the floor modulus of the {@code int} arguments. 950 * <p> 951 * The floor modulus is {@code x - (floorDiv(x, y) * y)}, 952 * has the same sign as the divisor {@code y}, and 953 * is in the range of {@code -abs(y) < r < +abs(y)}. 954 * <p> 955 * The relationship between {@code floorDiv} and {@code floorMod} is such that: 956 * <ul> 957 * <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x} 958 * </ul> 959 * <p> 960 * See {@link Math#floorMod(int, int) Math.floorMod} for examples and 961 * a comparison to the {@code %} operator. 962 * 963 * @param x the dividend 964 * @param y the divisor 965 * @return the floor modulus {@code x - (floorDiv(x, y) * y)} 966 * @throws ArithmeticException if the divisor {@code y} is zero 967 * @see Math#floorMod(int, int) 968 * @see StrictMath#floorDiv(int, int) 969 * @since 1.8 970 */ 971 public static int floorMod(int x, int y) { 972 return Math.floorMod(x , y); 973 } 974 975 /** 976 * Returns the floor modulus of the {@code long} and {@int} arguments. 977 * <p> 978 * The floor modulus is {@code x - (floorDiv(x, y) * y)}, 979 * has the same sign as the divisor {@code y}, and 980 * is in the range of {@code -abs(y) < r < +abs(y)}. 981 * 982 * <p> 983 * The relationship between {@code floorDiv} and {@code floorMod} is such that: 984 * <ul> 985 * <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x} 986 * </ul> 987 * <p> 988 * See {@link Math#floorMod(int, int) Math.floorMod} for examples and 989 * a comparison to the {@code %} operator. 990 * 991 * @param x the dividend 992 * @param y the divisor 993 * @return the floor modulus {@code x - (floorDiv(x, y) * y)} 994 * @throws ArithmeticException if the divisor {@code y} is zero 995 * @see Math#floorMod(long, int) 996 * @see StrictMath#floorDiv(long, int) 997 * @since 9 998 */ 999 public static int floorMod(long x, int y) { 1000 return Math.floorMod(x , y); 1001 } 1002 1003 /** 1004 * Returns the floor modulus of the {@code long} arguments. 1005 * <p> 1006 * The floor modulus is {@code x - (floorDiv(x, y) * y)}, 1007 * has the same sign as the divisor {@code y}, and 1008 * is in the range of {@code -abs(y) < r < +abs(y)}. 1009 * <p> 1010 * The relationship between {@code floorDiv} and {@code floorMod} is such that: 1011 * <ul> 1012 * <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x} 1013 * </ul> 1014 * <p> 1015 * See {@link Math#floorMod(int, int) Math.floorMod} for examples and 1016 * a comparison to the {@code %} operator. 1017 * 1018 * @param x the dividend 1019 * @param y the divisor 1020 * @return the floor modulus {@code x - (floorDiv(x, y) * y)} 1021 * @throws ArithmeticException if the divisor {@code y} is zero 1022 * @see Math#floorMod(long, long) 1023 * @see StrictMath#floorDiv(long, long) 1024 * @since 1.8 1025 */ 1026 public static long floorMod(long x, long y) { 1027 return Math.floorMod(x, y); 1028 } 1029 1030 /** 1031 * Returns the absolute value of an {@code int} value. 1032 * If the argument is not negative, the argument is returned. 1033 * If the argument is negative, the negation of the argument is returned. 1034 * 1035 * <p>Note that if the argument is equal to the value of 1036 * {@link Integer#MIN_VALUE}, the most negative representable 1037 * {@code int} value, the result is that same value, which is 1038 * negative. 1039 * 1040 * @param a the argument whose absolute value is to be determined. 1041 * @return the absolute value of the argument. 1042 */ 1043 public static int abs(int a) { 1044 return Math.abs(a); 1045 } 1046 1047 /** 1048 * Returns the absolute value of a {@code long} value. 1049 * If the argument is not negative, the argument is returned. 1050 * If the argument is negative, the negation of the argument is returned. 1051 * 1052 * <p>Note that if the argument is equal to the value of 1053 * {@link Long#MIN_VALUE}, the most negative representable 1054 * {@code long} value, the result is that same value, which 1055 * is negative. 1056 * 1057 * @param a the argument whose absolute value is to be determined. 1058 * @return the absolute value of the argument. 1059 */ 1060 public static long abs(long a) { 1061 return Math.abs(a); 1062 } 1063 1064 /** 1065 * Returns the absolute value of a {@code float} value. 1066 * If the argument is not negative, the argument is returned. 1067 * If the argument is negative, the negation of the argument is returned. 1068 * Special cases: 1069 * <ul><li>If the argument is positive zero or negative zero, the 1070 * result is positive zero. 1071 * <li>If the argument is infinite, the result is positive infinity. 1072 * <li>If the argument is NaN, the result is NaN.</ul> 1073 * 1074 * @apiNote As implied by the above, one valid implementation of 1075 * this method is given by the expression below which computes a 1076 * {@code float} with the same exponent and significand as the 1077 * argument but with a guaranteed zero sign bit indicating a 1078 * positive value: <br> 1079 * {@code Float.intBitsToFloat(0x7fffffff & Float.floatToRawIntBits(a))} 1080 * 1081 * @param a the argument whose absolute value is to be determined 1082 * @return the absolute value of the argument. 1083 */ 1084 public static float abs(float a) { 1085 return Math.abs(a); 1086 } 1087 1088 /** 1089 * Returns the absolute value of a {@code double} value. 1090 * If the argument is not negative, the argument is returned. 1091 * If the argument is negative, the negation of the argument is returned. 1092 * Special cases: 1093 * <ul><li>If the argument is positive zero or negative zero, the result 1094 * is positive zero. 1095 * <li>If the argument is infinite, the result is positive infinity. 1096 * <li>If the argument is NaN, the result is NaN.</ul> 1097 * 1098 * @apiNote As implied by the above, one valid implementation of 1099 * this method is given by the expression below which computes a 1100 * {@code double} with the same exponent and significand as the 1101 * argument but with a guaranteed zero sign bit indicating a 1102 * positive value: <br> 1103 * {@code Double.longBitsToDouble((Double.doubleToRawLongBits(a)<<1)>>>1)} 1104 * 1105 * @param a the argument whose absolute value is to be determined 1106 * @return the absolute value of the argument. 1107 */ 1108 public static double abs(double a) { 1109 return Math.abs(a); 1110 } 1111 1112 /** 1113 * Returns the greater of two {@code int} values. That is, the 1114 * result is the argument closer to the value of 1115 * {@link Integer#MAX_VALUE}. If the arguments have the same value, 1116 * the result is that same value. 1117 * 1118 * @param a an argument. 1119 * @param b another argument. 1120 * @return the larger of {@code a} and {@code b}. 1121 */ 1122 @HotSpotIntrinsicCandidate 1123 public static int max(int a, int b) { 1124 return Math.max(a, b); 1125 } 1126 1127 /** 1128 * Returns the greater of two {@code long} values. That is, the 1129 * result is the argument closer to the value of 1130 * {@link Long#MAX_VALUE}. If the arguments have the same value, 1131 * the result is that same value. 1132 * 1133 * @param a an argument. 1134 * @param b another argument. 1135 * @return the larger of {@code a} and {@code b}. 1136 */ 1137 public static long max(long a, long b) { 1138 return Math.max(a, b); 1139 } 1140 1141 /** 1142 * Returns the greater of two {@code float} values. That is, 1143 * the result is the argument closer to positive infinity. If the 1144 * arguments have the same value, the result is that same 1145 * value. If either value is NaN, then the result is NaN. Unlike 1146 * the numerical comparison operators, this method considers 1147 * negative zero to be strictly smaller than positive zero. If one 1148 * argument is positive zero and the other negative zero, the 1149 * result is positive zero. 1150 * 1151 * @param a an argument. 1152 * @param b another argument. 1153 * @return the larger of {@code a} and {@code b}. 1154 */ 1155 public static float max(float a, float b) { 1156 return Math.max(a, b); 1157 } 1158 1159 /** 1160 * Returns the greater of two {@code double} values. That 1161 * is, the result is the argument closer to positive infinity. If 1162 * the arguments have the same value, the result is that same 1163 * value. If either value is NaN, then the result is NaN. Unlike 1164 * the numerical comparison operators, this method considers 1165 * negative zero to be strictly smaller than positive zero. If one 1166 * argument is positive zero and the other negative zero, the 1167 * result is positive zero. 1168 * 1169 * @param a an argument. 1170 * @param b another argument. 1171 * @return the larger of {@code a} and {@code b}. 1172 */ 1173 public static double max(double a, double b) { 1174 return Math.max(a, b); 1175 } 1176 1177 /** 1178 * Returns the smaller of two {@code int} values. That is, 1179 * the result the argument closer to the value of 1180 * {@link Integer#MIN_VALUE}. If the arguments have the same 1181 * value, the result is that same value. 1182 * 1183 * @param a an argument. 1184 * @param b another argument. 1185 * @return the smaller of {@code a} and {@code b}. 1186 */ 1187 @HotSpotIntrinsicCandidate 1188 public static int min(int a, int b) { 1189 return Math.min(a, b); 1190 } 1191 1192 /** 1193 * Returns the smaller of two {@code long} values. That is, 1194 * the result is the argument closer to the value of 1195 * {@link Long#MIN_VALUE}. If the arguments have the same 1196 * value, the result is that same value. 1197 * 1198 * @param a an argument. 1199 * @param b another argument. 1200 * @return the smaller of {@code a} and {@code b}. 1201 */ 1202 public static long min(long a, long b) { 1203 return Math.min(a, b); 1204 } 1205 1206 /** 1207 * Returns the smaller of two {@code float} values. That is, 1208 * the result is the value closer to negative infinity. If the 1209 * arguments have the same value, the result is that same 1210 * value. If either value is NaN, then the result is NaN. Unlike 1211 * the numerical comparison operators, this method considers 1212 * negative zero to be strictly smaller than positive zero. If 1213 * one argument is positive zero and the other is negative zero, 1214 * the result is negative zero. 1215 * 1216 * @param a an argument. 1217 * @param b another argument. 1218 * @return the smaller of {@code a} and {@code b.} 1219 */ 1220 public static float min(float a, float b) { 1221 return Math.min(a, b); 1222 } 1223 1224 /** 1225 * Returns the smaller of two {@code double} values. That 1226 * is, the result is the value closer to negative infinity. If the 1227 * arguments have the same value, the result is that same 1228 * value. If either value is NaN, then the result is NaN. Unlike 1229 * the numerical comparison operators, this method considers 1230 * negative zero to be strictly smaller than positive zero. If one 1231 * argument is positive zero and the other is negative zero, the 1232 * result is negative zero. 1233 * 1234 * @param a an argument. 1235 * @param b another argument. 1236 * @return the smaller of {@code a} and {@code b}. 1237 */ 1238 public static double min(double a, double b) { 1239 return Math.min(a, b); 1240 } 1241 1242 /** 1243 * Returns the fused multiply add of the three arguments; that is, 1244 * returns the exact product of the first two arguments summed 1245 * with the third argument and then rounded once to the nearest 1246 * {@code double}. 1247 * 1248 * The rounding is done using the {@linkplain 1249 * java.math.RoundingMode#HALF_EVEN round to nearest even 1250 * rounding mode}. 1251 * 1252 * In contrast, if {@code a * b + c} is evaluated as a regular 1253 * floating-point expression, two rounding errors are involved, 1254 * the first for the multiply operation, the second for the 1255 * addition operation. 1256 * 1257 * <p>Special cases: 1258 * <ul> 1259 * <li> If any argument is NaN, the result is NaN. 1260 * 1261 * <li> If one of the first two arguments is infinite and the 1262 * other is zero, the result is NaN. 1263 * 1264 * <li> If the exact product of the first two arguments is infinite 1265 * (in other words, at least one of the arguments is infinite and 1266 * the other is neither zero nor NaN) and the third argument is an 1267 * infinity of the opposite sign, the result is NaN. 1268 * 1269 * </ul> 1270 * 1271 * <p>Note that {@code fusedMac(a, 1.0, c)} returns the same 1272 * result as ({@code a + c}). However, 1273 * {@code fusedMac(a, b, +0.0)} does <em>not</em> always return the 1274 * same result as ({@code a * b}) since 1275 * {@code fusedMac(-0.0, +0.0, +0.0)} is {@code +0.0} while 1276 * ({@code -0.0 * +0.0}) is {@code -0.0}; {@code fusedMac(a, b, -0.0)} is 1277 * equivalent to ({@code a * b}) however. 1278 * 1279 * @apiNote This method corresponds to the fusedMultiplyAdd 1280 * operation defined in IEEE 754-2008. 1281 * 1282 * @param a a value 1283 * @param b a value 1284 * @param c a value 1285 * 1286 * @return (<i>a</i> × <i>b</i> + <i>c</i>) 1287 * computed, as if with unlimited range and precision, and rounded 1288 * once to the nearest {@code double} value 1289 * 1290 * @since 9 1291 */ 1292 public static double fma(double a, double b, double c) { 1293 return Math.fma(a, b, c); 1294 } 1295 1296 /** 1297 * Returns the fused multiply add of the three arguments; that is, 1298 * returns the exact product of the first two arguments summed 1299 * with the third argument and then rounded once to the nearest 1300 * {@code float}. 1301 * 1302 * The rounding is done using the {@linkplain 1303 * java.math.RoundingMode#HALF_EVEN round to nearest even 1304 * rounding mode}. 1305 * 1306 * In contrast, if {@code a * b + c} is evaluated as a regular 1307 * floating-point expression, two rounding errors are involved, 1308 * the first for the multiply operation, the second for the 1309 * addition operation. 1310 * 1311 * <p>Special cases: 1312 * <ul> 1313 * <li> If any argument is NaN, the result is NaN. 1314 * 1315 * <li> If one of the first two arguments is infinite and the 1316 * other is zero, the result is NaN. 1317 * 1318 * <li> If the exact product of the first two arguments is infinite 1319 * (in other words, at least one of the arguments is infinite and 1320 * the other is neither zero nor NaN) and the third argument is an 1321 * infinity of the opposite sign, the result is NaN. 1322 * 1323 * </ul> 1324 * 1325 * <p>Note that {@code fma(a, 1.0f, c)} returns the same 1326 * result as ({@code a + c}). However, 1327 * {@code fma(a, b, +0.0f)} does <em>not</em> always return the 1328 * same result as ({@code a * b}) since 1329 * {@code fma(-0.0f, +0.0f, +0.0f)} is {@code +0.0f} while 1330 * ({@code -0.0f * +0.0f}) is {@code -0.0f}; {@code fma(a, b, -0.0f)} is 1331 * equivalent to ({@code a * b}) however. 1332 * 1333 * @apiNote This method corresponds to the fusedMultiplyAdd 1334 * operation defined in IEEE 754-2008. 1335 * 1336 * @param a a value 1337 * @param b a value 1338 * @param c a value 1339 * 1340 * @return (<i>a</i> × <i>b</i> + <i>c</i>) 1341 * computed, as if with unlimited range and precision, and rounded 1342 * once to the nearest {@code float} value 1343 * 1344 * @since 9 1345 */ 1346 public static float fma(float a, float b, float c) { 1347 return Math.fma(a, b, c); 1348 } 1349 1350 /** 1351 * Returns the size of an ulp of the argument. An ulp, unit in 1352 * the last place, of a {@code double} value is the positive 1353 * distance between this floating-point value and the {@code 1354 * double} value next larger in magnitude. Note that for non-NaN 1355 * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 1356 * 1357 * <p>Special Cases: 1358 * <ul> 1359 * <li> If the argument is NaN, then the result is NaN. 1360 * <li> If the argument is positive or negative infinity, then the 1361 * result is positive infinity. 1362 * <li> If the argument is positive or negative zero, then the result is 1363 * {@code Double.MIN_VALUE}. 1364 * <li> If the argument is ±{@code Double.MAX_VALUE}, then 1365 * the result is equal to 2<sup>971</sup>. 1366 * </ul> 1367 * 1368 * @param d the floating-point value whose ulp is to be returned 1369 * @return the size of an ulp of the argument 1370 * @author Joseph D. Darcy 1371 * @since 1.5 1372 */ 1373 public static double ulp(double d) { 1374 return Math.ulp(d); 1375 } 1376 1377 /** 1378 * Returns the size of an ulp of the argument. An ulp, unit in 1379 * the last place, of a {@code float} value is the positive 1380 * distance between this floating-point value and the {@code 1381 * float} value next larger in magnitude. Note that for non-NaN 1382 * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 1383 * 1384 * <p>Special Cases: 1385 * <ul> 1386 * <li> If the argument is NaN, then the result is NaN. 1387 * <li> If the argument is positive or negative infinity, then the 1388 * result is positive infinity. 1389 * <li> If the argument is positive or negative zero, then the result is 1390 * {@code Float.MIN_VALUE}. 1391 * <li> If the argument is ±{@code Float.MAX_VALUE}, then 1392 * the result is equal to 2<sup>104</sup>. 1393 * </ul> 1394 * 1395 * @param f the floating-point value whose ulp is to be returned 1396 * @return the size of an ulp of the argument 1397 * @author Joseph D. Darcy 1398 * @since 1.5 1399 */ 1400 public static float ulp(float f) { 1401 return Math.ulp(f); 1402 } 1403 1404 /** 1405 * Returns the signum function of the argument; zero if the argument 1406 * is zero, 1.0 if the argument is greater than zero, -1.0 if the 1407 * argument is less than zero. 1408 * 1409 * <p>Special Cases: 1410 * <ul> 1411 * <li> If the argument is NaN, then the result is NaN. 1412 * <li> If the argument is positive zero or negative zero, then the 1413 * result is the same as the argument. 1414 * </ul> 1415 * 1416 * @param d the floating-point value whose signum is to be returned 1417 * @return the signum function of the argument 1418 * @author Joseph D. Darcy 1419 * @since 1.5 1420 */ 1421 public static double signum(double d) { 1422 return Math.signum(d); 1423 } 1424 1425 /** 1426 * Returns the signum function of the argument; zero if the argument 1427 * is zero, 1.0f if the argument is greater than zero, -1.0f if the 1428 * argument is less than zero. 1429 * 1430 * <p>Special Cases: 1431 * <ul> 1432 * <li> If the argument is NaN, then the result is NaN. 1433 * <li> If the argument is positive zero or negative zero, then the 1434 * result is the same as the argument. 1435 * </ul> 1436 * 1437 * @param f the floating-point value whose signum is to be returned 1438 * @return the signum function of the argument 1439 * @author Joseph D. Darcy 1440 * @since 1.5 1441 */ 1442 public static float signum(float f) { 1443 return Math.signum(f); 1444 } 1445 1446 /** 1447 * Returns the hyperbolic sine of a {@code double} value. 1448 * The hyperbolic sine of <i>x</i> is defined to be 1449 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2 1450 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1451 * 1452 * <p>Special cases: 1453 * <ul> 1454 * 1455 * <li>If the argument is NaN, then the result is NaN. 1456 * 1457 * <li>If the argument is infinite, then the result is an infinity 1458 * with the same sign as the argument. 1459 * 1460 * <li>If the argument is zero, then the result is a zero with the 1461 * same sign as the argument. 1462 * 1463 * </ul> 1464 * 1465 * @param x The number whose hyperbolic sine is to be returned. 1466 * @return The hyperbolic sine of {@code x}. 1467 * @since 1.5 1468 */ 1469 public static native double sinh(double x); 1470 1471 /** 1472 * Returns the hyperbolic cosine of a {@code double} value. 1473 * The hyperbolic cosine of <i>x</i> is defined to be 1474 * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2 1475 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1476 * 1477 * <p>Special cases: 1478 * <ul> 1479 * 1480 * <li>If the argument is NaN, then the result is NaN. 1481 * 1482 * <li>If the argument is infinite, then the result is positive 1483 * infinity. 1484 * 1485 * <li>If the argument is zero, then the result is {@code 1.0}. 1486 * 1487 * </ul> 1488 * 1489 * @param x The number whose hyperbolic cosine is to be returned. 1490 * @return The hyperbolic cosine of {@code x}. 1491 * @since 1.5 1492 */ 1493 public static native double cosh(double x); 1494 1495 /** 1496 * Returns the hyperbolic tangent of a {@code double} value. 1497 * The hyperbolic tangent of <i>x</i> is defined to be 1498 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>), 1499 * in other words, {@linkplain Math#sinh 1500 * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note 1501 * that the absolute value of the exact tanh is always less than 1502 * 1. 1503 * 1504 * <p>Special cases: 1505 * <ul> 1506 * 1507 * <li>If the argument is NaN, then the result is NaN. 1508 * 1509 * <li>If the argument is zero, then the result is a zero with the 1510 * same sign as the argument. 1511 * 1512 * <li>If the argument is positive infinity, then the result is 1513 * {@code +1.0}. 1514 * 1515 * <li>If the argument is negative infinity, then the result is 1516 * {@code -1.0}. 1517 * 1518 * </ul> 1519 * 1520 * @param x The number whose hyperbolic tangent is to be returned. 1521 * @return The hyperbolic tangent of {@code x}. 1522 * @since 1.5 1523 */ 1524 public static native double tanh(double x); 1525 1526 /** 1527 * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1528 * without intermediate overflow or underflow. 1529 * 1530 * <p>Special cases: 1531 * <ul> 1532 * 1533 * <li> If either argument is infinite, then the result 1534 * is positive infinity. 1535 * 1536 * <li> If either argument is NaN and neither argument is infinite, 1537 * then the result is NaN. 1538 * 1539 * </ul> 1540 * 1541 * @param x a value 1542 * @param y a value 1543 * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1544 * without intermediate overflow or underflow 1545 * @since 1.5 1546 */ 1547 public static double hypot(double x, double y) { 1548 return FdLibm.Hypot.compute(x, y); 1549 } 1550 1551 /** 1552 * Returns <i>e</i><sup>x</sup> -1. Note that for values of 1553 * <i>x</i> near 0, the exact sum of 1554 * {@code expm1(x)} + 1 is much closer to the true 1555 * result of <i>e</i><sup>x</sup> than {@code exp(x)}. 1556 * 1557 * <p>Special cases: 1558 * <ul> 1559 * <li>If the argument is NaN, the result is NaN. 1560 * 1561 * <li>If the argument is positive infinity, then the result is 1562 * positive infinity. 1563 * 1564 * <li>If the argument is negative infinity, then the result is 1565 * -1.0. 1566 * 1567 * <li>If the argument is zero, then the result is a zero with the 1568 * same sign as the argument. 1569 * 1570 * </ul> 1571 * 1572 * @param x the exponent to raise <i>e</i> to in the computation of 1573 * <i>e</i><sup>{@code x}</sup> -1. 1574 * @return the value <i>e</i><sup>{@code x}</sup> - 1. 1575 * @since 1.5 1576 */ 1577 public static native double expm1(double x); 1578 1579 /** 1580 * Returns the natural logarithm of the sum of the argument and 1. 1581 * Note that for small values {@code x}, the result of 1582 * {@code log1p(x)} is much closer to the true result of ln(1 1583 * + {@code x}) than the floating-point evaluation of 1584 * {@code log(1.0+x)}. 1585 * 1586 * <p>Special cases: 1587 * <ul> 1588 * 1589 * <li>If the argument is NaN or less than -1, then the result is 1590 * NaN. 1591 * 1592 * <li>If the argument is positive infinity, then the result is 1593 * positive infinity. 1594 * 1595 * <li>If the argument is negative one, then the result is 1596 * negative infinity. 1597 * 1598 * <li>If the argument is zero, then the result is a zero with the 1599 * same sign as the argument. 1600 * 1601 * </ul> 1602 * 1603 * @param x a value 1604 * @return the value ln({@code x} + 1), the natural 1605 * log of {@code x} + 1 1606 * @since 1.5 1607 */ 1608 public static native double log1p(double x); 1609 1610 /** 1611 * Returns the first floating-point argument with the sign of the 1612 * second floating-point argument. For this method, a NaN 1613 * {@code sign} argument is always treated as if it were 1614 * positive. 1615 * 1616 * @param magnitude the parameter providing the magnitude of the result 1617 * @param sign the parameter providing the sign of the result 1618 * @return a value with the magnitude of {@code magnitude} 1619 * and the sign of {@code sign}. 1620 * @since 1.6 1621 */ 1622 public static double copySign(double magnitude, double sign) { 1623 return Math.copySign(magnitude, (Double.isNaN(sign)?1.0d:sign)); 1624 } 1625 1626 /** 1627 * Returns the first floating-point argument with the sign of the 1628 * second floating-point argument. For this method, a NaN 1629 * {@code sign} argument is always treated as if it were 1630 * positive. 1631 * 1632 * @param magnitude the parameter providing the magnitude of the result 1633 * @param sign the parameter providing the sign of the result 1634 * @return a value with the magnitude of {@code magnitude} 1635 * and the sign of {@code sign}. 1636 * @since 1.6 1637 */ 1638 public static float copySign(float magnitude, float sign) { 1639 return Math.copySign(magnitude, (Float.isNaN(sign)?1.0f:sign)); 1640 } 1641 /** 1642 * Returns the unbiased exponent used in the representation of a 1643 * {@code float}. Special cases: 1644 * 1645 * <ul> 1646 * <li>If the argument is NaN or infinite, then the result is 1647 * {@link Float#MAX_EXPONENT} + 1. 1648 * <li>If the argument is zero or subnormal, then the result is 1649 * {@link Float#MIN_EXPONENT} -1. 1650 * </ul> 1651 * @param f a {@code float} value 1652 * @return the unbiased exponent of the argument 1653 * @since 1.6 1654 */ 1655 public static int getExponent(float f) { 1656 return Math.getExponent(f); 1657 } 1658 1659 /** 1660 * Returns the unbiased exponent used in the representation of a 1661 * {@code double}. Special cases: 1662 * 1663 * <ul> 1664 * <li>If the argument is NaN or infinite, then the result is 1665 * {@link Double#MAX_EXPONENT} + 1. 1666 * <li>If the argument is zero or subnormal, then the result is 1667 * {@link Double#MIN_EXPONENT} -1. 1668 * </ul> 1669 * @param d a {@code double} value 1670 * @return the unbiased exponent of the argument 1671 * @since 1.6 1672 */ 1673 public static int getExponent(double d) { 1674 return Math.getExponent(d); 1675 } 1676 1677 /** 1678 * Returns the floating-point number adjacent to the first 1679 * argument in the direction of the second argument. If both 1680 * arguments compare as equal the second argument is returned. 1681 * 1682 * <p>Special cases: 1683 * <ul> 1684 * <li> If either argument is a NaN, then NaN is returned. 1685 * 1686 * <li> If both arguments are signed zeros, {@code direction} 1687 * is returned unchanged (as implied by the requirement of 1688 * returning the second argument if the arguments compare as 1689 * equal). 1690 * 1691 * <li> If {@code start} is 1692 * ±{@link Double#MIN_VALUE} and {@code direction} 1693 * has a value such that the result should have a smaller 1694 * magnitude, then a zero with the same sign as {@code start} 1695 * is returned. 1696 * 1697 * <li> If {@code start} is infinite and 1698 * {@code direction} has a value such that the result should 1699 * have a smaller magnitude, {@link Double#MAX_VALUE} with the 1700 * same sign as {@code start} is returned. 1701 * 1702 * <li> If {@code start} is equal to ± 1703 * {@link Double#MAX_VALUE} and {@code direction} has a 1704 * value such that the result should have a larger magnitude, an 1705 * infinity with same sign as {@code start} is returned. 1706 * </ul> 1707 * 1708 * @param start starting floating-point value 1709 * @param direction value indicating which of 1710 * {@code start}'s neighbors or {@code start} should 1711 * be returned 1712 * @return The floating-point number adjacent to {@code start} in the 1713 * direction of {@code direction}. 1714 * @since 1.6 1715 */ 1716 public static double nextAfter(double start, double direction) { 1717 return Math.nextAfter(start, direction); 1718 } 1719 1720 /** 1721 * Returns the floating-point number adjacent to the first 1722 * argument in the direction of the second argument. If both 1723 * arguments compare as equal a value equivalent to the second argument 1724 * is returned. 1725 * 1726 * <p>Special cases: 1727 * <ul> 1728 * <li> If either argument is a NaN, then NaN is returned. 1729 * 1730 * <li> If both arguments are signed zeros, a value equivalent 1731 * to {@code direction} is returned. 1732 * 1733 * <li> If {@code start} is 1734 * ±{@link Float#MIN_VALUE} and {@code direction} 1735 * has a value such that the result should have a smaller 1736 * magnitude, then a zero with the same sign as {@code start} 1737 * is returned. 1738 * 1739 * <li> If {@code start} is infinite and 1740 * {@code direction} has a value such that the result should 1741 * have a smaller magnitude, {@link Float#MAX_VALUE} with the 1742 * same sign as {@code start} is returned. 1743 * 1744 * <li> If {@code start} is equal to ± 1745 * {@link Float#MAX_VALUE} and {@code direction} has a 1746 * value such that the result should have a larger magnitude, an 1747 * infinity with same sign as {@code start} is returned. 1748 * </ul> 1749 * 1750 * @param start starting floating-point value 1751 * @param direction value indicating which of 1752 * {@code start}'s neighbors or {@code start} should 1753 * be returned 1754 * @return The floating-point number adjacent to {@code start} in the 1755 * direction of {@code direction}. 1756 * @since 1.6 1757 */ 1758 public static float nextAfter(float start, double direction) { 1759 return Math.nextAfter(start, direction); 1760 } 1761 1762 /** 1763 * Returns the floating-point value adjacent to {@code d} in 1764 * the direction of positive infinity. This method is 1765 * semantically equivalent to {@code nextAfter(d, 1766 * Double.POSITIVE_INFINITY)}; however, a {@code nextUp} 1767 * implementation may run faster than its equivalent 1768 * {@code nextAfter} call. 1769 * 1770 * <p>Special Cases: 1771 * <ul> 1772 * <li> If the argument is NaN, the result is NaN. 1773 * 1774 * <li> If the argument is positive infinity, the result is 1775 * positive infinity. 1776 * 1777 * <li> If the argument is zero, the result is 1778 * {@link Double#MIN_VALUE} 1779 * 1780 * </ul> 1781 * 1782 * @param d starting floating-point value 1783 * @return The adjacent floating-point value closer to positive 1784 * infinity. 1785 * @since 1.6 1786 */ 1787 public static double nextUp(double d) { 1788 return Math.nextUp(d); 1789 } 1790 1791 /** 1792 * Returns the floating-point value adjacent to {@code f} in 1793 * the direction of positive infinity. This method is 1794 * semantically equivalent to {@code nextAfter(f, 1795 * Float.POSITIVE_INFINITY)}; however, a {@code nextUp} 1796 * implementation may run faster than its equivalent 1797 * {@code nextAfter} call. 1798 * 1799 * <p>Special Cases: 1800 * <ul> 1801 * <li> If the argument is NaN, the result is NaN. 1802 * 1803 * <li> If the argument is positive infinity, the result is 1804 * positive infinity. 1805 * 1806 * <li> If the argument is zero, the result is 1807 * {@link Float#MIN_VALUE} 1808 * 1809 * </ul> 1810 * 1811 * @param f starting floating-point value 1812 * @return The adjacent floating-point value closer to positive 1813 * infinity. 1814 * @since 1.6 1815 */ 1816 public static float nextUp(float f) { 1817 return Math.nextUp(f); 1818 } 1819 1820 /** 1821 * Returns the floating-point value adjacent to {@code d} in 1822 * the direction of negative infinity. This method is 1823 * semantically equivalent to {@code nextAfter(d, 1824 * Double.NEGATIVE_INFINITY)}; however, a 1825 * {@code nextDown} implementation may run faster than its 1826 * equivalent {@code nextAfter} call. 1827 * 1828 * <p>Special Cases: 1829 * <ul> 1830 * <li> If the argument is NaN, the result is NaN. 1831 * 1832 * <li> If the argument is negative infinity, the result is 1833 * negative infinity. 1834 * 1835 * <li> If the argument is zero, the result is 1836 * {@code -Double.MIN_VALUE} 1837 * 1838 * </ul> 1839 * 1840 * @param d starting floating-point value 1841 * @return The adjacent floating-point value closer to negative 1842 * infinity. 1843 * @since 1.8 1844 */ 1845 public static double nextDown(double d) { 1846 return Math.nextDown(d); 1847 } 1848 1849 /** 1850 * Returns the floating-point value adjacent to {@code f} in 1851 * the direction of negative infinity. This method is 1852 * semantically equivalent to {@code nextAfter(f, 1853 * Float.NEGATIVE_INFINITY)}; however, a 1854 * {@code nextDown} implementation may run faster than its 1855 * equivalent {@code nextAfter} call. 1856 * 1857 * <p>Special Cases: 1858 * <ul> 1859 * <li> If the argument is NaN, the result is NaN. 1860 * 1861 * <li> If the argument is negative infinity, the result is 1862 * negative infinity. 1863 * 1864 * <li> If the argument is zero, the result is 1865 * {@code -Float.MIN_VALUE} 1866 * 1867 * </ul> 1868 * 1869 * @param f starting floating-point value 1870 * @return The adjacent floating-point value closer to negative 1871 * infinity. 1872 * @since 1.8 1873 */ 1874 public static float nextDown(float f) { 1875 return Math.nextDown(f); 1876 } 1877 1878 /** 1879 * Returns {@code d} × 1880 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1881 * by a single correctly rounded floating-point multiply to a 1882 * member of the double value set. See the Java 1883 * Language Specification for a discussion of floating-point 1884 * value sets. If the exponent of the result is between {@link 1885 * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the 1886 * answer is calculated exactly. If the exponent of the result 1887 * would be larger than {@code Double.MAX_EXPONENT}, an 1888 * infinity is returned. Note that if the result is subnormal, 1889 * precision may be lost; that is, when {@code scalb(x, n)} 1890 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1891 * <i>x</i>. When the result is non-NaN, the result has the same 1892 * sign as {@code d}. 1893 * 1894 * <p>Special cases: 1895 * <ul> 1896 * <li> If the first argument is NaN, NaN is returned. 1897 * <li> If the first argument is infinite, then an infinity of the 1898 * same sign is returned. 1899 * <li> If the first argument is zero, then a zero of the same 1900 * sign is returned. 1901 * </ul> 1902 * 1903 * @param d number to be scaled by a power of two. 1904 * @param scaleFactor power of 2 used to scale {@code d} 1905 * @return {@code d} × 2<sup>{@code scaleFactor}</sup> 1906 * @since 1.6 1907 */ 1908 public static double scalb(double d, int scaleFactor) { 1909 return Math.scalb(d, scaleFactor); 1910 } 1911 1912 /** 1913 * Returns {@code f} × 1914 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1915 * by a single correctly rounded floating-point multiply to a 1916 * member of the float value set. See the Java 1917 * Language Specification for a discussion of floating-point 1918 * value sets. If the exponent of the result is between {@link 1919 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the 1920 * answer is calculated exactly. If the exponent of the result 1921 * would be larger than {@code Float.MAX_EXPONENT}, an 1922 * infinity is returned. Note that if the result is subnormal, 1923 * precision may be lost; that is, when {@code scalb(x, n)} 1924 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1925 * <i>x</i>. When the result is non-NaN, the result has the same 1926 * sign as {@code f}. 1927 * 1928 * <p>Special cases: 1929 * <ul> 1930 * <li> If the first argument is NaN, NaN is returned. 1931 * <li> If the first argument is infinite, then an infinity of the 1932 * same sign is returned. 1933 * <li> If the first argument is zero, then a zero of the same 1934 * sign is returned. 1935 * </ul> 1936 * 1937 * @param f number to be scaled by a power of two. 1938 * @param scaleFactor power of 2 used to scale {@code f} 1939 * @return {@code f} × 2<sup>{@code scaleFactor}</sup> 1940 * @since 1.6 1941 */ 1942 public static float scalb(float f, int scaleFactor) { 1943 return Math.scalb(f, scaleFactor); 1944 } 1945 }