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