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