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