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