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