1 /* 2 * Copyright (c) 1999, 2023, 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.vm.annotation.IntrinsicCandidate; 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="https://www.netlib.org/fdlibm/">{@code fdlibm}</a>. These 44 * algorithms, which are written in the C programming language, are 45 * then to be understood to be transliterated into Java and executed 46 * with all floating-point and integer operations following the rules 47 * of Java arithmetic. The following transformations are used in the 48 * transliteration: 49 * 50 * <ul> 51 * <li>Extraction and setting of the high and low halves of a 64-bit 52 * {@code double} in C is expressed using Java platform methods that 53 * perform bit-wise conversions {@linkplain 54 * Double#doubleToRawLongBits(double) from {@code double} to {@code 55 * long}} and {@linkplain Double#longBitsToDouble(long) {@code long} 56 * to {@code double}}. 57 * 58 * <li>Unsigned {@code int} values in C are mapped to signed {@code 59 * int} values in Java with updates to operations to replicate 60 * unsigned semantics where the results on the same textual operation 61 * would differ. For example, {@code >>} shifts on unsigned C values 62 * are replaced with {@code >>>} shifts on signed Java values. Sized 63 * comparisons on unsigned C values ({@code <}, {@code <=}, {@code >}, 64 * {@code >=}) are replaced with semantically equivalent calls to 65 * {@link Integer#compareUnsigned(int, int) compareUnsigned}. 66 * </ul> 67 * 68 * <p>The Java math library is defined with respect to 69 * {@code fdlibm} version 5.3. Where {@code fdlibm} provides 70 * more than one definition for a function (such as 71 * {@code acos}), use the "IEEE 754 core function" version 72 * (residing in a file whose name begins with the letter 73 * {@code e}). The methods which require {@code fdlibm} 74 * semantics are {@code sin}, {@code cos}, {@code tan}, 75 * {@code asin}, {@code acos}, {@code atan}, 76 * {@code exp}, {@code log}, {@code log10}, 77 * {@code cbrt}, {@code atan2}, {@code pow}, 78 * {@code sinh}, {@code cosh}, {@code tanh}, 79 * {@code hypot}, {@code expm1}, and {@code log1p}. 80 * 81 * <p> 82 * The platform uses signed two's complement integer arithmetic with 83 * int and long primitive types. The developer should choose 84 * the primitive type to ensure that arithmetic operations consistently 85 * produce correct results, which in some cases means the operations 86 * will not overflow the range of values of the computation. 87 * The best practice is to choose the primitive type and algorithm to avoid 88 * overflow. In cases where the size is {@code int} or {@code long} and 89 * overflow errors need to be detected, the methods whose names end with 90 * {@code Exact} throw an {@code ArithmeticException} when the results overflow. 91 * 92 * <h2><a id=Ieee754RecommendedOps>IEEE 754 Recommended 93 * Operations</a></h2> 94 * 95 * The {@link java.lang.Math Math} class discusses how the shared 96 * quality of implementation criteria for selected {@code Math} and 97 * {@code StrictMath} methods <a 98 * href="Math.html#Ieee754RecommendedOps">relate to the IEEE 754 99 * recommended operations</a>. 100 * 101 * @see <a href="https://standards.ieee.org/ieee/754/6210/"> 102 * <cite>IEEE Standard for Floating-Point Arithmetic</cite></a> 103 * 104 * @author Joseph D. Darcy 105 * @since 1.3 106 */ 107 public final class StrictMath { 108 109 /** 110 * Don't let anyone instantiate this class. 111 */ StrictMath()112 private StrictMath() {} 113 114 /** 115 * The {@code double} value that is closer than any other to 116 * <i>e</i>, the base of the natural logarithms. 117 */ 118 public static final double E = 2.718281828459045; 119 120 /** 121 * The {@code double} value that is closer than any other to 122 * <i>pi</i> (π), the ratio of the circumference of a circle to its 123 * diameter. 124 */ 125 public static final double PI = 3.141592653589793; 126 127 /** 128 * The {@code double} value that is closer than any other to 129 * <i>tau</i> (τ), the ratio of the circumference of a circle 130 * to its radius. 131 * 132 * @apiNote 133 * The value of <i>pi</i> is one half that of <i>tau</i>; in other 134 * words, <i>tau</i> is double <i>pi</i> . 135 * 136 * @since 19 137 */ 138 public static final double TAU = 2.0 * PI; 139 140 /** 141 * Constant by which to multiply an angular value in degrees to obtain an 142 * angular value in radians. 143 */ 144 private static final double DEGREES_TO_RADIANS = 0.017453292519943295; 145 146 /** 147 * Constant by which to multiply an angular value in radians to obtain an 148 * angular value in degrees. 149 */ 150 151 private static final double RADIANS_TO_DEGREES = 57.29577951308232; 152 153 /** 154 * Returns the trigonometric sine of an angle. Special cases: 155 * <ul><li>If the argument is NaN or an infinity, then the 156 * result is NaN. 157 * <li>If the argument is zero, then the result is a zero with the 158 * same sign as the argument.</ul> 159 * 160 * @param a an angle, in radians. 161 * @return the sine of the argument. 162 */ 163 // Android-changed: Reimplement in native 164 // public static double sin(double a) { 165 // return FdLibm.Sin.compute(a); 166 // } sin(double a)167 public static native double sin(double a); 168 169 /** 170 * Returns the trigonometric cosine of an angle. Special cases: 171 * <ul><li>If the argument is NaN or an infinity, then the 172 * result is NaN. 173 * <li>If the argument is zero, then the result is {@code 1.0}. 174 * </ul> 175 * 176 * @param a an angle, in radians. 177 * @return the cosine of the argument. 178 */ 179 // Android-changed: Reimplement in native 180 // public static double cos(double a) { 181 // return FdLibm.Cos.compute(a); 182 // } cos(double a)183 public static native double cos(double a); 184 185 /** 186 * Returns the trigonometric tangent of an angle. Special cases: 187 * <ul><li>If the argument is NaN or an infinity, then the result 188 * is NaN. 189 * <li>If the argument is zero, then the result is a zero with the 190 * same sign as the argument.</ul> 191 * 192 * @param a an angle, in radians. 193 * @return the tangent of the argument. 194 */ 195 // Android-changed: Reimplement in native 196 // public static double tan(double a) { 197 // return FdLibm.Tan.compute(a); 198 // } tan(double a)199 public static native double tan(double a); 200 201 /** 202 * Returns the arc sine of a value; the returned angle is in the 203 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases: 204 * <ul><li>If the argument is NaN or its absolute value is greater 205 * than 1, then the result is NaN. 206 * <li>If the argument is zero, then the result is a zero with the 207 * same sign as the argument.</ul> 208 * 209 * @param a the value whose arc sine is to be returned. 210 * @return the arc sine of the argument. 211 */ 212 // Android-changed: Reimplement in native 213 // public static double asin(double a) { 214 // return FdLibm.Asin.compute(a); 215 // } asin(double a)216 public static native double asin(double a); 217 218 /** 219 * Returns the arc cosine of a value; the returned angle is in the 220 * range 0.0 through <i>pi</i>. Special case: 221 * <ul><li>If the argument is NaN or its absolute value is greater 222 * than 1, then the result is NaN. 223 * <li>If the argument is {@code 1.0}, the result is positive zero. 224 * </ul> 225 * 226 * @param a the value whose arc cosine is to be returned. 227 * @return the arc cosine of the argument. 228 */ 229 // Android-changed: Reimplement in native 230 // public static double acos(double a) { 231 // return FdLibm.Acos.compute(a); 232 // } acos(double a)233 public static native double acos(double a); 234 235 /** 236 * Returns the arc tangent of a value; the returned angle is in the 237 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases: 238 * <ul><li>If the argument is NaN, then the result is NaN. 239 * <li>If the argument is zero, then the result is a zero with the 240 * same sign as the argument. 241 * <li>If the argument is {@linkplain Double#isInfinite infinite}, 242 * then the result is the closest value to <i>pi</i>/2 with the 243 * same sign as the input. 244 * </ul> 245 * 246 * @param a the value whose arc tangent is to be returned. 247 * @return the arc tangent of the argument. 248 */ 249 // Android-changed: Reimplement in native 250 // public static double atan(double a) { 251 // return FdLibm.Atan.compute(a); 252 // } atan(double a)253 public static native double atan(double a); 254 255 /** 256 * Converts an angle measured in degrees to an approximately 257 * equivalent angle measured in radians. The conversion from 258 * degrees to radians is generally inexact. 259 * 260 * @param angdeg an angle, in degrees 261 * @return the measurement of the angle {@code angdeg} 262 * in radians. 263 */ toRadians(double angdeg)264 public static strictfp double toRadians(double angdeg) { 265 // Do not delegate to Math.toRadians(angdeg) because 266 // this method has the strictfp modifier. 267 return angdeg * DEGREES_TO_RADIANS; 268 } 269 270 /** 271 * Converts an angle measured in radians to an approximately 272 * equivalent angle measured in degrees. The conversion from 273 * radians to degrees is generally inexact; users should 274 * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly 275 * equal {@code 0.0}. 276 * 277 * @param angrad an angle, in radians 278 * @return the measurement of the angle {@code angrad} 279 * in degrees. 280 */ toDegrees(double angrad)281 public static strictfp double toDegrees(double angrad) { 282 // Do not delegate to Math.toDegrees(angrad) because 283 // this method has the strictfp modifier. 284 return angrad * RADIANS_TO_DEGREES; 285 } 286 287 /** 288 * Returns Euler's number <i>e</i> raised to the power of a 289 * {@code double} value. Special cases: 290 * <ul><li>If the argument is NaN, the result is NaN. 291 * <li>If the argument is positive infinity, then the result is 292 * positive infinity. 293 * <li>If the argument is negative infinity, then the result is 294 * positive zero. 295 * <li>If the argument is zero, then the result is {@code 1.0}. 296 * </ul> 297 * 298 * @param a the exponent to raise <i>e</i> to. 299 * @return the value <i>e</i><sup>{@code a}</sup>, 300 * where <i>e</i> is the base of the natural logarithms. 301 */ 302 // BEGIN Android-changed: Reimplement in native 303 /* 304 public static double exp(double a) { 305 return FdLibm.Exp.compute(a); 306 } 307 */ 308 // END Android-changed: Reimplement in native exp(double a)309 public static native double exp(double a); 310 311 /** 312 * Returns the natural logarithm (base <i>e</i>) of a {@code double} 313 * value. Special cases: 314 * <ul><li>If the argument is NaN or less than zero, then the result 315 * is NaN. 316 * <li>If the argument is positive infinity, then the result is 317 * positive infinity. 318 * <li>If the argument is positive zero or negative zero, then the 319 * result is negative infinity. 320 * <li>If the argument is {@code 1.0}, then the result is positive 321 * zero. 322 * </ul> 323 * 324 * @param a a value 325 * @return the value ln {@code a}, the natural logarithm of 326 * {@code a}. 327 */ 328 // Android-changed: Reimplement in native 329 // public static double log(double a) { 330 // return FdLibm.Log.compute(a); 331 // } log(double a)332 public static native double log(double a); 333 334 /** 335 * Returns the base 10 logarithm of a {@code double} value. 336 * Special cases: 337 * 338 * <ul><li>If the argument is NaN or less than zero, then the result 339 * is NaN. 340 * <li>If the argument is positive infinity, then the result is 341 * positive infinity. 342 * <li>If the argument is positive zero or negative zero, then the 343 * result is negative infinity. 344 * <li>If the argument is equal to 10<sup><i>n</i></sup> for 345 * integer <i>n</i>, then the result is <i>n</i>. In particular, 346 * if the argument is {@code 1.0} (10<sup>0</sup>), then the 347 * result is positive zero. 348 * </ul> 349 * 350 * @param a a value 351 * @return the base 10 logarithm of {@code a}. 352 * @since 1.5 353 */ 354 // Android-changed: Reimplement in native 355 // public static double log10(double a) { 356 // return FdLibm.Log10.compute(a); 357 // } log10(double a)358 public static native double log10(double a); 359 360 /** 361 * Returns the correctly rounded positive square root of a 362 * {@code double} value. 363 * Special cases: 364 * <ul><li>If the argument is NaN or less than zero, then the result 365 * is NaN. 366 * <li>If the argument is positive infinity, then the result is positive 367 * infinity. 368 * <li>If the argument is positive zero or negative zero, then the 369 * result is the same as the argument.</ul> 370 * Otherwise, the result is the {@code double} value closest to 371 * the true mathematical square root of the argument value. 372 * 373 * @param a a value. 374 * @return the positive square root of {@code a}. 375 */ 376 @IntrinsicCandidate 377 // Android-changed: Reimplement in native 378 // public static double sqrt(double a) { 379 // return FdLibm.Sqrt.compute(a); 380 // } sqrt(double a)381 public static native double sqrt(double a); 382 383 /** 384 * Returns the cube root of a {@code double} value. For 385 * positive finite {@code x}, {@code cbrt(-x) == 386 * -cbrt(x)}; that is, the cube root of a negative value is 387 * the negative of the cube root of that value's magnitude. 388 * Special cases: 389 * 390 * <ul> 391 * 392 * <li>If the argument is NaN, then the result is NaN. 393 * 394 * <li>If the argument is infinite, then the result is an infinity 395 * with the same sign as the argument. 396 * 397 * <li>If the argument is zero, then the result is a zero with the 398 * same sign as the argument. 399 * 400 * </ul> 401 * 402 * @param a a value. 403 * @return the cube root of {@code a}. 404 * @since 1.5 405 */ 406 // BEGIN Android-changed: Reimplement in native 407 /* 408 public static double cbrt(double a) { 409 return FdLibm.Cbrt.compute(a); 410 } 411 */ 412 // END Android-changed: Reimplement in native cbrt(double a)413 public static native double cbrt(double a); 414 415 /** 416 * Computes the remainder operation on two arguments as prescribed 417 * by the IEEE 754 standard. 418 * The remainder value is mathematically equal to 419 * <code>f1 - f2</code> × <i>n</i>, 420 * where <i>n</i> is the mathematical integer closest to the exact 421 * mathematical value of the quotient {@code f1/f2}, and if two 422 * mathematical integers are equally close to {@code f1/f2}, 423 * then <i>n</i> is the integer that is even. If the remainder is 424 * zero, its sign is the same as the sign of the first argument. 425 * Special cases: 426 * <ul><li>If either argument is NaN, or the first argument is infinite, 427 * or the second argument is positive zero or negative zero, then the 428 * result is NaN. 429 * <li>If the first argument is finite and the second argument is 430 * infinite, then the result is the same as the first argument.</ul> 431 * 432 * @param f1 the dividend. 433 * @param f2 the divisor. 434 * @return the remainder when {@code f1} is divided by 435 * {@code f2}. 436 */ 437 // Android-changed: Reimplement in native 438 // public static double IEEEremainder(double f1, double f2) { 439 // return FdLibm.IEEEremainder.compute(f1, f2); 440 // } IEEEremainder(double f1, double f2)441 public static native double IEEEremainder(double f1, double f2); 442 443 /** 444 * Returns the smallest (closest to negative infinity) 445 * {@code double} value that is greater than or equal to the 446 * argument and is equal to a mathematical integer. Special cases: 447 * <ul><li>If the argument value is already equal to a 448 * mathematical integer, then the result is the same as the 449 * argument. <li>If the argument is NaN or an infinity or 450 * positive zero or negative zero, then the result is the same as 451 * the argument. <li>If the argument value is less than zero but 452 * greater than -1.0, then the result is negative zero.</ul> Note 453 * that the value of {@code StrictMath.ceil(x)} is exactly the 454 * value of {@code -StrictMath.floor(-x)}. 455 * 456 * @param a a value. 457 * @return the smallest (closest to negative infinity) 458 * floating-point value that is greater than or equal to 459 * the argument and is equal to a mathematical integer. 460 */ ceil(double a)461 public static double ceil(double a) { 462 return floorOrCeil(a, -0.0, 1.0, 1.0); 463 } 464 465 /** 466 * Returns the largest (closest to positive infinity) 467 * {@code double} value that is less than or equal to the 468 * argument and is equal to a mathematical integer. Special cases: 469 * <ul><li>If the argument value is already equal to a 470 * mathematical integer, then the result is the same as the 471 * argument. <li>If the argument is NaN or an infinity or 472 * positive zero or negative zero, then the result is the same as 473 * the argument.</ul> 474 * 475 * @param a a value. 476 * @return the largest (closest to positive infinity) 477 * floating-point value that less than or equal to the argument 478 * and is equal to a mathematical integer. 479 */ floor(double a)480 public static double floor(double a) { 481 return floorOrCeil(a, -1.0, 0.0, -1.0); 482 } 483 484 /** 485 * Internal method to share logic between floor and ceil. 486 * 487 * @param a the value to be floored or ceiled 488 * @param negativeBoundary result for values in (-1, 0) 489 * @param positiveBoundary result for values in (0, 1) 490 * @param sign the sign of the result 491 */ floorOrCeil(double a, double negativeBoundary, double positiveBoundary, double sign)492 private static double floorOrCeil(double a, 493 double negativeBoundary, 494 double positiveBoundary, 495 double sign) { 496 int exponent = Math.getExponent(a); 497 498 if (exponent < 0) { 499 /* 500 * Absolute value of argument is less than 1. 501 * floorOrCeil(-0.0) => -0.0 502 * floorOrCeil(+0.0) => +0.0 503 */ 504 return ((a == 0.0) ? a : 505 ( (a < 0.0) ? negativeBoundary : positiveBoundary) ); 506 } else if (exponent >= 52) { 507 /* 508 * Infinity, NaN, or a value so large it must be integral. 509 */ 510 return a; 511 } 512 // Else the argument is either an integral value already XOR it 513 // has to be rounded to one. 514 assert exponent >= 0 && exponent <= 51; 515 516 long doppel = Double.doubleToRawLongBits(a); 517 long mask = DoubleConsts.SIGNIF_BIT_MASK >> exponent; 518 519 if ( (mask & doppel) == 0L ) 520 return a; // integral value 521 else { 522 double result = Double.longBitsToDouble(doppel & (~mask)); 523 if (sign*a > 0.0) 524 result = result + sign; 525 return result; 526 } 527 } 528 529 /** 530 * Returns the {@code double} value that is closest in value 531 * to the argument and is equal to a mathematical integer. If two 532 * {@code double} values that are mathematical integers are 533 * equally close to the value of the argument, the result is the 534 * integer value that is even. Special cases: 535 * <ul><li>If the argument value is already equal to a mathematical 536 * integer, then the result is the same as the argument. 537 * <li>If the argument is NaN or an infinity or positive zero or negative 538 * zero, then the result is the same as the argument.</ul> 539 * 540 * @param a a value. 541 * @return the closest floating-point value to {@code a} that is 542 * equal to a mathematical integer. 543 * @author Joseph D. Darcy 544 */ rint(double a)545 public static double rint(double a) { 546 /* 547 * If the absolute value of a is not less than 2^52, it 548 * is either a finite integer (the double format does not have 549 * enough significand bits for a number that large to have any 550 * fractional portion), an infinity, or a NaN. In any of 551 * these cases, rint of the argument is the argument. 552 * 553 * Otherwise, the sum (twoToThe52 + a ) will properly round 554 * away any fractional portion of a since ulp(twoToThe52) == 555 * 1.0; subtracting out twoToThe52 from this sum will then be 556 * exact and leave the rounded integer portion of a. 557 */ 558 double twoToThe52 = (double)(1L << 52); // 2^52 559 double sign = Math.copySign(1.0, a); // preserve sign info 560 a = Math.abs(a); 561 562 if (a < twoToThe52) { // E_min <= ilogb(a) <= 51 563 a = ((twoToThe52 + a ) - twoToThe52); 564 } 565 566 return sign * a; // restore original sign 567 } 568 569 /** 570 * Returns the angle <i>theta</i> from the conversion of rectangular 571 * coordinates ({@code x}, {@code y}) to polar 572 * coordinates (r, <i>theta</i>). 573 * This method computes the phase <i>theta</i> by computing an arc tangent 574 * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special 575 * cases: 576 * <ul><li>If either argument is NaN, then the result is NaN. 577 * <li>If the first argument is positive zero and the second argument 578 * is positive, or the first argument is positive and finite and the 579 * second argument is positive infinity, then the result is positive 580 * zero. 581 * <li>If the first argument is negative zero and the second argument 582 * is positive, or the first argument is negative and finite and the 583 * second argument is positive infinity, then the result is negative zero. 584 * <li>If the first argument is positive zero and the second argument 585 * is negative, or the first argument is positive and finite and the 586 * second argument is negative infinity, then the result is the 587 * {@code double} value closest to <i>pi</i>. 588 * <li>If the first argument is negative zero and the second argument 589 * is negative, or the first argument is negative and finite and the 590 * second argument is negative infinity, then the result is the 591 * {@code double} value closest to -<i>pi</i>. 592 * <li>If the first argument is positive and the second argument is 593 * positive zero or negative zero, or the first argument is positive 594 * infinity and the second argument is finite, then the result is the 595 * {@code double} value closest to <i>pi</i>/2. 596 * <li>If the first argument is negative and the second argument is 597 * positive zero or negative zero, or the first argument is negative 598 * infinity and the second argument is finite, then the result is the 599 * {@code double} value closest to -<i>pi</i>/2. 600 * <li>If both arguments are positive infinity, then the result is the 601 * {@code double} value closest to <i>pi</i>/4. 602 * <li>If the first argument is positive infinity and the second argument 603 * is negative infinity, then the result is the {@code double} 604 * value closest to 3*<i>pi</i>/4. 605 * <li>If the first argument is negative infinity and the second argument 606 * is positive infinity, then the result is the {@code double} value 607 * closest to -<i>pi</i>/4. 608 * <li>If both arguments are negative infinity, then the result is the 609 * {@code double} value closest to -3*<i>pi</i>/4.</ul> 610 * 611 * @apiNote 612 * For <i>y</i> with a positive sign and finite nonzero 613 * <i>x</i>, the exact mathematical value of {@code atan2} is 614 * equal to: 615 * <ul> 616 * <li>If <i>x</i> {@literal >} 0, atan(abs(<i>y</i>/<i>x</i>)) 617 * <li>If <i>x</i> {@literal <} 0, π - atan(abs(<i>y</i>/<i>x</i>)) 618 * </ul> 619 * 620 * @param y the ordinate coordinate 621 * @param x the abscissa coordinate 622 * @return the <i>theta</i> component of the point 623 * (<i>r</i>, <i>theta</i>) 624 * in polar coordinates that corresponds to the point 625 * (<i>x</i>, <i>y</i>) in Cartesian coordinates. 626 */ 627 // Android-changed: Reimplement in native 628 // public static double atan2(double y, double x) { 629 // return FdLibm.Atan2.compute(y, x); 630 // } atan2(double y, double x)631 public static native double atan2(double y, double x); 632 633 /** 634 * Returns the value of the first argument raised to the power of the 635 * second argument. Special cases: 636 * 637 * <ul><li>If the second argument is positive or negative zero, then the 638 * result is 1.0. 639 * <li>If the second argument is 1.0, then the result is the same as the 640 * first argument. 641 * <li>If the second argument is NaN, then the result is NaN. 642 * <li>If the first argument is NaN and the second argument is nonzero, 643 * then the result is NaN. 644 * 645 * <li>If 646 * <ul> 647 * <li>the absolute value of the first argument is greater than 1 648 * and the second argument is positive infinity, or 649 * <li>the absolute value of the first argument is less than 1 and 650 * the second argument is negative infinity, 651 * </ul> 652 * then the result is positive infinity. 653 * 654 * <li>If 655 * <ul> 656 * <li>the absolute value of the first argument is greater than 1 and 657 * the second argument is negative infinity, or 658 * <li>the absolute value of the 659 * first argument is less than 1 and the second argument is positive 660 * infinity, 661 * </ul> 662 * then the result is positive zero. 663 * 664 * <li>If the absolute value of the first argument equals 1 and the 665 * second argument is infinite, then the result is NaN. 666 * 667 * <li>If 668 * <ul> 669 * <li>the first argument is positive zero and the second argument 670 * is greater than zero, or 671 * <li>the first argument is positive infinity and the second 672 * argument is less than zero, 673 * </ul> 674 * then the result is positive zero. 675 * 676 * <li>If 677 * <ul> 678 * <li>the first argument is positive zero and the second argument 679 * is less than zero, or 680 * <li>the first argument is positive infinity and the second 681 * argument is greater than zero, 682 * </ul> 683 * then the result is positive infinity. 684 * 685 * <li>If 686 * <ul> 687 * <li>the first argument is negative zero and the second argument 688 * is greater than zero but not a finite odd integer, or 689 * <li>the first argument is negative infinity and the second 690 * argument is less than zero but not a finite odd integer, 691 * </ul> 692 * then the result is positive zero. 693 * 694 * <li>If 695 * <ul> 696 * <li>the first argument is negative zero and the second argument 697 * is a positive finite odd integer, or 698 * <li>the first argument is negative infinity and the second 699 * argument is a negative finite odd integer, 700 * </ul> 701 * then the result is negative zero. 702 * 703 * <li>If 704 * <ul> 705 * <li>the first argument is negative zero and the second argument 706 * is less than zero but not a finite odd integer, or 707 * <li>the first argument is negative infinity and the second 708 * argument is greater than zero but not a finite odd integer, 709 * </ul> 710 * then the result is positive infinity. 711 * 712 * <li>If 713 * <ul> 714 * <li>the first argument is negative zero and the second argument 715 * is a negative finite odd integer, or 716 * <li>the first argument is negative infinity and the second 717 * argument is a positive finite odd integer, 718 * </ul> 719 * then the result is negative infinity. 720 * 721 * <li>If the first argument is finite and less than zero 722 * <ul> 723 * <li> if the second argument is a finite even integer, the 724 * result is equal to the result of raising the absolute value of 725 * the first argument to the power of the second argument 726 * 727 * <li>if the second argument is a finite odd integer, the result 728 * is equal to the negative of the result of raising the absolute 729 * value of the first argument to the power of the second 730 * argument 731 * 732 * <li>if the second argument is finite and not an integer, then 733 * the result is NaN. 734 * </ul> 735 * 736 * <li>If both arguments are integers, then the result is exactly equal 737 * to the mathematical result of raising the first argument to the power 738 * of the second argument if that result can in fact be represented 739 * exactly as a {@code double} value.</ul> 740 * 741 * <p>(In the foregoing descriptions, a floating-point value is 742 * considered to be an integer if and only if it is finite and a 743 * fixed point of the method {@link #ceil ceil} or, 744 * equivalently, a fixed point of the method {@link #floor 745 * floor}. A value is a fixed point of a one-argument 746 * method if and only if the result of applying the method to the 747 * value is equal to the value.) 748 * 749 * @apiNote 750 * The special cases definitions of this method differ from the 751 * special case definitions of the IEEE 754 recommended {@code 752 * pow} operation for ±{@code 1.0} raised to an infinite 753 * power. This method treats such cases as indeterminate and 754 * specifies a NaN is returned. The IEEE 754 specification treats 755 * the infinite power as a large integer (large-magnitude 756 * floating-point numbers are numerically integers, specifically 757 * even integers) and therefore specifies {@code 1.0} be returned. 758 * 759 * @param a base. 760 * @param b the exponent. 761 * @return the value {@code a}<sup>{@code b}</sup>. 762 */ 763 // BEGIN Android-changed: Reimplement in native 764 /* 765 public static double pow(double a, double b) { 766 return FdLibm.Pow.compute(a, b); 767 } 768 */ 769 // END Android-changed: Reimplement in native pow(double a, double b)770 public static native double pow(double a, double b); 771 772 /** 773 * Returns the closest {@code int} to the argument, with ties 774 * rounding to positive infinity. 775 * 776 * <p>Special cases: 777 * <ul><li>If the argument is NaN, the result is 0. 778 * <li>If the argument is negative infinity or any value less than or 779 * equal to the value of {@code Integer.MIN_VALUE}, the result is 780 * equal to the value of {@code Integer.MIN_VALUE}. 781 * <li>If the argument is positive infinity or any value greater than or 782 * equal to the value of {@code Integer.MAX_VALUE}, the result is 783 * equal to the value of {@code Integer.MAX_VALUE}.</ul> 784 * 785 * @param a a floating-point value to be rounded to an integer. 786 * @return the value of the argument rounded to the nearest 787 * {@code int} value. 788 * @see java.lang.Integer#MAX_VALUE 789 * @see java.lang.Integer#MIN_VALUE 790 */ round(float a)791 public static int round(float a) { 792 return Math.round(a); 793 } 794 795 /** 796 * Returns the closest {@code long} to the argument, with ties 797 * rounding to positive infinity. 798 * 799 * <p>Special cases: 800 * <ul><li>If the argument is NaN, the result is 0. 801 * <li>If the argument is negative infinity or any value less than or 802 * equal to the value of {@code Long.MIN_VALUE}, the result is 803 * equal to the value of {@code Long.MIN_VALUE}. 804 * <li>If the argument is positive infinity or any value greater than or 805 * equal to the value of {@code Long.MAX_VALUE}, the result is 806 * equal to the value of {@code Long.MAX_VALUE}.</ul> 807 * 808 * @param a a floating-point value to be rounded to a 809 * {@code long}. 810 * @return the value of the argument rounded to the nearest 811 * {@code long} value. 812 * @see java.lang.Long#MAX_VALUE 813 * @see java.lang.Long#MIN_VALUE 814 */ round(double a)815 public static long round(double a) { 816 return Math.round(a); 817 } 818 819 private static final class RandomNumberGeneratorHolder { 820 static final Random randomNumberGenerator = new Random(); 821 } 822 823 /** 824 * Returns a {@code double} value with a positive sign, greater 825 * than or equal to {@code 0.0} and less than {@code 1.0}. 826 * Returned values are chosen pseudorandomly with (approximately) 827 * uniform distribution from that range. 828 * 829 * <p>When this method is first called, it creates a single new 830 * pseudorandom-number generator, exactly as if by the expression 831 * 832 * <blockquote>{@code new java.util.Random()}</blockquote> 833 * 834 * This new pseudorandom-number generator is used thereafter for 835 * all calls to this method and is used nowhere else. 836 * 837 * <p>This method is properly synchronized to allow correct use by 838 * more than one thread. However, if many threads need to generate 839 * pseudorandom numbers at a great rate, it may reduce contention 840 * for each thread to have its own pseudorandom-number generator. 841 * 842 * @return a pseudorandom {@code double} greater than or equal 843 * to {@code 0.0} and less than {@code 1.0}. 844 * @see Random#nextDouble() 845 */ random()846 public static double random() { 847 return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble(); 848 } 849 850 /** 851 * Returns the sum of its arguments, 852 * throwing an exception if the result overflows an {@code int}. 853 * 854 * @param x the first value 855 * @param y the second value 856 * @return the result 857 * @throws ArithmeticException if the result overflows an int 858 * @see Math#addExact(int,int) 859 * @since 1.8 860 */ addExact(int x, int y)861 public static int addExact(int x, int y) { 862 return Math.addExact(x, y); 863 } 864 865 /** 866 * Returns the sum of its arguments, 867 * throwing an exception if the result overflows a {@code long}. 868 * 869 * @param x the first value 870 * @param y the second value 871 * @return the result 872 * @throws ArithmeticException if the result overflows a long 873 * @see Math#addExact(long,long) 874 * @since 1.8 875 */ addExact(long x, long y)876 public static long addExact(long x, long y) { 877 return Math.addExact(x, y); 878 } 879 880 /** 881 * Returns the difference of the arguments, 882 * throwing an exception if the result overflows an {@code int}. 883 * 884 * @param x the first value 885 * @param y the second value to subtract from the first 886 * @return the result 887 * @throws ArithmeticException if the result overflows an int 888 * @see Math#subtractExact(int,int) 889 * @since 1.8 890 */ subtractExact(int x, int y)891 public static int subtractExact(int x, int y) { 892 return Math.subtractExact(x, y); 893 } 894 895 /** 896 * Returns the difference of the arguments, 897 * throwing an exception if the result overflows a {@code long}. 898 * 899 * @param x the first value 900 * @param y the second value to subtract from the first 901 * @return the result 902 * @throws ArithmeticException if the result overflows a long 903 * @see Math#subtractExact(long,long) 904 * @since 1.8 905 */ subtractExact(long x, long y)906 public static long subtractExact(long x, long y) { 907 return Math.subtractExact(x, y); 908 } 909 910 /** 911 * Returns the product of the arguments, 912 * throwing an exception if the result overflows an {@code int}. 913 * 914 * @param x the first value 915 * @param y the second value 916 * @return the result 917 * @throws ArithmeticException if the result overflows an int 918 * @see Math#multiplyExact(int,int) 919 * @since 1.8 920 */ multiplyExact(int x, int y)921 public static int multiplyExact(int x, int y) { 922 return Math.multiplyExact(x, y); 923 } 924 925 /** 926 * Returns the product of the arguments, throwing an exception if the result 927 * overflows a {@code long}. 928 * 929 * @param x the first value 930 * @param y the second value 931 * @return the result 932 * @throws ArithmeticException if the result overflows a long 933 * @see Math#multiplyExact(long,int) 934 * @since 9 935 */ multiplyExact(long x, int y)936 public static long multiplyExact(long x, int y) { 937 return Math.multiplyExact(x, y); 938 } 939 940 /** 941 * Returns the product of the arguments, 942 * throwing an exception if the result overflows a {@code long}. 943 * 944 * @param x the first value 945 * @param y the second value 946 * @return the result 947 * @throws ArithmeticException if the result overflows a long 948 * @see Math#multiplyExact(long,long) 949 * @since 1.8 950 */ multiplyExact(long x, long y)951 public static long multiplyExact(long x, long y) { 952 return Math.multiplyExact(x, y); 953 } 954 955 /** 956 * Returns the quotient of the arguments, throwing an exception if the 957 * result overflows an {@code int}. Such overflow occurs in this method if 958 * {@code x} is {@link Integer#MIN_VALUE} and {@code y} is {@code -1}. 959 * In contrast, if {@code Integer.MIN_VALUE / -1} were evaluated directly, 960 * the result would be {@code Integer.MIN_VALUE} and no exception would be 961 * thrown. 962 * <p> 963 * If {@code y} is zero, an {@code ArithmeticException} is thrown 964 * (JLS {@jls 15.17.2}). 965 * <p> 966 * The built-in remainder operator "{@code %}" is a suitable counterpart 967 * both for this method and for the built-in division operator "{@code /}". 968 * 969 * @param x the dividend 970 * @param y the divisor 971 * @return the quotient {@code x / y} 972 * @throws ArithmeticException if {@code y} is zero or the quotient 973 * overflows an int 974 * @jls 15.17.2 Division Operator / 975 * @see Math#divideExact(int,int) 976 * @since 18 977 */ divideExact(int x, int y)978 public static int divideExact(int x, int y) { 979 return Math.divideExact(x, y); 980 } 981 982 /** 983 * Returns the quotient of the arguments, throwing an exception if the 984 * result overflows a {@code long}. Such overflow occurs in this method if 985 * {@code x} is {@link Long#MIN_VALUE} and {@code y} is {@code -1}. 986 * In contrast, if {@code Long.MIN_VALUE / -1} were evaluated directly, 987 * the result would be {@code Long.MIN_VALUE} and no exception would be 988 * thrown. 989 * <p> 990 * If {@code y} is zero, an {@code ArithmeticException} is thrown 991 * (JLS {@jls 15.17.2}). 992 * <p> 993 * The built-in remainder operator "{@code %}" is a suitable counterpart 994 * both for this method and for the built-in division operator "{@code /}". 995 * 996 * @param x the dividend 997 * @param y the divisor 998 * @return the quotient {@code x / y} 999 * @throws ArithmeticException if {@code y} is zero or the quotient 1000 * overflows a long 1001 * @jls 15.17.2 Division Operator / 1002 * @see Math#divideExact(long,long) 1003 * @since 18 1004 */ divideExact(long x, long y)1005 public static long divideExact(long x, long y) { 1006 return Math.divideExact(x, y); 1007 } 1008 1009 /** 1010 * Returns the largest (closest to positive infinity) 1011 * {@code int} value that is less than or equal to the algebraic quotient. 1012 * This method is identical to {@link #floorDiv(int,int)} except that it 1013 * throws an {@code ArithmeticException} when the dividend is 1014 * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is 1015 * {@code -1} instead of ignoring the integer overflow and returning 1016 * {@code Integer.MIN_VALUE}. 1017 * <p> 1018 * The floor modulus method {@link #floorMod(int,int)} is a suitable 1019 * counterpart both for this method and for the {@link #floorDiv(int,int)} 1020 * method. 1021 * <p> 1022 * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and 1023 * a comparison to the integer division {@code /} operator. 1024 * 1025 * @param x the dividend 1026 * @param y the divisor 1027 * @return the largest (closest to positive infinity) 1028 * {@code int} value that is less than or equal to the algebraic quotient. 1029 * @throws ArithmeticException if the divisor {@code y} is zero, or the 1030 * dividend {@code x} is {@code Integer.MIN_VALUE} and the divisor {@code y} 1031 * is {@code -1}. 1032 * @see Math#floorDiv(int, int) 1033 * @since 18 1034 */ floorDivExact(int x, int y)1035 public static int floorDivExact(int x, int y) { 1036 return Math.floorDivExact(x, y); 1037 } 1038 1039 /** 1040 * Returns the largest (closest to positive infinity) 1041 * {@code long} value that is less than or equal to the algebraic quotient. 1042 * This method is identical to {@link #floorDiv(long,long)} except that it 1043 * throws an {@code ArithmeticException} when the dividend is 1044 * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is 1045 * {@code -1} instead of ignoring the integer overflow and returning 1046 * {@code Long.MIN_VALUE}. 1047 * <p> 1048 * The floor modulus method {@link #floorMod(long,long)} is a suitable 1049 * counterpart both for this method and for the {@link #floorDiv(long,long)} 1050 * method. 1051 * <p> 1052 * For examples, see {@link Math#floorDiv(int, int) Math.floorDiv}. 1053 * 1054 * @param x the dividend 1055 * @param y the divisor 1056 * @return the largest (closest to positive infinity) 1057 * {@code long} value that is less than or equal to the algebraic quotient. 1058 * @throws ArithmeticException if the divisor {@code y} is zero, or the 1059 * dividend {@code x} is {@code Long.MIN_VALUE} and the divisor {@code y} 1060 * is {@code -1}. 1061 * @see Math#floorDiv(int, int) 1062 * @see Math#floorDiv(long,long) 1063 * @since 18 1064 */ floorDivExact(long x, long y)1065 public static long floorDivExact(long x, long y) { 1066 return Math.floorDivExact(x, y); 1067 } 1068 1069 /** 1070 * Returns the smallest (closest to negative infinity) 1071 * {@code int} value that is greater than or equal to the algebraic quotient. 1072 * This method is identical to {@link #ceilDiv(int,int)} except that it 1073 * throws an {@code ArithmeticException} when the dividend is 1074 * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is 1075 * {@code -1} instead of ignoring the integer overflow and returning 1076 * {@code Integer.MIN_VALUE}. 1077 * <p> 1078 * The ceil modulus method {@link #ceilMod(int,int)} is a suitable 1079 * counterpart both for this method and for the {@link #ceilDiv(int,int)} 1080 * method. 1081 * <p> 1082 * See {@link Math#ceilDiv(int, int) Math.ceilDiv} for examples and 1083 * a comparison to the integer division {@code /} operator. 1084 * 1085 * @param x the dividend 1086 * @param y the divisor 1087 * @return the smallest (closest to negative infinity) 1088 * {@code int} value that is greater than or equal to the algebraic quotient. 1089 * @throws ArithmeticException if the divisor {@code y} is zero, or the 1090 * dividend {@code x} is {@code Integer.MIN_VALUE} and the divisor {@code y} 1091 * is {@code -1}. 1092 * @see Math#ceilDiv(int, int) 1093 * @since 18 1094 */ ceilDivExact(int x, int y)1095 public static int ceilDivExact(int x, int y) { 1096 return Math.ceilDivExact(x, y); 1097 } 1098 1099 /** 1100 * Returns the smallest (closest to negative infinity) 1101 * {@code long} value that is greater than or equal to the algebraic quotient. 1102 * This method is identical to {@link #ceilDiv(long,long)} except that it 1103 * throws an {@code ArithmeticException} when the dividend is 1104 * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is 1105 * {@code -1} instead of ignoring the integer overflow and returning 1106 * {@code Long.MIN_VALUE}. 1107 * <p> 1108 * The ceil modulus method {@link #ceilMod(long,long)} is a suitable 1109 * counterpart both for this method and for the {@link #ceilDiv(long,long)} 1110 * method. 1111 * <p> 1112 * For examples, see {@link Math#ceilDiv(int, int) Math.ceilDiv}. 1113 * 1114 * @param x the dividend 1115 * @param y the divisor 1116 * @return the smallest (closest to negative infinity) 1117 * {@code long} value that is greater than or equal to the algebraic quotient. 1118 * @throws ArithmeticException if the divisor {@code y} is zero, or the 1119 * dividend {@code x} is {@code Long.MIN_VALUE} and the divisor {@code y} 1120 * is {@code -1}. 1121 * @see Math#ceilDiv(int, int) 1122 * @see Math#ceilDiv(long,long) 1123 * @since 18 1124 */ ceilDivExact(long x, long y)1125 public static long ceilDivExact(long x, long y) { 1126 return Math.ceilDivExact(x, y); 1127 } 1128 1129 /** 1130 * Returns the argument incremented by one, 1131 * throwing an exception if the result overflows an {@code int}. 1132 * The overflow only occurs for {@linkplain Integer#MAX_VALUE the maximum value}. 1133 * 1134 * @param a the value to increment 1135 * @return the result 1136 * @throws ArithmeticException if the result overflows an int 1137 * @see Math#incrementExact(int) 1138 * @since 14 1139 */ incrementExact(int a)1140 public static int incrementExact(int a) { 1141 return Math.incrementExact(a); 1142 } 1143 1144 /** 1145 * Returns the argument incremented by one, 1146 * throwing an exception if the result overflows a {@code long}. 1147 * The overflow only occurs for {@linkplain Long#MAX_VALUE the maximum value}. 1148 * 1149 * @param a the value to increment 1150 * @return the result 1151 * @throws ArithmeticException if the result overflows a long 1152 * @see Math#incrementExact(long) 1153 * @since 14 1154 */ incrementExact(long a)1155 public static long incrementExact(long a) { 1156 return Math.incrementExact(a); 1157 } 1158 1159 /** 1160 * Returns the argument decremented by one, 1161 * throwing an exception if the result overflows an {@code int}. 1162 * The overflow only occurs for {@linkplain Integer#MIN_VALUE the minimum value}. 1163 * 1164 * @param a the value to decrement 1165 * @return the result 1166 * @throws ArithmeticException if the result overflows an int 1167 * @see Math#decrementExact(int) 1168 * @since 14 1169 */ decrementExact(int a)1170 public static int decrementExact(int a) { 1171 return Math.decrementExact(a); 1172 } 1173 1174 /** 1175 * Returns the argument decremented by one, 1176 * throwing an exception if the result overflows a {@code long}. 1177 * The overflow only occurs for {@linkplain Long#MIN_VALUE the minimum value}. 1178 * 1179 * @param a the value to decrement 1180 * @return the result 1181 * @throws ArithmeticException if the result overflows a long 1182 * @see Math#decrementExact(long) 1183 * @since 14 1184 */ decrementExact(long a)1185 public static long decrementExact(long a) { 1186 return Math.decrementExact(a); 1187 } 1188 1189 /** 1190 * Returns the negation of the argument, 1191 * throwing an exception if the result overflows an {@code int}. 1192 * The overflow only occurs for {@linkplain Integer#MIN_VALUE the minimum value}. 1193 * 1194 * @param a the value to negate 1195 * @return the result 1196 * @throws ArithmeticException if the result overflows an int 1197 * @see Math#negateExact(int) 1198 * @since 14 1199 */ negateExact(int a)1200 public static int negateExact(int a) { 1201 return Math.negateExact(a); 1202 } 1203 1204 /** 1205 * Returns the negation of the argument, 1206 * throwing an exception if the result overflows a {@code long}. 1207 * The overflow only occurs for {@linkplain Long#MIN_VALUE the minimum value}. 1208 * 1209 * @param a the value to negate 1210 * @return the result 1211 * @throws ArithmeticException if the result overflows a long 1212 * @see Math#negateExact(long) 1213 * @since 14 1214 */ negateExact(long a)1215 public static long negateExact(long a) { 1216 return Math.negateExact(a); 1217 } 1218 1219 /** 1220 * Returns the value of the {@code long} argument, throwing an exception 1221 * if the value overflows an {@code int}. 1222 * 1223 * @param value the long value 1224 * @return the argument as an int 1225 * @throws ArithmeticException if the {@code argument} overflows an int 1226 * @see Math#toIntExact(long) 1227 * @since 1.8 1228 */ toIntExact(long value)1229 public static int toIntExact(long value) { 1230 return Math.toIntExact(value); 1231 } 1232 1233 /** 1234 * Returns the exact mathematical product of the arguments. 1235 * 1236 * @param x the first value 1237 * @param y the second value 1238 * @return the result 1239 * @see Math#multiplyFull(int,int) 1240 * @since 9 1241 */ multiplyFull(int x, int y)1242 public static long multiplyFull(int x, int y) { 1243 return Math.multiplyFull(x, y); 1244 } 1245 1246 /** 1247 * Returns as a {@code long} the most significant 64 bits of the 128-bit 1248 * product of two 64-bit factors. 1249 * 1250 * @param x the first value 1251 * @param y the second value 1252 * @return the result 1253 * @see #unsignedMultiplyHigh 1254 * @see Math#multiplyHigh(long,long) 1255 * @since 9 1256 */ multiplyHigh(long x, long y)1257 public static long multiplyHigh(long x, long y) { 1258 return Math.multiplyHigh(x, y); 1259 } 1260 1261 /** 1262 * Returns as a {@code long} the most significant 64 bits of the unsigned 1263 * 128-bit product of two unsigned 64-bit factors. 1264 * 1265 * @param x the first value 1266 * @param y the second value 1267 * @return the result 1268 * @see #multiplyHigh 1269 * @see Math#unsignedMultiplyHigh(long,long) 1270 * @since 18 1271 */ unsignedMultiplyHigh(long x, long y)1272 public static long unsignedMultiplyHigh(long x, long y) { 1273 return Math.unsignedMultiplyHigh(x, y); 1274 } 1275 1276 /** 1277 * Returns the largest (closest to positive infinity) 1278 * {@code int} value that is less than or equal to the algebraic quotient. 1279 * There is one special case: if the dividend is 1280 * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1}, 1281 * then integer overflow occurs and 1282 * the result is equal to {@code Integer.MIN_VALUE}. 1283 * <p> 1284 * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and 1285 * a comparison to the integer division {@code /} operator. 1286 * 1287 * @param x the dividend 1288 * @param y the divisor 1289 * @return the largest (closest to positive infinity) 1290 * {@code int} value that is less than or equal to the algebraic quotient. 1291 * @throws ArithmeticException if the divisor {@code y} is zero 1292 * @see Math#floorDiv(int, int) 1293 * @see Math#floor(double) 1294 * @since 1.8 1295 */ floorDiv(int x, int y)1296 public static int floorDiv(int x, int y) { 1297 return Math.floorDiv(x, y); 1298 } 1299 1300 /** 1301 * Returns the largest (closest to positive infinity) 1302 * {@code long} value that is less than or equal to the algebraic quotient. 1303 * There is one special case: if the dividend is 1304 * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1}, 1305 * then integer overflow occurs and 1306 * the result is equal to {@code Long.MIN_VALUE}. 1307 * <p> 1308 * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and 1309 * a comparison to the integer division {@code /} operator. 1310 * 1311 * @param x the dividend 1312 * @param y the divisor 1313 * @return the largest (closest to positive infinity) 1314 * {@code long} value that is less than or equal to the algebraic quotient. 1315 * @throws ArithmeticException if the divisor {@code y} is zero 1316 * @see Math#floorDiv(long, int) 1317 * @see Math#floor(double) 1318 * @since 9 1319 */ floorDiv(long x, int y)1320 public static long floorDiv(long x, int y) { 1321 return Math.floorDiv(x, y); 1322 } 1323 1324 /** 1325 * Returns the largest (closest to positive infinity) 1326 * {@code long} value that is less than or equal to the algebraic quotient. 1327 * There is one special case: if the dividend is 1328 * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1}, 1329 * then integer overflow occurs and 1330 * the result is equal to {@code Long.MIN_VALUE}. 1331 * <p> 1332 * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and 1333 * a comparison to the integer division {@code /} operator. 1334 * 1335 * @param x the dividend 1336 * @param y the divisor 1337 * @return the largest (closest to positive infinity) 1338 * {@code long} value that is less than or equal to the algebraic quotient. 1339 * @throws ArithmeticException if the divisor {@code y} is zero 1340 * @see Math#floorDiv(long, long) 1341 * @see Math#floor(double) 1342 * @since 1.8 1343 */ floorDiv(long x, long y)1344 public static long floorDiv(long x, long y) { 1345 return Math.floorDiv(x, y); 1346 } 1347 1348 /** 1349 * Returns the floor modulus of the {@code int} arguments. 1350 * <p> 1351 * The floor modulus is {@code r = x - (floorDiv(x, y) * y)}, 1352 * has the same sign as the divisor {@code y} or is zero, and 1353 * is in the range of {@code -abs(y) < r < +abs(y)}. 1354 * 1355 * <p> 1356 * The relationship between {@code floorDiv} and {@code floorMod} is such that: 1357 * <ul> 1358 * <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}</li> 1359 * </ul> 1360 * <p> 1361 * See {@link Math#floorMod(int, int) Math.floorMod} for examples and 1362 * a comparison to the {@code %} operator. 1363 * 1364 * @param x the dividend 1365 * @param y the divisor 1366 * @return the floor modulus {@code x - (floorDiv(x, y) * y)} 1367 * @throws ArithmeticException if the divisor {@code y} is zero 1368 * @see Math#floorMod(int, int) 1369 * @see StrictMath#floorDiv(int, int) 1370 * @since 1.8 1371 */ floorMod(int x, int y)1372 public static int floorMod(int x, int y) { 1373 return Math.floorMod(x , y); 1374 } 1375 1376 /** 1377 * Returns the floor modulus of the {@code long} and {@code int} arguments. 1378 * <p> 1379 * The floor modulus is {@code r = x - (floorDiv(x, y) * y)}, 1380 * has the same sign as the divisor {@code y} or is zero, and 1381 * is in the range of {@code -abs(y) < r < +abs(y)}. 1382 * 1383 * <p> 1384 * The relationship between {@code floorDiv} and {@code floorMod} is such that: 1385 * <ul> 1386 * <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}</li> 1387 * </ul> 1388 * <p> 1389 * See {@link Math#floorMod(int, int) Math.floorMod} for examples and 1390 * a comparison to the {@code %} operator. 1391 * 1392 * @param x the dividend 1393 * @param y the divisor 1394 * @return the floor modulus {@code x - (floorDiv(x, y) * y)} 1395 * @throws ArithmeticException if the divisor {@code y} is zero 1396 * @see Math#floorMod(long, int) 1397 * @see StrictMath#floorDiv(long, int) 1398 * @since 9 1399 */ floorMod(long x, int y)1400 public static int floorMod(long x, int y) { 1401 return Math.floorMod(x , y); 1402 } 1403 1404 /** 1405 * Returns the floor modulus of the {@code long} arguments. 1406 * <p> 1407 * The floor modulus is {@code r = x - (floorDiv(x, y) * y)}, 1408 * has the same sign as the divisor {@code y} or is zero, and 1409 * is in the range of {@code -abs(y) < r < +abs(y)}. 1410 * 1411 * <p> 1412 * The relationship between {@code floorDiv} and {@code floorMod} is such that: 1413 * <ul> 1414 * <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}</li> 1415 * </ul> 1416 * <p> 1417 * See {@link Math#floorMod(int, int) Math.floorMod} for examples and 1418 * a comparison to the {@code %} operator. 1419 * 1420 * @param x the dividend 1421 * @param y the divisor 1422 * @return the floor modulus {@code x - (floorDiv(x, y) * y)} 1423 * @throws ArithmeticException if the divisor {@code y} is zero 1424 * @see Math#floorMod(long, long) 1425 * @see StrictMath#floorDiv(long, long) 1426 * @since 1.8 1427 */ floorMod(long x, long y)1428 public static long floorMod(long x, long y) { 1429 return Math.floorMod(x, y); 1430 } 1431 1432 /** 1433 * Returns the smallest (closest to negative infinity) 1434 * {@code int} value that is greater than or equal to the algebraic quotient. 1435 * There is one special case: if the dividend is 1436 * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1}, 1437 * then integer overflow occurs and 1438 * the result is equal to {@code Integer.MIN_VALUE}. 1439 * <p> 1440 * See {@link Math#ceilDiv(int, int) Math.ceilDiv} for examples and 1441 * a comparison to the integer division {@code /} operator. 1442 * 1443 * @param x the dividend 1444 * @param y the divisor 1445 * @return the smallest (closest to negative infinity) 1446 * {@code int} value that is greater than or equal to the algebraic quotient. 1447 * @throws ArithmeticException if the divisor {@code y} is zero 1448 * @see Math#ceilDiv(int, int) 1449 * @see Math#ceil(double) 1450 * @since 18 1451 */ ceilDiv(int x, int y)1452 public static int ceilDiv(int x, int y) { 1453 return Math.ceilDiv(x, y); 1454 } 1455 1456 /** 1457 * Returns the smallest (closest to negative infinity) 1458 * {@code long} value that is greater than or equal to the algebraic quotient. 1459 * There is one special case: if the dividend is 1460 * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1}, 1461 * then integer overflow occurs and 1462 * the result is equal to {@code Long.MIN_VALUE}. 1463 * <p> 1464 * See {@link Math#ceilDiv(int, int) Math.ceilDiv} for examples and 1465 * a comparison to the integer division {@code /} operator. 1466 * 1467 * @param x the dividend 1468 * @param y the divisor 1469 * @return the smallest (closest to negative infinity) 1470 * {@code long} value that is greater than or equal to the algebraic quotient. 1471 * @throws ArithmeticException if the divisor {@code y} is zero 1472 * @see Math#ceilDiv(long, int) 1473 * @see Math#ceil(double) 1474 * @since 18 1475 */ ceilDiv(long x, int y)1476 public static long ceilDiv(long x, int y) { 1477 return Math.ceilDiv(x, y); 1478 } 1479 1480 /** 1481 * Returns the smallest (closest to negative infinity) 1482 * {@code long} value that is greater than or equal to the algebraic quotient. 1483 * There is one special case: if the dividend is 1484 * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1}, 1485 * then integer overflow occurs and 1486 * the result is equal to {@code Long.MIN_VALUE}. 1487 * <p> 1488 * See {@link Math#ceilDiv(int, int) Math.ceilDiv} for examples and 1489 * a comparison to the integer division {@code /} operator. 1490 * 1491 * @param x the dividend 1492 * @param y the divisor 1493 * @return the smallest (closest to negative infinity) 1494 * {@code long} value that is greater than or equal to the algebraic quotient. 1495 * @throws ArithmeticException if the divisor {@code y} is zero 1496 * @see Math#ceilDiv(long, long) 1497 * @see Math#ceil(double) 1498 * @since 18 1499 */ ceilDiv(long x, long y)1500 public static long ceilDiv(long x, long y) { 1501 return Math.ceilDiv(x, y); 1502 } 1503 1504 /** 1505 * Returns the ceiling modulus of the {@code int} arguments. 1506 * <p> 1507 * The ceiling modulus is {@code r = x - (ceilDiv(x, y) * y)}, 1508 * has the opposite sign as the divisor {@code y} or is zero, and 1509 * is in the range of {@code -abs(y) < r < +abs(y)}. 1510 * 1511 * <p> 1512 * The relationship between {@code ceilDiv} and {@code ceilMod} is such that: 1513 * <ul> 1514 * <li>{@code ceilDiv(x, y) * y + ceilMod(x, y) == x}</li> 1515 * </ul> 1516 * <p> 1517 * See {@link Math#ceilMod(int, int) Math.ceilMod} for examples and 1518 * a comparison to the {@code %} operator. 1519 * 1520 * @param x the dividend 1521 * @param y the divisor 1522 * @return the ceiling modulus {@code x - (ceilDiv(x, y) * y)} 1523 * @throws ArithmeticException if the divisor {@code y} is zero 1524 * @see Math#ceilMod(int, int) 1525 * @see StrictMath#ceilDiv(int, int) 1526 * @since 18 1527 */ ceilMod(int x, int y)1528 public static int ceilMod(int x, int y) { 1529 return Math.ceilMod(x , y); 1530 } 1531 1532 /** 1533 * Returns the ceiling modulus of the {@code long} and {@code int} arguments. 1534 * <p> 1535 * The ceiling modulus is {@code r = x - (ceilDiv(x, y) * y)}, 1536 * has the opposite sign as the divisor {@code y} or is zero, and 1537 * is in the range of {@code -abs(y) < r < +abs(y)}. 1538 * 1539 * <p> 1540 * The relationship between {@code ceilDiv} and {@code ceilMod} is such that: 1541 * <ul> 1542 * <li>{@code ceilDiv(x, y) * y + ceilMod(x, y) == x}</li> 1543 * </ul> 1544 * <p> 1545 * See {@link Math#ceilMod(int, int) Math.ceilMod} for examples and 1546 * a comparison to the {@code %} operator. 1547 * 1548 * @param x the dividend 1549 * @param y the divisor 1550 * @return the ceiling modulus {@code x - (ceilDiv(x, y) * y)} 1551 * @throws ArithmeticException if the divisor {@code y} is zero 1552 * @see Math#ceilMod(long, int) 1553 * @see StrictMath#ceilDiv(long, int) 1554 * @since 18 1555 */ ceilMod(long x, int y)1556 public static int ceilMod(long x, int y) { 1557 return Math.ceilMod(x , y); 1558 } 1559 1560 /** 1561 * Returns the ceiling modulus of the {@code long} arguments. 1562 * <p> 1563 * The ceiling modulus is {@code r = x - (ceilDiv(x, y) * y)}, 1564 * has the opposite sign as the divisor {@code y} or is zero, and 1565 * is in the range of {@code -abs(y) < r < +abs(y)}. 1566 * 1567 * <p> 1568 * The relationship between {@code ceilDiv} and {@code ceilMod} is such that: 1569 * <ul> 1570 * <li>{@code ceilDiv(x, y) * y + ceilMod(x, y) == x}</li> 1571 * </ul> 1572 * <p> 1573 * See {@link Math#ceilMod(int, int) Math.ceilMod} for examples and 1574 * a comparison to the {@code %} operator. 1575 * 1576 * @param x the dividend 1577 * @param y the divisor 1578 * @return the ceiling modulus {@code x - (ceilDiv(x, y) * y)} 1579 * @throws ArithmeticException if the divisor {@code y} is zero 1580 * @see Math#ceilMod(long, long) 1581 * @see StrictMath#ceilDiv(long, long) 1582 * @since 18 1583 */ ceilMod(long x, long y)1584 public static long ceilMod(long x, long y) { 1585 return Math.ceilMod(x, y); 1586 } 1587 1588 /** 1589 * Returns the absolute value of an {@code int} value. 1590 * If the argument is not negative, the argument is returned. 1591 * If the argument is negative, the negation of the argument is returned. 1592 * 1593 * <p>Note that if the argument is equal to the value of {@link 1594 * Integer#MIN_VALUE}, the most negative representable {@code int} 1595 * value, the result is that same value, which is negative. In 1596 * contrast, the {@link StrictMath#absExact(int)} method throws an 1597 * {@code ArithmeticException} for this value. 1598 * 1599 * @param a the argument whose absolute value is to be determined. 1600 * @return the absolute value of the argument. 1601 * @see Math#absExact(int) 1602 */ abs(int a)1603 public static int abs(int a) { 1604 return Math.abs(a); 1605 } 1606 1607 /** 1608 * Returns the mathematical absolute value of an {@code int} value 1609 * if it is exactly representable as an {@code int}, throwing 1610 * {@code ArithmeticException} if the result overflows the 1611 * positive {@code int} range. 1612 * 1613 * <p>Since the range of two's complement integers is asymmetric 1614 * with one additional negative value (JLS {@jls 4.2.1}), the 1615 * mathematical absolute value of {@link Integer#MIN_VALUE} 1616 * overflows the positive {@code int} range, so an exception is 1617 * thrown for that argument. 1618 * 1619 * @param a the argument whose absolute value is to be determined 1620 * @return the absolute value of the argument, unless overflow occurs 1621 * @throws ArithmeticException if the argument is {@link Integer#MIN_VALUE} 1622 * @see Math#abs(int) 1623 * @see Math#absExact(int) 1624 * @since 15 1625 */ absExact(int a)1626 public static int absExact(int a) { 1627 return Math.absExact(a); 1628 } 1629 1630 /** 1631 * Returns the absolute value of a {@code long} value. 1632 * If the argument is not negative, the argument is returned. 1633 * If the argument is negative, the negation of the argument is returned. 1634 * 1635 * <p>Note that if the argument is equal to the value of {@link 1636 * Long#MIN_VALUE}, the most negative representable {@code long} 1637 * value, the result is that same value, which is negative. In 1638 * contrast, the {@link StrictMath#absExact(long)} method throws 1639 * an {@code ArithmeticException} for this value. 1640 * 1641 * @param a the argument whose absolute value is to be determined. 1642 * @return the absolute value of the argument. 1643 * @see Math#absExact(long) 1644 */ abs(long a)1645 public static long abs(long a) { 1646 return Math.abs(a); 1647 } 1648 1649 /** 1650 * Returns the mathematical absolute value of an {@code long} value 1651 * if it is exactly representable as an {@code long}, throwing 1652 * {@code ArithmeticException} if the result overflows the 1653 * positive {@code long} range. 1654 * 1655 * <p>Since the range of two's complement integers is asymmetric 1656 * with one additional negative value (JLS {@jls 4.2.1}), the 1657 * mathematical absolute value of {@link Long#MIN_VALUE} overflows 1658 * the positive {@code long} range, so an exception is thrown for 1659 * that argument. 1660 * 1661 * @param a the argument whose absolute value is to be determined 1662 * @return the absolute value of the argument, unless overflow occurs 1663 * @throws ArithmeticException if the argument is {@link Long#MIN_VALUE} 1664 * @see Math#abs(long) 1665 * @see Math#absExact(long) 1666 * @since 15 1667 */ absExact(long a)1668 public static long absExact(long a) { 1669 return Math.absExact(a); 1670 } 1671 1672 /** 1673 * Returns the absolute value of a {@code float} value. 1674 * If the argument is not negative, the argument is returned. 1675 * If the argument is negative, the negation of the argument is returned. 1676 * Special cases: 1677 * <ul><li>If the argument is positive zero or negative zero, the 1678 * result is positive zero. 1679 * <li>If the argument is infinite, the result is positive infinity. 1680 * <li>If the argument is NaN, the result is NaN.</ul> 1681 * 1682 * @apiNote As implied by the above, one valid implementation of 1683 * this method is given by the expression below which computes a 1684 * {@code float} with the same exponent and significand as the 1685 * argument but with a guaranteed zero sign bit indicating a 1686 * positive value: <br> 1687 * {@code Float.intBitsToFloat(0x7fffffff & Float.floatToRawIntBits(a))} 1688 * 1689 * @param a the argument whose absolute value is to be determined 1690 * @return the absolute value of the argument. 1691 */ abs(float a)1692 public static float abs(float a) { 1693 return Math.abs(a); 1694 } 1695 1696 /** 1697 * Returns the absolute value of a {@code double} value. 1698 * If the argument is not negative, the argument is returned. 1699 * If the argument is negative, the negation of the argument is returned. 1700 * Special cases: 1701 * <ul><li>If the argument is positive zero or negative zero, the result 1702 * is positive zero. 1703 * <li>If the argument is infinite, the result is positive infinity. 1704 * <li>If the argument is NaN, the result is NaN.</ul> 1705 * 1706 * @apiNote As implied by the above, one valid implementation of 1707 * this method is given by the expression below which computes a 1708 * {@code double} with the same exponent and significand as the 1709 * argument but with a guaranteed zero sign bit indicating a 1710 * positive value: <br> 1711 * {@code Double.longBitsToDouble((Double.doubleToRawLongBits(a)<<1)>>>1)} 1712 * 1713 * @param a the argument whose absolute value is to be determined 1714 * @return the absolute value of the argument. 1715 */ abs(double a)1716 public static double abs(double a) { 1717 return Math.abs(a); 1718 } 1719 1720 /** 1721 * Returns the greater of two {@code int} values. That is, the 1722 * result is the argument closer to the value of 1723 * {@link Integer#MAX_VALUE}. If the arguments have the same value, 1724 * the result is that same value. 1725 * 1726 * @param a an argument. 1727 * @param b another argument. 1728 * @return the larger of {@code a} and {@code b}. 1729 */ 1730 @IntrinsicCandidate max(int a, int b)1731 public static int max(int a, int b) { 1732 return Math.max(a, b); 1733 } 1734 1735 /** 1736 * Returns the greater of two {@code long} values. That is, the 1737 * result is the argument closer to the value of 1738 * {@link Long#MAX_VALUE}. If the arguments have the same value, 1739 * the result is that same value. 1740 * 1741 * @param a an argument. 1742 * @param b another argument. 1743 * @return the larger of {@code a} and {@code b}. 1744 */ max(long a, long b)1745 public static long max(long a, long b) { 1746 return Math.max(a, b); 1747 } 1748 1749 /** 1750 * Returns the greater of two {@code float} values. That is, 1751 * the result is the argument closer to positive infinity. If the 1752 * arguments have the same value, the result is that same 1753 * value. If either value is NaN, then the result is NaN. Unlike 1754 * the numerical comparison operators, this method considers 1755 * negative zero to be strictly smaller than positive zero. If one 1756 * argument is positive zero and the other negative zero, the 1757 * result is positive zero. 1758 * 1759 * @param a an argument. 1760 * @param b another argument. 1761 * @return the larger of {@code a} and {@code b}. 1762 */ 1763 @IntrinsicCandidate max(float a, float b)1764 public static float max(float a, float b) { 1765 return Math.max(a, b); 1766 } 1767 1768 /** 1769 * Returns the greater of two {@code double} values. That 1770 * is, the result is the argument closer to positive infinity. If 1771 * the arguments have the same value, the result is that same 1772 * value. If either value is NaN, then the result is NaN. Unlike 1773 * the numerical comparison operators, this method considers 1774 * negative zero to be strictly smaller than positive zero. If one 1775 * argument is positive zero and the other negative zero, the 1776 * result is positive zero. 1777 * 1778 * @param a an argument. 1779 * @param b another argument. 1780 * @return the larger of {@code a} and {@code b}. 1781 */ 1782 @IntrinsicCandidate max(double a, double b)1783 public static double max(double a, double b) { 1784 return Math.max(a, b); 1785 } 1786 1787 /** 1788 * Returns the smaller of two {@code int} values. That is, 1789 * the result the argument closer to the value of 1790 * {@link Integer#MIN_VALUE}. If the arguments have the same 1791 * value, the result is that same value. 1792 * 1793 * @param a an argument. 1794 * @param b another argument. 1795 * @return the smaller of {@code a} and {@code b}. 1796 */ 1797 @IntrinsicCandidate min(int a, int b)1798 public static int min(int a, int b) { 1799 return Math.min(a, b); 1800 } 1801 1802 /** 1803 * Returns the smaller of two {@code long} values. That is, 1804 * the result is the argument closer to the value of 1805 * {@link Long#MIN_VALUE}. If the arguments have the same 1806 * value, the result is that same value. 1807 * 1808 * @param a an argument. 1809 * @param b another argument. 1810 * @return the smaller of {@code a} and {@code b}. 1811 */ min(long a, long b)1812 public static long min(long a, long b) { 1813 return Math.min(a, b); 1814 } 1815 1816 /** 1817 * Returns the smaller of two {@code float} values. That is, 1818 * the result is the value closer to negative infinity. If the 1819 * arguments have the same value, the result is that same 1820 * value. If either value is NaN, then the result is NaN. Unlike 1821 * the numerical comparison operators, this method considers 1822 * negative zero to be strictly smaller than positive zero. If 1823 * one argument is positive zero and the other is negative zero, 1824 * the result is negative zero. 1825 * 1826 * @param a an argument. 1827 * @param b another argument. 1828 * @return the smaller of {@code a} and {@code b.} 1829 */ 1830 @IntrinsicCandidate min(float a, float b)1831 public static float min(float a, float b) { 1832 return Math.min(a, b); 1833 } 1834 1835 /** 1836 * Returns the smaller of two {@code double} values. That 1837 * is, the result is the value closer to negative infinity. If the 1838 * arguments have the same value, the result is that same 1839 * value. If either value is NaN, then the result is NaN. Unlike 1840 * the numerical comparison operators, this method considers 1841 * negative zero to be strictly smaller than positive zero. If one 1842 * argument is positive zero and the other is negative zero, the 1843 * result is negative zero. 1844 * 1845 * @param a an argument. 1846 * @param b another argument. 1847 * @return the smaller of {@code a} and {@code b}. 1848 */ 1849 @IntrinsicCandidate min(double a, double b)1850 public static double min(double a, double b) { 1851 return Math.min(a, b); 1852 } 1853 1854 /** 1855 * Clamps the value to fit between min and max. If the value is less 1856 * than {@code min}, then {@code min} is returned. If the value is greater 1857 * than {@code max}, then {@code max} is returned. Otherwise, the original 1858 * value is returned. 1859 * <p> 1860 * While the original value of type long may not fit into the int type, 1861 * the bounds have the int type, so the result always fits the int type. 1862 * This allows to use method to safely cast long value to int with 1863 * saturation. 1864 * 1865 * @param value value to clamp 1866 * @param min minimal allowed value 1867 * @param max maximal allowed value 1868 * @return a clamped value that fits into {@code min..max} interval 1869 * @throws IllegalArgumentException if {@code min > max} 1870 * 1871 * @since 21 1872 */ clamp(long value, int min, int max)1873 public static int clamp(long value, int min, int max) { 1874 return Math.clamp(value, min, max); 1875 } 1876 1877 /** 1878 * Clamps the value to fit between min and max. If the value is less 1879 * than {@code min}, then {@code min} is returned. If the value is greater 1880 * than {@code max}, then {@code max} is returned. Otherwise, the original 1881 * value is returned. 1882 * 1883 * @param value value to clamp 1884 * @param min minimal allowed value 1885 * @param max maximal allowed value 1886 * @return a clamped value that fits into {@code min..max} interval 1887 * @throws IllegalArgumentException if {@code min > max} 1888 * 1889 * @since 21 1890 */ clamp(long value, long min, long max)1891 public static long clamp(long value, long min, long max) { 1892 return Math.clamp(value, min, max); 1893 } 1894 1895 /** 1896 * Clamps the value to fit between min and max. If the value is less 1897 * than {@code min}, then {@code min} is returned. If the value is greater 1898 * than {@code max}, then {@code max} is returned. Otherwise, the original 1899 * value is returned. If value is NaN, the result is also NaN. 1900 * <p> 1901 * Unlike the numerical comparison operators, this method considers 1902 * negative zero to be strictly smaller than positive zero. 1903 * E.g., {@code clamp(-0.0, 0.0, 1.0)} returns 0.0. 1904 * 1905 * @param value value to clamp 1906 * @param min minimal allowed value 1907 * @param max maximal allowed value 1908 * @return a clamped value that fits into {@code min..max} interval 1909 * @throws IllegalArgumentException if either of {@code min} and {@code max} 1910 * arguments is NaN, or {@code min > max}, or {@code min} is +0.0, and 1911 * {@code max} is -0.0. 1912 * 1913 * @since 21 1914 */ clamp(double value, double min, double max)1915 public static double clamp(double value, double min, double max) { 1916 return Math.clamp(value, min, max); 1917 } 1918 1919 /** 1920 * Clamps the value to fit between min and max. If the value is less 1921 * than {@code min}, then {@code min} is returned. If the value is greater 1922 * than {@code max}, then {@code max} is returned. Otherwise, the original 1923 * value is returned. If value is NaN, the result is also NaN. 1924 * <p> 1925 * Unlike the numerical comparison operators, this method considers 1926 * negative zero to be strictly smaller than positive zero. 1927 * E.g., {@code clamp(-0.0f, 0.0f, 1.0f)} returns 0.0f. 1928 * 1929 * @param value value to clamp 1930 * @param min minimal allowed value 1931 * @param max maximal allowed value 1932 * @return a clamped value that fits into {@code min..max} interval 1933 * @throws IllegalArgumentException if either of {@code min} and {@code max} 1934 * arguments is NaN, or {@code min > max}, or {@code min} is +0.0f, and 1935 * {@code max} is -0.0f. 1936 * 1937 * @since 21 1938 */ clamp(float value, float min, float max)1939 public static float clamp(float value, float min, float max) { 1940 return Math.clamp(value, min, max); 1941 } 1942 1943 /** 1944 * Returns the fused multiply add of the three arguments; that is, 1945 * returns the exact product of the first two arguments summed 1946 * with the third argument and then rounded once to the nearest 1947 * {@code double}. 1948 * 1949 * The rounding is done using the {@linkplain 1950 * java.math.RoundingMode#HALF_EVEN round to nearest even 1951 * rounding mode}. 1952 * 1953 * In contrast, if {@code a * b + c} is evaluated as a regular 1954 * floating-point expression, two rounding errors are involved, 1955 * the first for the multiply operation, the second for the 1956 * addition operation. 1957 * 1958 * <p>Special cases: 1959 * <ul> 1960 * <li> If any argument is NaN, the result is NaN. 1961 * 1962 * <li> If one of the first two arguments is infinite and the 1963 * other is zero, the result is NaN. 1964 * 1965 * <li> If the exact product of the first two arguments is infinite 1966 * (in other words, at least one of the arguments is infinite and 1967 * the other is neither zero nor NaN) and the third argument is an 1968 * infinity of the opposite sign, the result is NaN. 1969 * 1970 * </ul> 1971 * 1972 * <p>Note that {@code fusedMac(a, 1.0, c)} returns the same 1973 * result as ({@code a + c}). However, 1974 * {@code fusedMac(a, b, +0.0)} does <em>not</em> always return the 1975 * same result as ({@code a * b}) since 1976 * {@code fusedMac(-0.0, +0.0, +0.0)} is {@code +0.0} while 1977 * ({@code -0.0 * +0.0}) is {@code -0.0}; {@code fusedMac(a, b, -0.0)} is 1978 * equivalent to ({@code a * b}) however. 1979 * 1980 * @apiNote This method corresponds to the fusedMultiplyAdd 1981 * operation defined in IEEE 754-2008. 1982 * 1983 * @param a a value 1984 * @param b a value 1985 * @param c a value 1986 * 1987 * @return (<i>a</i> × <i>b</i> + <i>c</i>) 1988 * computed, as if with unlimited range and precision, and rounded 1989 * once to the nearest {@code double} value 1990 * 1991 * @since 9 1992 */ fma(double a, double b, double c)1993 public static double fma(double a, double b, double c) { 1994 return Math.fma(a, b, c); 1995 } 1996 1997 /** 1998 * Returns the fused multiply add of the three arguments; that is, 1999 * returns the exact product of the first two arguments summed 2000 * with the third argument and then rounded once to the nearest 2001 * {@code float}. 2002 * 2003 * The rounding is done using the {@linkplain 2004 * java.math.RoundingMode#HALF_EVEN round to nearest even 2005 * rounding mode}. 2006 * 2007 * In contrast, if {@code a * b + c} is evaluated as a regular 2008 * floating-point expression, two rounding errors are involved, 2009 * the first for the multiply operation, the second for the 2010 * addition operation. 2011 * 2012 * <p>Special cases: 2013 * <ul> 2014 * <li> If any argument is NaN, the result is NaN. 2015 * 2016 * <li> If one of the first two arguments is infinite and the 2017 * other is zero, the result is NaN. 2018 * 2019 * <li> If the exact product of the first two arguments is infinite 2020 * (in other words, at least one of the arguments is infinite and 2021 * the other is neither zero nor NaN) and the third argument is an 2022 * infinity of the opposite sign, the result is NaN. 2023 * 2024 * </ul> 2025 * 2026 * <p>Note that {@code fma(a, 1.0f, c)} returns the same 2027 * result as ({@code a + c}). However, 2028 * {@code fma(a, b, +0.0f)} does <em>not</em> always return the 2029 * same result as ({@code a * b}) since 2030 * {@code fma(-0.0f, +0.0f, +0.0f)} is {@code +0.0f} while 2031 * ({@code -0.0f * +0.0f}) is {@code -0.0f}; {@code fma(a, b, -0.0f)} is 2032 * equivalent to ({@code a * b}) however. 2033 * 2034 * @apiNote This method corresponds to the fusedMultiplyAdd 2035 * operation defined in IEEE 754-2008. 2036 * 2037 * @param a a value 2038 * @param b a value 2039 * @param c a value 2040 * 2041 * @return (<i>a</i> × <i>b</i> + <i>c</i>) 2042 * computed, as if with unlimited range and precision, and rounded 2043 * once to the nearest {@code float} value 2044 * 2045 * @since 9 2046 */ fma(float a, float b, float c)2047 public static float fma(float a, float b, float c) { 2048 return Math.fma(a, b, c); 2049 } 2050 2051 /** 2052 * Returns the size of an ulp of the argument. An ulp, unit in 2053 * the last place, of a {@code double} value is the positive 2054 * distance between this floating-point value and the {@code 2055 * double} value next larger in magnitude. Note that for non-NaN 2056 * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 2057 * 2058 * <p>Special Cases: 2059 * <ul> 2060 * <li> If the argument is NaN, then the result is NaN. 2061 * <li> If the argument is positive or negative infinity, then the 2062 * result is positive infinity. 2063 * <li> If the argument is positive or negative zero, then the result is 2064 * {@code Double.MIN_VALUE}. 2065 * <li> If the argument is ±{@code Double.MAX_VALUE}, then 2066 * the result is equal to 2<sup>971</sup>. 2067 * </ul> 2068 * 2069 * @param d the floating-point value whose ulp is to be returned 2070 * @return the size of an ulp of the argument 2071 * @author Joseph D. Darcy 2072 * @since 1.5 2073 */ ulp(double d)2074 public static double ulp(double d) { 2075 return Math.ulp(d); 2076 } 2077 2078 /** 2079 * Returns the size of an ulp of the argument. An ulp, unit in 2080 * the last place, of a {@code float} value is the positive 2081 * distance between this floating-point value and the {@code 2082 * float} value next larger in magnitude. Note that for non-NaN 2083 * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 2084 * 2085 * <p>Special Cases: 2086 * <ul> 2087 * <li> If the argument is NaN, then the result is NaN. 2088 * <li> If the argument is positive or negative infinity, then the 2089 * result is positive infinity. 2090 * <li> If the argument is positive or negative zero, then the result is 2091 * {@code Float.MIN_VALUE}. 2092 * <li> If the argument is ±{@code Float.MAX_VALUE}, then 2093 * the result is equal to 2<sup>104</sup>. 2094 * </ul> 2095 * 2096 * @param f the floating-point value whose ulp is to be returned 2097 * @return the size of an ulp of the argument 2098 * @author Joseph D. Darcy 2099 * @since 1.5 2100 */ ulp(float f)2101 public static float ulp(float f) { 2102 return Math.ulp(f); 2103 } 2104 2105 /** 2106 * Returns the signum function of the argument; zero if the argument 2107 * is zero, 1.0 if the argument is greater than zero, -1.0 if the 2108 * argument is less than zero. 2109 * 2110 * <p>Special Cases: 2111 * <ul> 2112 * <li> If the argument is NaN, then the result is NaN. 2113 * <li> If the argument is positive zero or negative zero, then the 2114 * result is the same as the argument. 2115 * </ul> 2116 * 2117 * @param d the floating-point value whose signum is to be returned 2118 * @return the signum function of the argument 2119 * @author Joseph D. Darcy 2120 * @since 1.5 2121 */ signum(double d)2122 public static double signum(double d) { 2123 return Math.signum(d); 2124 } 2125 2126 /** 2127 * Returns the signum function of the argument; zero if the argument 2128 * is zero, 1.0f if the argument is greater than zero, -1.0f if the 2129 * argument is less than zero. 2130 * 2131 * <p>Special Cases: 2132 * <ul> 2133 * <li> If the argument is NaN, then the result is NaN. 2134 * <li> If the argument is positive zero or negative zero, then the 2135 * result is the same as the argument. 2136 * </ul> 2137 * 2138 * @param f the floating-point value whose signum is to be returned 2139 * @return the signum function of the argument 2140 * @author Joseph D. Darcy 2141 * @since 1.5 2142 */ signum(float f)2143 public static float signum(float f) { 2144 return Math.signum(f); 2145 } 2146 2147 /** 2148 * Returns the hyperbolic sine of a {@code double} value. 2149 * The hyperbolic sine of <i>x</i> is defined to be 2150 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2 2151 * where <i>e</i> is {@linkplain Math#E Euler's number}. 2152 * 2153 * <p>Special cases: 2154 * <ul> 2155 * 2156 * <li>If the argument is NaN, then the result is NaN. 2157 * 2158 * <li>If the argument is infinite, then the result is an infinity 2159 * with the same sign as the argument. 2160 * 2161 * <li>If the argument is zero, then the result is a zero with the 2162 * same sign as the argument. 2163 * 2164 * </ul> 2165 * 2166 * @param x The number whose hyperbolic sine is to be returned. 2167 * @return The hyperbolic sine of {@code x}. 2168 * @since 1.5 2169 */ 2170 // Android-changed: Reimplement in native 2171 // public static double sinh(double x) { 2172 // return FdLibm.Sinh.compute(x); 2173 // } sinh(double x)2174 public static native double sinh(double x); 2175 2176 /** 2177 * Returns the hyperbolic cosine of a {@code double} value. 2178 * The hyperbolic cosine of <i>x</i> is defined to be 2179 * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2 2180 * where <i>e</i> is {@linkplain Math#E Euler's number}. 2181 * 2182 * <p>Special cases: 2183 * <ul> 2184 * 2185 * <li>If the argument is NaN, then the result is NaN. 2186 * 2187 * <li>If the argument is infinite, then the result is positive 2188 * infinity. 2189 * 2190 * <li>If the argument is zero, then the result is {@code 1.0}. 2191 * 2192 * </ul> 2193 * 2194 * @param x The number whose hyperbolic cosine is to be returned. 2195 * @return The hyperbolic cosine of {@code x}. 2196 * @since 1.5 2197 */ 2198 // Android-changed: Reimplement in native 2199 // public static double cosh(double x) { 2200 // return FdLibm.Cosh.compute(x); 2201 // } cosh(double x)2202 public static native double cosh(double x); 2203 2204 /** 2205 * Returns the hyperbolic tangent of a {@code double} value. 2206 * The hyperbolic tangent of <i>x</i> is defined to be 2207 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>), 2208 * in other words, {@linkplain Math#sinh 2209 * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note 2210 * that the absolute value of the exact tanh is always less than 2211 * 1. 2212 * 2213 * <p>Special cases: 2214 * <ul> 2215 * 2216 * <li>If the argument is NaN, then the result is NaN. 2217 * 2218 * <li>If the argument is zero, then the result is a zero with the 2219 * same sign as the argument. 2220 * 2221 * <li>If the argument is positive infinity, then the result is 2222 * {@code +1.0}. 2223 * 2224 * <li>If the argument is negative infinity, then the result is 2225 * {@code -1.0}. 2226 * 2227 * </ul> 2228 * 2229 * @param x The number whose hyperbolic tangent is to be returned. 2230 * @return The hyperbolic tangent of {@code x}. 2231 * @since 1.5 2232 */ 2233 // Android-changed: Reimplement in native 2234 // public static double tanh(double x) { 2235 // return FdLibm.Tanh.compute(x); 2236 // } tanh(double x)2237 public static native double tanh(double x); 2238 2239 /** 2240 * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 2241 * without intermediate overflow or underflow. 2242 * 2243 * <p>Special cases: 2244 * <ul> 2245 * 2246 * <li> If either argument is infinite, then the result 2247 * is positive infinity. 2248 * 2249 * <li> If either argument is NaN and neither argument is infinite, 2250 * then the result is NaN. 2251 * 2252 * <li> If both arguments are zero, the result is positive zero. 2253 * </ul> 2254 * 2255 * @param x a value 2256 * @param y a value 2257 * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 2258 * without intermediate overflow or underflow 2259 * @since 1.5 2260 */ 2261 // BEGIN Android-changed: Reimplement in native 2262 /* 2263 public static double hypot(double x, double y) { 2264 return FdLibm.Hypot.compute(x, y); 2265 } 2266 */ 2267 // END Android-changed: Reimplement in native hypot(double x, double y)2268 public static native double hypot(double x, double y); 2269 2270 /** 2271 * Returns <i>e</i><sup>x</sup> -1. Note that for values of 2272 * <i>x</i> near 0, the exact sum of 2273 * {@code expm1(x)} + 1 is much closer to the true 2274 * result of <i>e</i><sup>x</sup> than {@code exp(x)}. 2275 * 2276 * <p>Special cases: 2277 * <ul> 2278 * <li>If the argument is NaN, the result is NaN. 2279 * 2280 * <li>If the argument is positive infinity, then the result is 2281 * positive infinity. 2282 * 2283 * <li>If the argument is negative infinity, then the result is 2284 * -1.0. 2285 * 2286 * <li>If the argument is zero, then the result is a zero with the 2287 * same sign as the argument. 2288 * 2289 * </ul> 2290 * 2291 * @param x the exponent to raise <i>e</i> to in the computation of 2292 * <i>e</i><sup>{@code x}</sup> -1. 2293 * @return the value <i>e</i><sup>{@code x}</sup> - 1. 2294 * @since 1.5 2295 */ 2296 // Android-changed: Reimplement in native 2297 // public static double expm1(double x) { 2298 // return FdLibm.Expm1.compute(x); 2299 // } expm1(double x)2300 public static native double expm1(double x); 2301 2302 /** 2303 * Returns the natural logarithm of the sum of the argument and 1. 2304 * Note that for small values {@code x}, the result of 2305 * {@code log1p(x)} is much closer to the true result of ln(1 2306 * + {@code x}) than the floating-point evaluation of 2307 * {@code log(1.0+x)}. 2308 * 2309 * <p>Special cases: 2310 * <ul> 2311 * 2312 * <li>If the argument is NaN or less than -1, then the result is 2313 * NaN. 2314 * 2315 * <li>If the argument is positive infinity, then the result is 2316 * positive infinity. 2317 * 2318 * <li>If the argument is negative one, then the result is 2319 * negative infinity. 2320 * 2321 * <li>If the argument is zero, then the result is a zero with the 2322 * same sign as the argument. 2323 * 2324 * </ul> 2325 * 2326 * @param x a value 2327 * @return the value ln({@code x} + 1), the natural 2328 * log of {@code x} + 1 2329 * @since 1.5 2330 */ 2331 // Android-changed: Reimplement in native 2332 // public static double log1p(double x) { 2333 // return FdLibm.Log1p.compute(x); 2334 // } log1p(double x)2335 public static native double log1p(double x); 2336 2337 /** 2338 * Returns the first floating-point argument with the sign of the 2339 * second floating-point argument. For this method, a NaN 2340 * {@code sign} argument is always treated as if it were 2341 * positive. 2342 * 2343 * @param magnitude the parameter providing the magnitude of the result 2344 * @param sign the parameter providing the sign of the result 2345 * @return a value with the magnitude of {@code magnitude} 2346 * and the sign of {@code sign}. 2347 * @since 1.6 2348 */ copySign(double magnitude, double sign)2349 public static double copySign(double magnitude, double sign) { 2350 return Math.copySign(magnitude, (Double.isNaN(sign)?1.0d:sign)); 2351 } 2352 2353 /** 2354 * Returns the first floating-point argument with the sign of the 2355 * second floating-point argument. For this method, a NaN 2356 * {@code sign} argument is always treated as if it were 2357 * positive. 2358 * 2359 * @param magnitude the parameter providing the magnitude of the result 2360 * @param sign the parameter providing the sign of the result 2361 * @return a value with the magnitude of {@code magnitude} 2362 * and the sign of {@code sign}. 2363 * @since 1.6 2364 */ copySign(float magnitude, float sign)2365 public static float copySign(float magnitude, float sign) { 2366 return Math.copySign(magnitude, (Float.isNaN(sign)?1.0f:sign)); 2367 } 2368 /** 2369 * Returns the unbiased exponent used in the representation of a 2370 * {@code float}. Special cases: 2371 * 2372 * <ul> 2373 * <li>If the argument is NaN or infinite, then the result is 2374 * {@link Float#MAX_EXPONENT} + 1. 2375 * <li>If the argument is zero or subnormal, then the result is 2376 * {@link Float#MIN_EXPONENT} -1. 2377 * </ul> 2378 * @param f a {@code float} value 2379 * @return the unbiased exponent of the argument 2380 * @since 1.6 2381 */ getExponent(float f)2382 public static int getExponent(float f) { 2383 return Math.getExponent(f); 2384 } 2385 2386 /** 2387 * Returns the unbiased exponent used in the representation of a 2388 * {@code double}. Special cases: 2389 * 2390 * <ul> 2391 * <li>If the argument is NaN or infinite, then the result is 2392 * {@link Double#MAX_EXPONENT} + 1. 2393 * <li>If the argument is zero or subnormal, then the result is 2394 * {@link Double#MIN_EXPONENT} -1. 2395 * </ul> 2396 * @param d a {@code double} value 2397 * @return the unbiased exponent of the argument 2398 * @since 1.6 2399 */ getExponent(double d)2400 public static int getExponent(double d) { 2401 return Math.getExponent(d); 2402 } 2403 2404 /** 2405 * Returns the floating-point number adjacent to the first 2406 * argument in the direction of the second argument. If both 2407 * arguments compare as equal the second argument is returned. 2408 * 2409 * <p>Special cases: 2410 * <ul> 2411 * <li> If either argument is a NaN, then NaN is returned. 2412 * 2413 * <li> If both arguments are signed zeros, {@code direction} 2414 * is returned unchanged (as implied by the requirement of 2415 * returning the second argument if the arguments compare as 2416 * equal). 2417 * 2418 * <li> If {@code start} is 2419 * ±{@link Double#MIN_VALUE} and {@code direction} 2420 * has a value such that the result should have a smaller 2421 * magnitude, then a zero with the same sign as {@code start} 2422 * is returned. 2423 * 2424 * <li> If {@code start} is infinite and 2425 * {@code direction} has a value such that the result should 2426 * have a smaller magnitude, {@link Double#MAX_VALUE} with the 2427 * same sign as {@code start} is returned. 2428 * 2429 * <li> If {@code start} is equal to ± 2430 * {@link Double#MAX_VALUE} and {@code direction} has a 2431 * value such that the result should have a larger magnitude, an 2432 * infinity with same sign as {@code start} is returned. 2433 * </ul> 2434 * 2435 * @param start starting floating-point value 2436 * @param direction value indicating which of 2437 * {@code start}'s neighbors or {@code start} should 2438 * be returned 2439 * @return The floating-point number adjacent to {@code start} in the 2440 * direction of {@code direction}. 2441 * @since 1.6 2442 */ nextAfter(double start, double direction)2443 public static double nextAfter(double start, double direction) { 2444 return Math.nextAfter(start, direction); 2445 } 2446 2447 /** 2448 * Returns the floating-point number adjacent to the first 2449 * argument in the direction of the second argument. If both 2450 * arguments compare as equal a value equivalent to the second argument 2451 * is returned. 2452 * 2453 * <p>Special cases: 2454 * <ul> 2455 * <li> If either argument is a NaN, then NaN is returned. 2456 * 2457 * <li> If both arguments are signed zeros, a value equivalent 2458 * to {@code direction} is returned. 2459 * 2460 * <li> If {@code start} is 2461 * ±{@link Float#MIN_VALUE} and {@code direction} 2462 * has a value such that the result should have a smaller 2463 * magnitude, then a zero with the same sign as {@code start} 2464 * is returned. 2465 * 2466 * <li> If {@code start} is infinite and 2467 * {@code direction} has a value such that the result should 2468 * have a smaller magnitude, {@link Float#MAX_VALUE} with the 2469 * same sign as {@code start} is returned. 2470 * 2471 * <li> If {@code start} is equal to ± 2472 * {@link Float#MAX_VALUE} and {@code direction} has a 2473 * value such that the result should have a larger magnitude, an 2474 * infinity with same sign as {@code start} is returned. 2475 * </ul> 2476 * 2477 * @param start starting floating-point value 2478 * @param direction value indicating which of 2479 * {@code start}'s neighbors or {@code start} should 2480 * be returned 2481 * @return The floating-point number adjacent to {@code start} in the 2482 * direction of {@code direction}. 2483 * @since 1.6 2484 */ nextAfter(float start, double direction)2485 public static float nextAfter(float start, double direction) { 2486 return Math.nextAfter(start, direction); 2487 } 2488 2489 /** 2490 * Returns the floating-point value adjacent to {@code d} in 2491 * the direction of positive infinity. This method is 2492 * semantically equivalent to {@code nextAfter(d, 2493 * Double.POSITIVE_INFINITY)}; however, a {@code nextUp} 2494 * implementation may run faster than its equivalent 2495 * {@code nextAfter} call. 2496 * 2497 * <p>Special Cases: 2498 * <ul> 2499 * <li> If the argument is NaN, the result is NaN. 2500 * 2501 * <li> If the argument is positive infinity, the result is 2502 * positive infinity. 2503 * 2504 * <li> If the argument is zero, the result is 2505 * {@link Double#MIN_VALUE} 2506 * 2507 * </ul> 2508 * 2509 * @param d starting floating-point value 2510 * @return The adjacent floating-point value closer to positive 2511 * infinity. 2512 * @since 1.6 2513 */ nextUp(double d)2514 public static double nextUp(double d) { 2515 return Math.nextUp(d); 2516 } 2517 2518 /** 2519 * Returns the floating-point value adjacent to {@code f} in 2520 * the direction of positive infinity. This method is 2521 * semantically equivalent to {@code nextAfter(f, 2522 * Float.POSITIVE_INFINITY)}; however, a {@code nextUp} 2523 * implementation may run faster than its equivalent 2524 * {@code nextAfter} call. 2525 * 2526 * <p>Special Cases: 2527 * <ul> 2528 * <li> If the argument is NaN, the result is NaN. 2529 * 2530 * <li> If the argument is positive infinity, the result is 2531 * positive infinity. 2532 * 2533 * <li> If the argument is zero, the result is 2534 * {@link Float#MIN_VALUE} 2535 * 2536 * </ul> 2537 * 2538 * @param f starting floating-point value 2539 * @return The adjacent floating-point value closer to positive 2540 * infinity. 2541 * @since 1.6 2542 */ nextUp(float f)2543 public static float nextUp(float f) { 2544 return Math.nextUp(f); 2545 } 2546 2547 /** 2548 * Returns the floating-point value adjacent to {@code d} in 2549 * the direction of negative infinity. This method is 2550 * semantically equivalent to {@code nextAfter(d, 2551 * Double.NEGATIVE_INFINITY)}; however, a 2552 * {@code nextDown} implementation may run faster than its 2553 * equivalent {@code nextAfter} call. 2554 * 2555 * <p>Special Cases: 2556 * <ul> 2557 * <li> If the argument is NaN, the result is NaN. 2558 * 2559 * <li> If the argument is negative infinity, the result is 2560 * negative infinity. 2561 * 2562 * <li> If the argument is zero, the result is 2563 * {@code -Double.MIN_VALUE} 2564 * 2565 * </ul> 2566 * 2567 * @param d starting floating-point value 2568 * @return The adjacent floating-point value closer to negative 2569 * infinity. 2570 * @since 1.8 2571 */ nextDown(double d)2572 public static double nextDown(double d) { 2573 return Math.nextDown(d); 2574 } 2575 2576 /** 2577 * Returns the floating-point value adjacent to {@code f} in 2578 * the direction of negative infinity. This method is 2579 * semantically equivalent to {@code nextAfter(f, 2580 * Float.NEGATIVE_INFINITY)}; however, a 2581 * {@code nextDown} implementation may run faster than its 2582 * equivalent {@code nextAfter} call. 2583 * 2584 * <p>Special Cases: 2585 * <ul> 2586 * <li> If the argument is NaN, the result is NaN. 2587 * 2588 * <li> If the argument is negative infinity, the result is 2589 * negative infinity. 2590 * 2591 * <li> If the argument is zero, the result is 2592 * {@code -Float.MIN_VALUE} 2593 * 2594 * </ul> 2595 * 2596 * @param f starting floating-point value 2597 * @return The adjacent floating-point value closer to negative 2598 * infinity. 2599 * @since 1.8 2600 */ nextDown(float f)2601 public static float nextDown(float f) { 2602 return Math.nextDown(f); 2603 } 2604 2605 /** 2606 * Returns {@code d} × 2<sup>{@code scaleFactor}</sup> 2607 * rounded as if performed by a single correctly rounded 2608 * floating-point multiply. If the exponent of the result is 2609 * between {@link Double#MIN_EXPONENT} and {@link 2610 * Double#MAX_EXPONENT}, the answer is calculated exactly. If the 2611 * exponent of the result would be larger than {@code 2612 * Double.MAX_EXPONENT}, an infinity is returned. Note that if 2613 * the result is subnormal, precision may be lost; that is, when 2614 * {@code scalb(x, n)} is subnormal, {@code scalb(scalb(x, n), 2615 * -n)} may not equal <i>x</i>. When the result is non-NaN, the 2616 * result has the same sign as {@code d}. 2617 * 2618 * <p>Special cases: 2619 * <ul> 2620 * <li> If the first argument is NaN, NaN is returned. 2621 * <li> If the first argument is infinite, then an infinity of the 2622 * same sign is returned. 2623 * <li> If the first argument is zero, then a zero of the same 2624 * sign is returned. 2625 * </ul> 2626 * 2627 * @param d number to be scaled by a power of two. 2628 * @param scaleFactor power of 2 used to scale {@code d} 2629 * @return {@code d} × 2<sup>{@code scaleFactor}</sup> 2630 * @since 1.6 2631 */ scalb(double d, int scaleFactor)2632 public static double scalb(double d, int scaleFactor) { 2633 return Math.scalb(d, scaleFactor); 2634 } 2635 2636 /** 2637 * Returns {@code f} × 2<sup>{@code scaleFactor}</sup> 2638 * rounded as if performed by a single correctly rounded 2639 * floating-point multiply. If the exponent of the result is 2640 * between {@link Float#MIN_EXPONENT} and {@link 2641 * Float#MAX_EXPONENT}, the answer is calculated exactly. If the 2642 * exponent of the result would be larger than {@code 2643 * Float.MAX_EXPONENT}, an infinity is returned. Note that if the 2644 * result is subnormal, precision may be lost; that is, when 2645 * {@code scalb(x, n)} is subnormal, {@code scalb(scalb(x, n), 2646 * -n)} may not equal <i>x</i>. When the result is non-NaN, the 2647 * result has the same sign as {@code f}. 2648 * 2649 * <p>Special cases: 2650 * <ul> 2651 * <li> If the first argument is NaN, NaN is returned. 2652 * <li> If the first argument is infinite, then an infinity of the 2653 * same sign is returned. 2654 * <li> If the first argument is zero, then a zero of the same 2655 * sign is returned. 2656 * </ul> 2657 * 2658 * @param f number to be scaled by a power of two. 2659 * @param scaleFactor power of 2 used to scale {@code f} 2660 * @return {@code f} × 2<sup>{@code scaleFactor}</sup> 2661 * @since 1.6 2662 */ scalb(float f, int scaleFactor)2663 public static float scalb(float f, int scaleFactor) { 2664 return Math.scalb(f, scaleFactor); 2665 } 2666 } 2667