1 /* 2 * Copyright (c) 1999, 2011, 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 import java.util.Random; 28 import sun.misc.FpUtils; 29 import sun.misc.DoubleConsts; 30 31 /** 32 * The class {@code StrictMath} contains methods for performing basic 33 * numeric operations such as the elementary exponential, logarithm, 34 * square root, and trigonometric functions. 35 * 36 * <p>To help ensure portability of Java programs, the definitions of 37 * some of the numeric functions in this package require that they 38 * produce the same results as certain published algorithms. These 39 * algorithms are available from the well-known network library 40 * {@code netlib} as the package "Freely Distributable Math 41 * Library," <a 42 * href="ftp://ftp.netlib.org/fdlibm.tar">{@code fdlibm}</a>. These 43 * algorithms, which are written in the C programming language, are 44 * then to be understood as executed with all floating-point 45 * operations following the rules of Java floating-point arithmetic. 46 * 47 * <p>The Java math library is defined with respect to 48 * {@code fdlibm} version 5.3. Where {@code fdlibm} provides 49 * more than one definition for a function (such as 50 * {@code acos}), use the "IEEE 754 core function" version 51 * (residing in a file whose name begins with the letter 52 * {@code e}). The methods which require {@code fdlibm} 53 * semantics are {@code sin}, {@code cos}, {@code tan}, 54 * {@code asin}, {@code acos}, {@code atan}, 55 * {@code exp}, {@code log}, {@code log10}, 56 * {@code cbrt}, {@code atan2}, {@code pow}, 57 * {@code sinh}, {@code cosh}, {@code tanh}, 58 * {@code hypot}, {@code expm1}, and {@code log1p}. 59 * 60 * @author unascribed 61 * @author Joseph D. Darcy 62 * @since 1.3 63 */ 64 65 public final class StrictMath { 66 67 /** 68 * Don't let anyone instantiate this class. 69 */ 70 private StrictMath() {} 71 72 /** 73 * The {@code double} value that is closer than any other to 74 * <i>e</i>, the base of the natural logarithms. 75 */ 76 public static final double E = 2.7182818284590452354; 77 78 /** 79 * The {@code double} value that is closer than any other to 80 * <i>pi</i>, the ratio of the circumference of a circle to its 81 * diameter. 82 */ 83 public static final double PI = 3.14159265358979323846; 84 85 /** 86 * Returns the trigonometric sine of an angle. Special cases: 87 * <ul><li>If the argument is NaN or an infinity, then the 88 * result is NaN. 89 * <li>If the argument is zero, then the result is a zero with the 90 * same sign as the argument.</ul> 91 * 92 * @param a an angle, in radians. 93 * @return the sine of the argument. 94 */ 95 public static native double sin(double a); 96 97 /** 98 * Returns the trigonometric cosine of an angle. Special cases: 99 * <ul><li>If the argument is NaN or an infinity, then the 100 * result is NaN.</ul> 101 * 102 * @param a an angle, in radians. 103 * @return the cosine of the argument. 104 */ 105 public static native double cos(double a); 106 107 /** 108 * Returns the trigonometric tangent of an angle. Special cases: 109 * <ul><li>If the argument is NaN or an infinity, then the result 110 * is NaN. 111 * <li>If the argument is zero, then the result is a zero with the 112 * same sign as the argument.</ul> 113 * 114 * @param a an angle, in radians. 115 * @return the tangent of the argument. 116 */ 117 public static native double tan(double a); 118 119 /** 120 * Returns the arc sine of a value; the returned angle is in the 121 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases: 122 * <ul><li>If the argument is NaN or its absolute value is greater 123 * than 1, then the result is NaN. 124 * <li>If the argument is zero, then the result is a zero with the 125 * same sign as the argument.</ul> 126 * 127 * @param a the value whose arc sine is to be returned. 128 * @return the arc sine of the argument. 129 */ 130 public static native double asin(double a); 131 132 /** 133 * Returns the arc cosine of a value; the returned angle is in the 134 * range 0.0 through <i>pi</i>. Special case: 135 * <ul><li>If the argument is NaN or its absolute value is greater 136 * than 1, then the result is NaN.</ul> 137 * 138 * @param a the value whose arc cosine is to be returned. 139 * @return the arc cosine of the argument. 140 */ 141 public static native double acos(double a); 142 143 /** 144 * Returns the arc tangent of a value; the returned angle is in the 145 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases: 146 * <ul><li>If the argument is NaN, then the result is NaN. 147 * <li>If the argument is zero, then the result is a zero with the 148 * same sign as the argument.</ul> 149 * 150 * @param a the value whose arc tangent is to be returned. 151 * @return the arc tangent of the argument. 152 */ 153 public static native double atan(double a); 154 155 /** 156 * Converts an angle measured in degrees to an approximately 157 * equivalent angle measured in radians. The conversion from 158 * degrees to radians is generally inexact. 159 * 160 * @param angdeg an angle, in degrees 161 * @return the measurement of the angle {@code angdeg} 162 * in radians. 163 */ 164 public static strictfp double toRadians(double angdeg) { 165 return angdeg / 180.0 * PI; 166 } 167 168 /** 169 * Converts an angle measured in radians to an approximately 170 * equivalent angle measured in degrees. The conversion from 171 * radians to degrees is generally inexact; users should 172 * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly 173 * equal {@code 0.0}. 174 * 175 * @param angrad an angle, in radians 176 * @return the measurement of the angle {@code angrad} 177 * in degrees. 178 */ 179 public static strictfp double toDegrees(double angrad) { 180 return angrad * 180.0 / PI; 181 } 182 183 /** 184 * Returns Euler's number <i>e</i> raised to the power of a 185 * {@code double} value. Special cases: 186 * <ul><li>If the argument is NaN, the result is NaN. 187 * <li>If the argument is positive infinity, then the result is 188 * positive infinity. 189 * <li>If the argument is negative infinity, then the result is 190 * positive zero.</ul> 191 * 192 * @param a the exponent to raise <i>e</i> to. 193 * @return the value <i>e</i><sup>{@code a}</sup>, 194 * where <i>e</i> is the base of the natural logarithms. 195 */ 196 public static native double exp(double a); 197 198 /** 199 * Returns the natural logarithm (base <i>e</i>) of a {@code double} 200 * value. Special cases: 201 * <ul><li>If the argument is NaN or less than zero, then the result 202 * is NaN. 203 * <li>If the argument is positive infinity, then the result is 204 * positive infinity. 205 * <li>If the argument is positive zero or negative zero, then the 206 * result is negative infinity.</ul> 207 * 208 * @param a a value 209 * @return the value ln {@code a}, the natural logarithm of 210 * {@code a}. 211 */ 212 public static native double log(double a); 213 214 215 /** 216 * Returns the base 10 logarithm of a {@code double} value. 217 * Special cases: 218 * 219 * <ul><li>If the argument is NaN or less than zero, then the result 220 * is NaN. 221 * <li>If the argument is positive infinity, then the result is 222 * positive infinity. 223 * <li>If the argument is positive zero or negative zero, then the 224 * result is negative infinity. 225 * <li> If the argument is equal to 10<sup><i>n</i></sup> for 226 * integer <i>n</i>, then the result is <i>n</i>. 227 * </ul> 228 * 229 * @param a a value 230 * @return the base 10 logarithm of {@code a}. 231 * @since 1.5 232 */ 233 public static native double log10(double a); 234 235 /** 236 * Returns the correctly rounded positive square root of a 237 * {@code double} value. 238 * Special cases: 239 * <ul><li>If the argument is NaN or less than zero, then the result 240 * is NaN. 241 * <li>If the argument is positive infinity, then the result is positive 242 * infinity. 243 * <li>If the argument is positive zero or negative zero, then the 244 * result is the same as the argument.</ul> 245 * Otherwise, the result is the {@code double} value closest to 246 * the true mathematical square root of the argument value. 247 * 248 * @param a a value. 249 * @return the positive square root of {@code a}. 250 */ 251 public static native double sqrt(double a); 252 253 /** 254 * Returns the cube root of a {@code double} value. For 255 * positive finite {@code x}, {@code cbrt(-x) == 256 * -cbrt(x)}; that is, the cube root of a negative value is 257 * the negative of the cube root of that value's magnitude. 258 * Special cases: 259 * 260 * <ul> 261 * 262 * <li>If the argument is NaN, then the result is NaN. 263 * 264 * <li>If the argument is infinite, then the result is an infinity 265 * with the same sign as the argument. 266 * 267 * <li>If the argument is zero, then the result is a zero with the 268 * same sign as the argument. 269 * 270 * </ul> 271 * 272 * @param a a value. 273 * @return the cube root of {@code a}. 274 * @since 1.5 275 */ 276 public static native double cbrt(double a); 277 278 /** 279 * Computes the remainder operation on two arguments as prescribed 280 * by the IEEE 754 standard. 281 * The remainder value is mathematically equal to 282 * <code>f1 - f2</code> × <i>n</i>, 283 * where <i>n</i> is the mathematical integer closest to the exact 284 * mathematical value of the quotient {@code f1/f2}, and if two 285 * mathematical integers are equally close to {@code f1/f2}, 286 * then <i>n</i> is the integer that is even. If the remainder is 287 * zero, its sign is the same as the sign of the first argument. 288 * Special cases: 289 * <ul><li>If either argument is NaN, or the first argument is infinite, 290 * or the second argument is positive zero or negative zero, then the 291 * result is NaN. 292 * <li>If the first argument is finite and the second argument is 293 * infinite, then the result is the same as the first argument.</ul> 294 * 295 * @param f1 the dividend. 296 * @param f2 the divisor. 297 * @return the remainder when {@code f1} is divided by 298 * {@code f2}. 299 */ 300 public static native double IEEEremainder(double f1, double f2); 301 302 /** 303 * Returns the smallest (closest to negative infinity) 304 * {@code double} value that is greater than or equal to the 305 * argument and is equal to a mathematical integer. Special cases: 306 * <ul><li>If the argument value is already equal to a 307 * mathematical integer, then the result is the same as the 308 * argument. <li>If the argument is NaN or an infinity or 309 * positive zero or negative zero, then the result is the same as 310 * the argument. <li>If the argument value is less than zero but 311 * greater than -1.0, then the result is negative zero.</ul> Note 312 * that the value of {@code StrictMath.ceil(x)} is exactly the 313 * value of {@code -StrictMath.floor(-x)}. 314 * 315 * @param a a value. 316 * @return the smallest (closest to negative infinity) 317 * floating-point value that is greater than or equal to 318 * the argument and is equal to a mathematical integer. 319 */ 320 public static double ceil(double a) { 321 return floorOrCeil(a, -0.0, 1.0, 1.0); 322 } 323 324 /** 325 * Returns the largest (closest to positive infinity) 326 * {@code double} value that is less than or equal to the 327 * argument and is equal to a mathematical integer. Special cases: 328 * <ul><li>If the argument value is already equal to a 329 * mathematical integer, then the result is the same as the 330 * argument. <li>If the argument is NaN or an infinity or 331 * positive zero or negative zero, then the result is the same as 332 * the argument.</ul> 333 * 334 * @param a a value. 335 * @return the largest (closest to positive infinity) 336 * floating-point value that less than or equal to the argument 337 * and is equal to a mathematical integer. 338 */ 339 public static double floor(double a) { 340 return floorOrCeil(a, -1.0, 0.0, -1.0); 341 } 342 343 /** 344 * Internal method to share logic between floor and ceil. 345 * 346 * @param a the value to be floored or ceiled 347 * @param negativeBoundary result for values in (-1, 0) 348 * @param positiveBoundary result for values in (0, 1) 349 * @param increment value to add when the argument is non-integral 350 */ 351 private static double floorOrCeil(double a, 352 double negativeBoundary, 353 double positiveBoundary, 354 double sign) { 355 int exponent = Math.getExponent(a); 356 357 if (exponent < 0) { 358 /* 359 * Absolute value of argument is less than 1. 360 * floorOrceil(-0.0) => -0.0 361 * floorOrceil(+0.0) => +0.0 362 */ 363 return ((a == 0.0) ? a : 364 ( (a < 0.0) ? negativeBoundary : positiveBoundary) ); 365 } else if (exponent >= 52) { 366 /* 367 * Infinity, NaN, or a value so large it must be integral. 368 */ 369 return a; 370 } 371 // Else the argument is either an integral value already XOR it 372 // has to be rounded to one. 373 assert exponent >= 0 && exponent <= 51; 374 375 long doppel = Double.doubleToRawLongBits(a); 376 long mask = DoubleConsts.SIGNIF_BIT_MASK >> exponent; 377 378 if ( (mask & doppel) == 0L ) 379 return a; // integral value 380 else { 381 double result = Double.longBitsToDouble(doppel & (~mask)); 382 if (sign*a > 0.0) 383 result = result + sign; 384 return result; 385 } 386 } 387 388 /** 389 * Returns the {@code double} value that is closest in value 390 * to the argument and is equal to a mathematical integer. If two 391 * {@code double} values that are mathematical integers are 392 * equally close to the value of the argument, the result is the 393 * integer value that is even. Special cases: 394 * <ul><li>If the argument value is already equal to a mathematical 395 * integer, then the result is the same as the argument. 396 * <li>If the argument is NaN or an infinity or positive zero or negative 397 * zero, then the result is the same as the argument.</ul> 398 * 399 * @param a a value. 400 * @return the closest floating-point value to {@code a} that is 401 * equal to a mathematical integer. 402 * @author Joseph D. Darcy 403 */ 404 public static double rint(double a) { 405 /* 406 * If the absolute value of a is not less than 2^52, it 407 * is either a finite integer (the double format does not have 408 * enough significand bits for a number that large to have any 409 * fractional portion), an infinity, or a NaN. In any of 410 * these cases, rint of the argument is the argument. 411 * 412 * Otherwise, the sum (twoToThe52 + a ) will properly round 413 * away any fractional portion of a since ulp(twoToThe52) == 414 * 1.0; subtracting out twoToThe52 from this sum will then be 415 * exact and leave the rounded integer portion of a. 416 * 417 * This method does *not* need to be declared strictfp to get 418 * fully reproducible results. Whether or not a method is 419 * declared strictfp can only make a difference in the 420 * returned result if some operation would overflow or 421 * underflow with strictfp semantics. The operation 422 * (twoToThe52 + a ) cannot overflow since large values of a 423 * are screened out; the add cannot underflow since twoToThe52 424 * is too large. The subtraction ((twoToThe52 + a ) - 425 * twoToThe52) will be exact as discussed above and thus 426 * cannot overflow or meaningfully underflow. Finally, the 427 * last multiply in the return statement is by plus or minus 428 * 1.0, which is exact too. 429 */ 430 double twoToThe52 = (double)(1L << 52); // 2^52 431 double sign = FpUtils.rawCopySign(1.0, a); // preserve sign info 432 a = Math.abs(a); 433 434 if (a < twoToThe52) { // E_min <= ilogb(a) <= 51 435 a = ((twoToThe52 + a ) - twoToThe52); 436 } 437 438 return sign * a; // restore original sign 439 } 440 441 /** 442 * Returns the angle <i>theta</i> from the conversion of rectangular 443 * coordinates ({@code x}, {@code y}) to polar 444 * coordinates (r, <i>theta</i>). 445 * This method computes the phase <i>theta</i> by computing an arc tangent 446 * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special 447 * cases: 448 * <ul><li>If either argument is NaN, then the result is NaN. 449 * <li>If the first argument is positive zero and the second argument 450 * is positive, or the first argument is positive and finite and the 451 * second argument is positive infinity, then the result is positive 452 * zero. 453 * <li>If the first argument is negative zero and the second argument 454 * is positive, or the first argument is negative and finite and the 455 * second argument is positive infinity, then the result is negative zero. 456 * <li>If the first argument is positive zero and the second argument 457 * is negative, or the first argument is positive and finite and the 458 * second argument is negative infinity, then the result is the 459 * {@code double} value closest to <i>pi</i>. 460 * <li>If the first argument is negative zero and the second argument 461 * is negative, or the first argument is negative and finite and the 462 * second argument is negative infinity, then the result is the 463 * {@code double} value closest to -<i>pi</i>. 464 * <li>If the first argument is positive and the second argument is 465 * positive zero or negative zero, or the first argument is positive 466 * infinity and the second argument is finite, then the result is the 467 * {@code double} value closest to <i>pi</i>/2. 468 * <li>If the first argument is negative and the second argument is 469 * positive zero or negative zero, or the first argument is negative 470 * infinity and the second argument is finite, then the result is the 471 * {@code double} value closest to -<i>pi</i>/2. 472 * <li>If both arguments are positive infinity, then the result is the 473 * {@code double} value closest to <i>pi</i>/4. 474 * <li>If the first argument is positive infinity and the second argument 475 * is negative infinity, then the result is the {@code double} 476 * value closest to 3*<i>pi</i>/4. 477 * <li>If the first argument is negative infinity and the second argument 478 * is positive infinity, then the result is the {@code double} value 479 * closest to -<i>pi</i>/4. 480 * <li>If both arguments are negative infinity, then the result is the 481 * {@code double} value closest to -3*<i>pi</i>/4.</ul> 482 * 483 * @param y the ordinate coordinate 484 * @param x the abscissa coordinate 485 * @return the <i>theta</i> component of the point 486 * (<i>r</i>, <i>theta</i>) 487 * in polar coordinates that corresponds to the point 488 * (<i>x</i>, <i>y</i>) in Cartesian coordinates. 489 */ 490 public static native double atan2(double y, double x); 491 492 493 /** 494 * Returns the value of the first argument raised to the power of the 495 * second argument. Special cases: 496 * 497 * <ul><li>If the second argument is positive or negative zero, then the 498 * result is 1.0. 499 * <li>If the second argument is 1.0, then the result is the same as the 500 * first argument. 501 * <li>If the second argument is NaN, then the result is NaN. 502 * <li>If the first argument is NaN and the second argument is nonzero, 503 * then the result is NaN. 504 * 505 * <li>If 506 * <ul> 507 * <li>the absolute value of the first argument is greater than 1 508 * and the second argument is positive infinity, or 509 * <li>the absolute value of the first argument is less than 1 and 510 * the second argument is negative infinity, 511 * </ul> 512 * then the result is positive infinity. 513 * 514 * <li>If 515 * <ul> 516 * <li>the absolute value of the first argument is greater than 1 and 517 * the second argument is negative infinity, or 518 * <li>the absolute value of the 519 * first argument is less than 1 and the second argument is positive 520 * infinity, 521 * </ul> 522 * then the result is positive zero. 523 * 524 * <li>If the absolute value of the first argument equals 1 and the 525 * second argument is infinite, then the result is NaN. 526 * 527 * <li>If 528 * <ul> 529 * <li>the first argument is positive zero and the second argument 530 * is greater than zero, or 531 * <li>the first argument is positive infinity and the second 532 * argument is less than zero, 533 * </ul> 534 * then the result is positive zero. 535 * 536 * <li>If 537 * <ul> 538 * <li>the first argument is positive zero and the second argument 539 * is less than zero, or 540 * <li>the first argument is positive infinity and the second 541 * argument is greater than zero, 542 * </ul> 543 * then the result is positive infinity. 544 * 545 * <li>If 546 * <ul> 547 * <li>the first argument is negative zero and the second argument 548 * is greater than zero but not a finite odd integer, or 549 * <li>the first argument is negative infinity and the second 550 * argument is less than zero but not a finite odd integer, 551 * </ul> 552 * then the result is positive zero. 553 * 554 * <li>If 555 * <ul> 556 * <li>the first argument is negative zero and the second argument 557 * is a positive finite odd integer, or 558 * <li>the first argument is negative infinity and the second 559 * argument is a negative finite odd integer, 560 * </ul> 561 * then the result is negative zero. 562 * 563 * <li>If 564 * <ul> 565 * <li>the first argument is negative zero and the second argument 566 * is less than zero but not a finite odd integer, or 567 * <li>the first argument is negative infinity and the second 568 * argument is greater than zero but not a finite odd integer, 569 * </ul> 570 * then the result is positive infinity. 571 * 572 * <li>If 573 * <ul> 574 * <li>the first argument is negative zero and the second argument 575 * is a negative finite odd integer, or 576 * <li>the first argument is negative infinity and the second 577 * argument is a positive finite odd integer, 578 * </ul> 579 * then the result is negative infinity. 580 * 581 * <li>If the first argument is finite and less than zero 582 * <ul> 583 * <li> if the second argument is a finite even integer, the 584 * result is equal to the result of raising the absolute value of 585 * the first argument to the power of the second argument 586 * 587 * <li>if the second argument is a finite odd integer, the result 588 * is equal to the negative of the result of raising the absolute 589 * value of the first argument to the power of the second 590 * argument 591 * 592 * <li>if the second argument is finite and not an integer, then 593 * the result is NaN. 594 * </ul> 595 * 596 * <li>If both arguments are integers, then the result is exactly equal 597 * to the mathematical result of raising the first argument to the power 598 * of the second argument if that result can in fact be represented 599 * exactly as a {@code double} value.</ul> 600 * 601 * <p>(In the foregoing descriptions, a floating-point value is 602 * considered to be an integer if and only if it is finite and a 603 * fixed point of the method {@link #ceil ceil} or, 604 * equivalently, a fixed point of the method {@link #floor 605 * floor}. A value is a fixed point of a one-argument 606 * method if and only if the result of applying the method to the 607 * value is equal to the value.) 608 * 609 * @param a base. 610 * @param b the exponent. 611 * @return the value {@code a}<sup>{@code b}</sup>. 612 */ 613 public static native double pow(double a, double b); 614 615 /** 616 * Returns the closest {@code int} to the argument, with ties 617 * rounding up. 618 * 619 * <p>Special cases: 620 * <ul><li>If the argument is NaN, the result is 0. 621 * <li>If the argument is negative infinity or any value less than or 622 * equal to the value of {@code Integer.MIN_VALUE}, the result is 623 * equal to the value of {@code Integer.MIN_VALUE}. 624 * <li>If the argument is positive infinity or any value greater than or 625 * equal to the value of {@code Integer.MAX_VALUE}, the result is 626 * equal to the value of {@code Integer.MAX_VALUE}.</ul> 627 * 628 * @param a a floating-point value to be rounded to an integer. 629 * @return the value of the argument rounded to the nearest 630 * {@code int} value. 631 * @see java.lang.Integer#MAX_VALUE 632 * @see java.lang.Integer#MIN_VALUE 633 */ 634 public static int round(float a) { 635 return Math.round(a); 636 } 637 638 /** 639 * Returns the closest {@code long} to the argument, with ties 640 * rounding up. 641 * 642 * <p>Special cases: 643 * <ul><li>If the argument is NaN, the result is 0. 644 * <li>If the argument is negative infinity or any value less than or 645 * equal to the value of {@code Long.MIN_VALUE}, the result is 646 * equal to the value of {@code Long.MIN_VALUE}. 647 * <li>If the argument is positive infinity or any value greater than or 648 * equal to the value of {@code Long.MAX_VALUE}, the result is 649 * equal to the value of {@code Long.MAX_VALUE}.</ul> 650 * 651 * @param a a floating-point value to be rounded to a 652 * {@code long}. 653 * @return the value of the argument rounded to the nearest 654 * {@code long} value. 655 * @see java.lang.Long#MAX_VALUE 656 * @see java.lang.Long#MIN_VALUE 657 */ 658 public static long round(double a) { 659 return Math.round(a); 660 } 661 662 private static Random randomNumberGenerator; 663 664 private static synchronized Random initRNG() { 665 Random rnd = randomNumberGenerator; 666 return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd; 667 } 668 669 /** 670 * Returns a {@code double} value with a positive sign, greater 671 * than or equal to {@code 0.0} and less than {@code 1.0}. 672 * Returned values are chosen pseudorandomly with (approximately) 673 * uniform distribution from that range. 674 * 675 * <p>When this method is first called, it creates a single new 676 * pseudorandom-number generator, exactly as if by the expression 677 * 678 * <blockquote>{@code new java.util.Random()}</blockquote> 679 * 680 * This new pseudorandom-number generator is used thereafter for 681 * all calls to this method and is used nowhere else. 682 * 683 * <p>This method is properly synchronized to allow correct use by 684 * more than one thread. However, if many threads need to generate 685 * pseudorandom numbers at a great rate, it may reduce contention 686 * for each thread to have its own pseudorandom number generator. 687 * 688 * @return a pseudorandom {@code double} greater than or equal 689 * to {@code 0.0} and less than {@code 1.0}. 690 * @see Random#nextDouble() 691 */ 692 public static double random() { 693 Random rnd = randomNumberGenerator; 694 if (rnd == null) rnd = initRNG(); 695 return rnd.nextDouble(); 696 } 697 698 /** 699 * Returns the absolute value of an {@code int} value.. 700 * If the argument is not negative, the argument is returned. 701 * If the argument is negative, the negation of the argument is returned. 702 * 703 * <p>Note that if the argument is equal to the value of 704 * {@link Integer#MIN_VALUE}, the most negative representable 705 * {@code int} value, the result is that same value, which is 706 * negative. 707 * 708 * @param a the argument whose absolute value is to be determined. 709 * @return the absolute value of the argument. 710 */ 711 public static int abs(int a) { 712 return (a < 0) ? -a : a; 713 } 714 715 /** 716 * Returns the absolute value of a {@code long} value. 717 * If the argument is not negative, the argument is returned. 718 * If the argument is negative, the negation of the argument is returned. 719 * 720 * <p>Note that if the argument is equal to the value of 721 * {@link Long#MIN_VALUE}, the most negative representable 722 * {@code long} value, the result is that same value, which 723 * is negative. 724 * 725 * @param a the argument whose absolute value is to be determined. 726 * @return the absolute value of the argument. 727 */ 728 public static long abs(long a) { 729 return (a < 0) ? -a : a; 730 } 731 732 /** 733 * Returns the absolute value of a {@code float} value. 734 * If the argument is not negative, the argument is returned. 735 * If the argument is negative, the negation of the argument is returned. 736 * Special cases: 737 * <ul><li>If the argument is positive zero or negative zero, the 738 * result is positive zero. 739 * <li>If the argument is infinite, the result is positive infinity. 740 * <li>If the argument is NaN, the result is NaN.</ul> 741 * In other words, the result is the same as the value of the expression: 742 * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))} 743 * 744 * @param a the argument whose absolute value is to be determined 745 * @return the absolute value of the argument. 746 */ 747 public static float abs(float a) { 748 return (a <= 0.0F) ? 0.0F - a : a; 749 } 750 751 /** 752 * Returns the absolute value of a {@code double} value. 753 * If the argument is not negative, the argument is returned. 754 * If the argument is negative, the negation of the argument is returned. 755 * Special cases: 756 * <ul><li>If the argument is positive zero or negative zero, the result 757 * is positive zero. 758 * <li>If the argument is infinite, the result is positive infinity. 759 * <li>If the argument is NaN, the result is NaN.</ul> 760 * In other words, the result is the same as the value of the expression: 761 * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)} 762 * 763 * @param a the argument whose absolute value is to be determined 764 * @return the absolute value of the argument. 765 */ 766 public static double abs(double a) { 767 return (a <= 0.0D) ? 0.0D - a : a; 768 } 769 770 /** 771 * Returns the greater of two {@code int} values. That is, the 772 * result is the argument closer to the value of 773 * {@link Integer#MAX_VALUE}. If the arguments have the same value, 774 * the result is that same value. 775 * 776 * @param a an argument. 777 * @param b another argument. 778 * @return the larger of {@code a} and {@code b}. 779 */ 780 public static int max(int a, int b) { 781 return (a >= b) ? a : b; 782 } 783 784 /** 785 * Returns the greater of two {@code long} values. That is, the 786 * result is the argument closer to the value of 787 * {@link Long#MAX_VALUE}. If the arguments have the same value, 788 * the result is that same value. 789 * 790 * @param a an argument. 791 * @param b another argument. 792 * @return the larger of {@code a} and {@code b}. 793 */ 794 public static long max(long a, long b) { 795 return (a >= b) ? a : b; 796 } 797 798 // Use raw bit-wise conversions on guaranteed non-NaN arguments. 799 private static long negativeZeroFloatBits = Float.floatToRawIntBits(-0.0f); 800 private static long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d); 801 802 /** 803 * Returns the greater of two {@code float} values. That is, 804 * the result is the argument closer to positive infinity. If the 805 * arguments have the same value, the result is that same 806 * value. If either value is NaN, then the result is NaN. Unlike 807 * the numerical comparison operators, this method considers 808 * negative zero to be strictly smaller than positive zero. If one 809 * argument is positive zero and the other negative zero, the 810 * result is positive zero. 811 * 812 * @param a an argument. 813 * @param b another argument. 814 * @return the larger of {@code a} and {@code b}. 815 */ 816 public static float max(float a, float b) { 817 if (a != a) 818 return a; // a is NaN 819 if ((a == 0.0f) && 820 (b == 0.0f) && 821 (Float.floatToRawIntBits(a) == negativeZeroFloatBits)) { 822 // Raw conversion ok since NaN can't map to -0.0. 823 return b; 824 } 825 return (a >= b) ? a : b; 826 } 827 828 /** 829 * Returns the greater of two {@code double} values. That 830 * is, the result is the argument closer to positive infinity. If 831 * the arguments have the same value, the result is that same 832 * value. If either value is NaN, then the result is NaN. Unlike 833 * the numerical comparison operators, this method considers 834 * negative zero to be strictly smaller than positive zero. If one 835 * argument is positive zero and the other negative zero, the 836 * result is positive zero. 837 * 838 * @param a an argument. 839 * @param b another argument. 840 * @return the larger of {@code a} and {@code b}. 841 */ 842 public static double max(double a, double b) { 843 if (a != a) 844 return a; // a is NaN 845 if ((a == 0.0d) && 846 (b == 0.0d) && 847 (Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) { 848 // Raw conversion ok since NaN can't map to -0.0. 849 return b; 850 } 851 return (a >= b) ? a : b; 852 } 853 854 /** 855 * Returns the smaller of two {@code int} values. That is, 856 * the result the argument closer to the value of 857 * {@link Integer#MIN_VALUE}. If the arguments have the same 858 * value, the result is that same value. 859 * 860 * @param a an argument. 861 * @param b another argument. 862 * @return the smaller of {@code a} and {@code b}. 863 */ 864 public static int min(int a, int b) { 865 return (a <= b) ? a : b; 866 } 867 868 /** 869 * Returns the smaller of two {@code long} values. That is, 870 * the result is the argument closer to the value of 871 * {@link Long#MIN_VALUE}. If the arguments have the same 872 * value, the result is that same value. 873 * 874 * @param a an argument. 875 * @param b another argument. 876 * @return the smaller of {@code a} and {@code b}. 877 */ 878 public static long min(long a, long b) { 879 return (a <= b) ? a : b; 880 } 881 882 /** 883 * Returns the smaller of two {@code float} values. That is, 884 * the result is the value closer to negative infinity. If the 885 * arguments have the same value, the result is that same 886 * value. If either value is NaN, then the result is NaN. Unlike 887 * the numerical comparison operators, this method considers 888 * negative zero to be strictly smaller than positive zero. If 889 * one argument is positive zero and the other is negative zero, 890 * the result is negative zero. 891 * 892 * @param a an argument. 893 * @param b another argument. 894 * @return the smaller of {@code a} and {@code b.} 895 */ 896 public static float min(float a, float b) { 897 if (a != a) 898 return a; // a is NaN 899 if ((a == 0.0f) && 900 (b == 0.0f) && 901 (Float.floatToRawIntBits(b) == negativeZeroFloatBits)) { 902 // Raw conversion ok since NaN can't map to -0.0. 903 return b; 904 } 905 return (a <= b) ? a : b; 906 } 907 908 /** 909 * Returns the smaller of two {@code double} values. That 910 * is, the result is the value closer to negative infinity. If the 911 * arguments have the same value, the result is that same 912 * value. If either value is NaN, then the result is NaN. Unlike 913 * the numerical comparison operators, this method considers 914 * negative zero to be strictly smaller than positive zero. If one 915 * argument is positive zero and the other is negative zero, the 916 * result is negative zero. 917 * 918 * @param a an argument. 919 * @param b another argument. 920 * @return the smaller of {@code a} and {@code b}. 921 */ 922 public static double min(double a, double b) { 923 if (a != a) 924 return a; // a is NaN 925 if ((a == 0.0d) && 926 (b == 0.0d) && 927 (Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) { 928 // Raw conversion ok since NaN can't map to -0.0. 929 return b; 930 } 931 return (a <= b) ? a : b; 932 } 933 934 /** 935 * Returns the size of an ulp of the argument. An ulp, unit in 936 * the last place, of a {@code double} value is the positive 937 * distance between this floating-point value and the {@code 938 * double} value next larger in magnitude. Note that for non-NaN 939 * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 940 * 941 * <p>Special Cases: 942 * <ul> 943 * <li> If the argument is NaN, then the result is NaN. 944 * <li> If the argument is positive or negative infinity, then the 945 * result is positive infinity. 946 * <li> If the argument is positive or negative zero, then the result is 947 * {@code Double.MIN_VALUE}. 948 * <li> If the argument is ±{@code Double.MAX_VALUE}, then 949 * the result is equal to 2<sup>971</sup>. 950 * </ul> 951 * 952 * @param d the floating-point value whose ulp is to be returned 953 * @return the size of an ulp of the argument 954 * @author Joseph D. Darcy 955 * @since 1.5 956 */ 957 public static double ulp(double d) { 958 return sun.misc.FpUtils.ulp(d); 959 } 960 961 /** 962 * Returns the size of an ulp of the argument. An ulp, unit in 963 * the last place, of a {@code float} value is the positive 964 * distance between this floating-point value and the {@code 965 * float} value next larger in magnitude. Note that for non-NaN 966 * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 967 * 968 * <p>Special Cases: 969 * <ul> 970 * <li> If the argument is NaN, then the result is NaN. 971 * <li> If the argument is positive or negative infinity, then the 972 * result is positive infinity. 973 * <li> If the argument is positive or negative zero, then the result is 974 * {@code Float.MIN_VALUE}. 975 * <li> If the argument is ±{@code Float.MAX_VALUE}, then 976 * the result is equal to 2<sup>104</sup>. 977 * </ul> 978 * 979 * @param f the floating-point value whose ulp is to be returned 980 * @return the size of an ulp of the argument 981 * @author Joseph D. Darcy 982 * @since 1.5 983 */ 984 public static float ulp(float f) { 985 return sun.misc.FpUtils.ulp(f); 986 } 987 988 /** 989 * Returns the signum function of the argument; zero if the argument 990 * is zero, 1.0 if the argument is greater than zero, -1.0 if the 991 * argument is less than zero. 992 * 993 * <p>Special Cases: 994 * <ul> 995 * <li> If the argument is NaN, then the result is NaN. 996 * <li> If the argument is positive zero or negative zero, then the 997 * result is the same as the argument. 998 * </ul> 999 * 1000 * @param d the floating-point value whose signum is to be returned 1001 * @return the signum function of the argument 1002 * @author Joseph D. Darcy 1003 * @since 1.5 1004 */ 1005 public static double signum(double d) { 1006 return sun.misc.FpUtils.signum(d); 1007 } 1008 1009 /** 1010 * Returns the signum function of the argument; zero if the argument 1011 * is zero, 1.0f if the argument is greater than zero, -1.0f if the 1012 * argument is less than zero. 1013 * 1014 * <p>Special Cases: 1015 * <ul> 1016 * <li> If the argument is NaN, then the result is NaN. 1017 * <li> If the argument is positive zero or negative zero, then the 1018 * result is the same as the argument. 1019 * </ul> 1020 * 1021 * @param f the floating-point value whose signum is to be returned 1022 * @return the signum function of the argument 1023 * @author Joseph D. Darcy 1024 * @since 1.5 1025 */ 1026 public static float signum(float f) { 1027 return sun.misc.FpUtils.signum(f); 1028 } 1029 1030 /** 1031 * Returns the hyperbolic sine of a {@code double} value. 1032 * The hyperbolic sine of <i>x</i> is defined to be 1033 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2 1034 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1035 * 1036 * <p>Special cases: 1037 * <ul> 1038 * 1039 * <li>If the argument is NaN, then the result is NaN. 1040 * 1041 * <li>If the argument is infinite, then the result is an infinity 1042 * with the same sign as the argument. 1043 * 1044 * <li>If the argument is zero, then the result is a zero with the 1045 * same sign as the argument. 1046 * 1047 * </ul> 1048 * 1049 * @param x The number whose hyperbolic sine is to be returned. 1050 * @return The hyperbolic sine of {@code x}. 1051 * @since 1.5 1052 */ 1053 public static native double sinh(double x); 1054 1055 /** 1056 * Returns the hyperbolic cosine of a {@code double} value. 1057 * The hyperbolic cosine of <i>x</i> is defined to be 1058 * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2 1059 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1060 * 1061 * <p>Special cases: 1062 * <ul> 1063 * 1064 * <li>If the argument is NaN, then the result is NaN. 1065 * 1066 * <li>If the argument is infinite, then the result is positive 1067 * infinity. 1068 * 1069 * <li>If the argument is zero, then the result is {@code 1.0}. 1070 * 1071 * </ul> 1072 * 1073 * @param x The number whose hyperbolic cosine is to be returned. 1074 * @return The hyperbolic cosine of {@code x}. 1075 * @since 1.5 1076 */ 1077 public static native double cosh(double x); 1078 1079 /** 1080 * Returns the hyperbolic tangent of a {@code double} value. 1081 * The hyperbolic tangent of <i>x</i> is defined to be 1082 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>), 1083 * in other words, {@linkplain Math#sinh 1084 * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note 1085 * that the absolute value of the exact tanh is always less than 1086 * 1. 1087 * 1088 * <p>Special cases: 1089 * <ul> 1090 * 1091 * <li>If the argument is NaN, then the result is NaN. 1092 * 1093 * <li>If the argument is zero, then the result is a zero with the 1094 * same sign as the argument. 1095 * 1096 * <li>If the argument is positive infinity, then the result is 1097 * {@code +1.0}. 1098 * 1099 * <li>If the argument is negative infinity, then the result is 1100 * {@code -1.0}. 1101 * 1102 * </ul> 1103 * 1104 * @param x The number whose hyperbolic tangent is to be returned. 1105 * @return The hyperbolic tangent of {@code x}. 1106 * @since 1.5 1107 */ 1108 public static native double tanh(double x); 1109 1110 /** 1111 * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1112 * without intermediate overflow or underflow. 1113 * 1114 * <p>Special cases: 1115 * <ul> 1116 * 1117 * <li> If either argument is infinite, then the result 1118 * is positive infinity. 1119 * 1120 * <li> If either argument is NaN and neither argument is infinite, 1121 * then the result is NaN. 1122 * 1123 * </ul> 1124 * 1125 * @param x a value 1126 * @param y a value 1127 * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1128 * without intermediate overflow or underflow 1129 * @since 1.5 1130 */ 1131 public static native double hypot(double x, double y); 1132 1133 /** 1134 * Returns <i>e</i><sup>x</sup> -1. Note that for values of 1135 * <i>x</i> near 0, the exact sum of 1136 * {@code expm1(x)} + 1 is much closer to the true 1137 * result of <i>e</i><sup>x</sup> than {@code exp(x)}. 1138 * 1139 * <p>Special cases: 1140 * <ul> 1141 * <li>If the argument is NaN, the result is NaN. 1142 * 1143 * <li>If the argument is positive infinity, then the result is 1144 * positive infinity. 1145 * 1146 * <li>If the argument is negative infinity, then the result is 1147 * -1.0. 1148 * 1149 * <li>If the argument is zero, then the result is a zero with the 1150 * same sign as the argument. 1151 * 1152 * </ul> 1153 * 1154 * @param x the exponent to raise <i>e</i> to in the computation of 1155 * <i>e</i><sup>{@code x}</sup> -1. 1156 * @return the value <i>e</i><sup>{@code x}</sup> - 1. 1157 * @since 1.5 1158 */ 1159 public static native double expm1(double x); 1160 1161 /** 1162 * Returns the natural logarithm of the sum of the argument and 1. 1163 * Note that for small values {@code x}, the result of 1164 * {@code log1p(x)} is much closer to the true result of ln(1 1165 * + {@code x}) than the floating-point evaluation of 1166 * {@code log(1.0+x)}. 1167 * 1168 * <p>Special cases: 1169 * <ul> 1170 * 1171 * <li>If the argument is NaN or less than -1, then the result is 1172 * NaN. 1173 * 1174 * <li>If the argument is positive infinity, then the result is 1175 * positive infinity. 1176 * 1177 * <li>If the argument is negative one, then the result is 1178 * negative infinity. 1179 * 1180 * <li>If the argument is zero, then the result is a zero with the 1181 * same sign as the argument. 1182 * 1183 * </ul> 1184 * 1185 * @param x a value 1186 * @return the value ln({@code x} + 1), the natural 1187 * log of {@code x} + 1 1188 * @since 1.5 1189 */ 1190 public static native double log1p(double x); 1191 1192 /** 1193 * Returns the first floating-point argument with the sign of the 1194 * second floating-point argument. For this method, a NaN 1195 * {@code sign} argument is always treated as if it were 1196 * positive. 1197 * 1198 * @param magnitude the parameter providing the magnitude of the result 1199 * @param sign the parameter providing the sign of the result 1200 * @return a value with the magnitude of {@code magnitude} 1201 * and the sign of {@code sign}. 1202 * @since 1.6 1203 */ 1204 public static double copySign(double magnitude, double sign) { 1205 return sun.misc.FpUtils.copySign(magnitude, sign); 1206 } 1207 1208 /** 1209 * Returns the first floating-point argument with the sign of the 1210 * second floating-point argument. For this method, a NaN 1211 * {@code sign} argument is always treated as if it were 1212 * positive. 1213 * 1214 * @param magnitude the parameter providing the magnitude of the result 1215 * @param sign the parameter providing the sign of the result 1216 * @return a value with the magnitude of {@code magnitude} 1217 * and the sign of {@code sign}. 1218 * @since 1.6 1219 */ 1220 public static float copySign(float magnitude, float sign) { 1221 return sun.misc.FpUtils.copySign(magnitude, sign); 1222 } 1223 /** 1224 * Returns the unbiased exponent used in the representation of a 1225 * {@code float}. Special cases: 1226 * 1227 * <ul> 1228 * <li>If the argument is NaN or infinite, then the result is 1229 * {@link Float#MAX_EXPONENT} + 1. 1230 * <li>If the argument is zero or subnormal, then the result is 1231 * {@link Float#MIN_EXPONENT} -1. 1232 * </ul> 1233 * @param f a {@code float} value 1234 * @since 1.6 1235 */ 1236 public static int getExponent(float f) { 1237 return sun.misc.FpUtils.getExponent(f); 1238 } 1239 1240 /** 1241 * Returns the unbiased exponent used in the representation of a 1242 * {@code double}. Special cases: 1243 * 1244 * <ul> 1245 * <li>If the argument is NaN or infinite, then the result is 1246 * {@link Double#MAX_EXPONENT} + 1. 1247 * <li>If the argument is zero or subnormal, then the result is 1248 * {@link Double#MIN_EXPONENT} -1. 1249 * </ul> 1250 * @param d a {@code double} value 1251 * @since 1.6 1252 */ 1253 public static int getExponent(double d) { 1254 return sun.misc.FpUtils.getExponent(d); 1255 } 1256 1257 /** 1258 * Returns the floating-point number adjacent to the first 1259 * argument in the direction of the second argument. If both 1260 * arguments compare as equal the second argument is returned. 1261 * 1262 * <p>Special cases: 1263 * <ul> 1264 * <li> If either argument is a NaN, then NaN is returned. 1265 * 1266 * <li> If both arguments are signed zeros, {@code direction} 1267 * is returned unchanged (as implied by the requirement of 1268 * returning the second argument if the arguments compare as 1269 * equal). 1270 * 1271 * <li> If {@code start} is 1272 * ±{@link Double#MIN_VALUE} and {@code direction} 1273 * has a value such that the result should have a smaller 1274 * magnitude, then a zero with the same sign as {@code start} 1275 * is returned. 1276 * 1277 * <li> If {@code start} is infinite and 1278 * {@code direction} has a value such that the result should 1279 * have a smaller magnitude, {@link Double#MAX_VALUE} with the 1280 * same sign as {@code start} is returned. 1281 * 1282 * <li> If {@code start} is equal to ± 1283 * {@link Double#MAX_VALUE} and {@code direction} has a 1284 * value such that the result should have a larger magnitude, an 1285 * infinity with same sign as {@code start} is returned. 1286 * </ul> 1287 * 1288 * @param start starting floating-point value 1289 * @param direction value indicating which of 1290 * {@code start}'s neighbors or {@code start} should 1291 * be returned 1292 * @return The floating-point number adjacent to {@code start} in the 1293 * direction of {@code direction}. 1294 * @since 1.6 1295 */ 1296 public static double nextAfter(double start, double direction) { 1297 return sun.misc.FpUtils.nextAfter(start, direction); 1298 } 1299 1300 /** 1301 * Returns the floating-point number adjacent to the first 1302 * argument in the direction of the second argument. If both 1303 * arguments compare as equal a value equivalent to the second argument 1304 * is returned. 1305 * 1306 * <p>Special cases: 1307 * <ul> 1308 * <li> If either argument is a NaN, then NaN is returned. 1309 * 1310 * <li> If both arguments are signed zeros, a value equivalent 1311 * to {@code direction} is returned. 1312 * 1313 * <li> If {@code start} is 1314 * ±{@link Float#MIN_VALUE} and {@code direction} 1315 * has a value such that the result should have a smaller 1316 * magnitude, then a zero with the same sign as {@code start} 1317 * is returned. 1318 * 1319 * <li> If {@code start} is infinite and 1320 * {@code direction} has a value such that the result should 1321 * have a smaller magnitude, {@link Float#MAX_VALUE} with the 1322 * same sign as {@code start} is returned. 1323 * 1324 * <li> If {@code start} is equal to ± 1325 * {@link Float#MAX_VALUE} and {@code direction} has a 1326 * value such that the result should have a larger magnitude, an 1327 * infinity with same sign as {@code start} is returned. 1328 * </ul> 1329 * 1330 * @param start starting floating-point value 1331 * @param direction value indicating which of 1332 * {@code start}'s neighbors or {@code start} should 1333 * be returned 1334 * @return The floating-point number adjacent to {@code start} in the 1335 * direction of {@code direction}. 1336 * @since 1.6 1337 */ 1338 public static float nextAfter(float start, double direction) { 1339 return sun.misc.FpUtils.nextAfter(start, direction); 1340 } 1341 1342 /** 1343 * Returns the floating-point value adjacent to {@code d} in 1344 * the direction of positive infinity. This method is 1345 * semantically equivalent to {@code nextAfter(d, 1346 * Double.POSITIVE_INFINITY)}; however, a {@code nextUp} 1347 * implementation may run faster than its equivalent 1348 * {@code nextAfter} call. 1349 * 1350 * <p>Special Cases: 1351 * <ul> 1352 * <li> If the argument is NaN, the result is NaN. 1353 * 1354 * <li> If the argument is positive infinity, the result is 1355 * positive infinity. 1356 * 1357 * <li> If the argument is zero, the result is 1358 * {@link Double#MIN_VALUE} 1359 * 1360 * </ul> 1361 * 1362 * @param d starting floating-point value 1363 * @return The adjacent floating-point value closer to positive 1364 * infinity. 1365 * @since 1.6 1366 */ 1367 public static double nextUp(double d) { 1368 return sun.misc.FpUtils.nextUp(d); 1369 } 1370 1371 /** 1372 * Returns the floating-point value adjacent to {@code f} in 1373 * the direction of positive infinity. This method is 1374 * semantically equivalent to {@code nextAfter(f, 1375 * Float.POSITIVE_INFINITY)}; however, a {@code nextUp} 1376 * implementation may run faster than its equivalent 1377 * {@code nextAfter} call. 1378 * 1379 * <p>Special Cases: 1380 * <ul> 1381 * <li> If the argument is NaN, the result is NaN. 1382 * 1383 * <li> If the argument is positive infinity, the result is 1384 * positive infinity. 1385 * 1386 * <li> If the argument is zero, the result is 1387 * {@link Float#MIN_VALUE} 1388 * 1389 * </ul> 1390 * 1391 * @param f starting floating-point value 1392 * @return The adjacent floating-point value closer to positive 1393 * infinity. 1394 * @since 1.6 1395 */ 1396 public static float nextUp(float f) { 1397 return sun.misc.FpUtils.nextUp(f); 1398 } 1399 1400 1401 /** 1402 * Return {@code d} × 1403 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1404 * by a single correctly rounded floating-point multiply to a 1405 * member of the double value set. See the Java 1406 * Language Specification for a discussion of floating-point 1407 * value sets. If the exponent of the result is between {@link 1408 * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the 1409 * answer is calculated exactly. If the exponent of the result 1410 * would be larger than {@code Double.MAX_EXPONENT}, an 1411 * infinity is returned. Note that if the result is subnormal, 1412 * precision may be lost; that is, when {@code scalb(x, n)} 1413 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1414 * <i>x</i>. When the result is non-NaN, the result has the same 1415 * sign as {@code d}. 1416 * 1417 * <p>Special cases: 1418 * <ul> 1419 * <li> If the first argument is NaN, NaN is returned. 1420 * <li> If the first argument is infinite, then an infinity of the 1421 * same sign is returned. 1422 * <li> If the first argument is zero, then a zero of the same 1423 * sign is returned. 1424 * </ul> 1425 * 1426 * @param d number to be scaled by a power of two. 1427 * @param scaleFactor power of 2 used to scale {@code d} 1428 * @return {@code d} × 2<sup>{@code scaleFactor}</sup> 1429 * @since 1.6 1430 */ 1431 public static double scalb(double d, int scaleFactor) { 1432 return sun.misc.FpUtils.scalb(d, scaleFactor); 1433 } 1434 1435 /** 1436 * Return {@code f} × 1437 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1438 * by a single correctly rounded floating-point multiply to a 1439 * member of the float value set. See the Java 1440 * Language Specification for a discussion of floating-point 1441 * value sets. If the exponent of the result is between {@link 1442 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the 1443 * answer is calculated exactly. If the exponent of the result 1444 * would be larger than {@code Float.MAX_EXPONENT}, an 1445 * infinity is returned. Note that if the result is subnormal, 1446 * precision may be lost; that is, when {@code scalb(x, n)} 1447 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1448 * <i>x</i>. When the result is non-NaN, the result has the same 1449 * sign as {@code f}. 1450 * 1451 * <p>Special cases: 1452 * <ul> 1453 * <li> If the first argument is NaN, NaN is returned. 1454 * <li> If the first argument is infinite, then an infinity of the 1455 * same sign is returned. 1456 * <li> If the first argument is zero, then a zero of the same 1457 * sign is returned. 1458 * </ul> 1459 * 1460 * @param f number to be scaled by a power of two. 1461 * @param scaleFactor power of 2 used to scale {@code f} 1462 * @return {@code f} × 2<sup>{@code scaleFactor}</sup> 1463 * @since 1.6 1464 */ 1465 public static float scalb(float f, int scaleFactor) { 1466 return sun.misc.FpUtils.scalb(f, scaleFactor); 1467 } 1468 }