2
* -------------------------------------------------------------------------
4
* -------------------------------------------------------------------------
5
* Copyright (c) 1999 Visual Numerics Inc. All Rights Reserved.
7
* Permission to use, copy, modify, and distribute this software is freely
8
* granted by Visual Numerics, Inc., provided that the copyright notice
9
* above and the following warranty disclaimer are preserved in human
12
* Because this software is licenses free of charge, it is provided
13
* "AS IS", with NO WARRANTY. TO THE EXTENT PERMITTED BY LAW, VNI
14
* DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15
* TO ITS PERFORMANCE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16
* VNI WILL NOT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING OUT OF THE USE
17
* OF OR INABILITY TO USE THIS SOFTWARE, INCLUDING BUT NOT LIMITED TO DIRECT,
18
* INDIRECT, SPECIAL, CONSEQUENTIAL, PUNITIVE, AND EXEMPLARY DAMAGES, EVEN
19
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
22
* This Java code is based on C code in the package fdlibm,
23
* which can be obtained from www.netlib.org.
24
* The original fdlibm C code contains the following notice.
26
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
28
* Developed at SunSoft, a Sun Microsystems, Inc. business.
29
* Permission to use, copy, modify, and distribute this
30
* software is freely granted, provided that this notice
33
*--------------------------------------------------------------------------
36
package ikvm.internal;
37
import java.util.Random;
41
* Pure Java implementation of the standard java.lang.Math class.
42
* This Java code is based on C code in the package fdlibm,
43
* which can be obtained from www.netlib.org.
45
* @author Sun Microsystems (original C code in fdlibm)
46
* @author John F. Brophy (translated from C to Java)
49
public final class JMath {
50
static public final double PI = 0x1.921fb54442d18p1; /* 3.14159265358979323846 */
51
static public final double E = 2.7182818284590452354;
52
static private Random random;
55
* Returns the absolute value of its argument.
56
* @param x The argument, an integer.
57
* @return Returns |x|.
59
strictfp public static int abs(int x) {
60
return ((x < 0) ? (-x) : x);
64
* Returns the absolute value of its argument.
65
* @param x The argument, a long.
66
* @return Returns |x|.
68
strictfp public static long abs(long x) {
69
return ((x < 0L) ? (-x) : x);
73
* Returns the absolute value of its argument.
74
* @param x The argument, a float.
75
* @return Returns |x|.
77
strictfp public static float abs(float x) {
78
return ((x <= 0.0f) ? (0.0f - x) : x);
82
* Returns the absolute value of its argument.
83
* @param x The argument, a double.
84
* @return Returns |x|.
86
strictfp public static double abs(double x) {
87
return ((x <= 0.0) ? (0.0 - x) : x);
91
* Returns the smaller of its two arguments.
92
* @param x The first argument, an integer.
93
* @param y The second argument, an integer.
94
* @return Returns the smaller of x and y.
96
strictfp public static int min(int x, int y) {
97
return ((x < y) ? x : y);
101
* Returns the smaller of its two arguments.
102
* @param x The first argument, a long.
103
* @param y The second argument, a long.
104
* @return Returns the smaller of x and y.
106
strictfp public static long min(long x, long y) {
107
return ((x < y) ? x : y);
111
* Returns the smaller of its two arguments.
112
* @param x The first argument, a float.
113
* @param y The second argument, a float.
114
* @return Returns the smaller of x and y.
115
* This function considers -0.0f to
118
strictfp public static float min(float x, float y) {
119
if (Float.isNaN(x)) {
122
float ans = ((x <= y) ? x : y);
123
if ((ans == 0.0f) && (Float.floatToIntBits(y) == 0x80000000)) {
130
* Returns the smaller of its two arguments.
131
* @param x The first argument, a double.
132
* @param y The second argument, a double.
133
* @return Returns the smaller of x and y.
134
* This function considers -0.0 to
137
strictfp public static double min(double x, double y) {
138
if (Double.isNaN(x)) {
141
double ans = ((x <= y) ? x : y);
142
if ((x == 0.0) && (y == 0.0) &&
143
(Double.doubleToLongBits(y) == 0x8000000000000000L)) {
150
* Returns the larger of its two arguments.
151
* @param x The first argument, an integer.
152
* @param y The second argument, an integer.
153
* @return Returns the larger of x and y.
155
strictfp public static int max(int x, int y) {
156
return ((x > y) ? x : y);
160
* Returns the larger of its two arguments.
161
* @param x The first argument, a long.
162
* @param y The second argument, a long.
163
* @return Returns the larger of x and y.
165
strictfp public static long max(long x, long y) {
166
return ((x > y) ? x : y);
170
* Returns the larger of its two arguments.
171
* @param x The first argument, a float.
172
* @param y The second argument, a float.
173
* @return Returns the larger of x and y.
174
* This function considers -0.0f to
177
strictfp public static float max(float x, float y) {
178
if (Float.isNaN(x)) {
181
float ans = ((x >= y) ? x : y);
182
if ((ans == 0.0f) && (Float.floatToIntBits(x) == 0x80000000)) {
189
* Returns the larger of its two arguments.
190
* @param x The first argument, a double.
191
* @param y The second argument, a double.
192
* @return Returns the larger of x and y.
193
* This function considers -0.0 to
196
strictfp public static double max(double x, double y) {
197
if (Double.isNaN(x)) {
200
double ans = ((x >= y) ? x : y);
201
if ((x == 0.0) && (y == 0.0) &&
202
(Double.doubleToLongBits(x) == 0x8000000000000000L)) {
209
* Returns the integer closest to the arguments.
210
* @param x The argument, a float.
211
* @return Returns the integer closest to x.
213
strictfp public static int round(float x) {
214
return (int) floor(x + 0.5f);
218
* Returns the long closest to the arguments.
219
* @param x The argument, a double.
220
* @return Returns the long closest to x.
222
strictfp public static long round(double x) {
223
return (long) floor(x + 0.5);
227
* Returns the random number.
228
* @return Returns a random number from a uniform distribution.
230
synchronized strictfp public static double random() {
231
if (random == null) {
232
random = new Random();
234
return random.nextDouble();
238
* This following code is derived from fdlibm, which contained
239
* the following notice.
240
* ====================================================
241
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
243
* Developed at SunSoft, a Sun Microsystems, Inc. business.
244
* Permission to use, copy, modify, and distribute this
245
* software is freely granted, provided that this notice
247
* ====================================================
250
* The hexadecimal values are the intended ones for the following
251
* constants. The decimal values may be used, provided that the
252
* compiler will convert from decimal to binary accurately enough
253
* to produce the hexadecimal values shown.
255
static private final double huge = 1.0e+300;
256
static private final double tiny = 1.0e-300;
259
* Returns the value of its argument rounded toward
260
* positive infinity to an integral value.
261
* @param x The argument, a double.
262
* @return Returns the smallest double, not less than x,
263
* that is an integral value.
265
static public double ceil(double x) {
274
ix = Double.doubleToLongBits(x);
275
sign = (int) ((ix >> 63) & 1);
276
exp = ((int) (ix >> 52) & 0x7ff) - 0x3ff;
280
return NEGATIVE_ZERO;
281
} else if (x == 0.0) {
286
} else if (exp < 53) {
287
long mask = (0x000fffffffffffffL >>> exp);
288
if ((mask & ix) == 0) {
289
return x; // x is integral
292
ix += (0x0010000000000000L >> exp);
295
} else if (exp == 1024) { // infinity
299
return Double.longBitsToDouble(ix);
303
* Returns the value of its argument rounded toward
304
* negative infinity to an integral value.
305
* @param x The argument, a double.
306
* @return Returns the smallest double, not greater than x,
307
* that is an integral value.
309
static public double floor(double x) {
318
ix = Double.doubleToLongBits(x);
319
sign = (int) ((ix >> 63) & 1);
320
exp = ((int) (ix >> 52) & 0x7ff) - 0x3ff;
325
} else if (x == 0.0) {
330
} else if (exp < 53) {
331
long mask = (0x000fffffffffffffL >>> exp);
332
if ((mask & ix) == 0) {
333
return x; // x is integral
336
ix += (0x0010000000000000L >> exp);
339
} else if (exp == 1024) { // infinity
343
return Double.longBitsToDouble(ix);
346
static private final double[] TWO52 = {
347
0x1.0p52, /* 4.50359962737049600000e+15 */
348
-0x1.0p52 /* -4.50359962737049600000e+15 */
350
static private final double NEGATIVE_ZERO = -0x0.0p0;
353
* Returns the value of its argument rounded toward
354
* the closest integral value.
355
* @param x The argument, a double.
356
* @return Returns the double closest to x
357
* that is an integral value.
359
static public double rint(double x) {
369
ix = Double.doubleToLongBits(x);
370
sign = (int) ((ix >> 63) & 1);
371
exp = ((int) (ix >> 52) & 0x7ff) - 0x3ff;
376
} else if (x > 0.5) {
378
} else if (sign == 0) {
381
return NEGATIVE_ZERO;
383
} else if (exp < 53) {
384
long mask = (0x000fffffffffffffL >>> exp);
385
if ((mask & ix) == 0) {
386
return x; // x is integral
388
} else if (exp == 1024) { // infinity
392
x = Double.longBitsToDouble(ix);
394
return w - TWO52[sign];
398
* Returns x REM p = x - [x/p]*p as if in infinite
399
* precise arithmetic, where [x/p] is the (infinite bit)
400
* integer nearest x/p (in half way case choose the even one).
401
* @param x The dividend.
402
* @param y The divisor.
403
* @return The remainder computed according to the IEEE 754 standard.
405
static public double IEEEremainder(double x, double p) {
413
hx = __HI(x); /* high word of x */
414
lx = __LO(x); /* low word of x */
415
hp = __HI(p); /* high word of p */
416
lp = __LO(p); /* low word of p */
417
sx = hx & 0x80000000;
421
/* purge off exception values */
422
if ((hp | lp) == 0) {
423
return (x * p) / (x * p); /* p = 0 */
426
if ((hx >= 0x7ff00000) || /* x not finite */
427
((hp >= 0x7ff00000) && /* p is NaN */
428
(((hp - 0x7ff00000) | lp) != 0))) {
429
return (x * p) / (x * p);
432
if (hp <= 0x7fdfffff) {
433
x = x % (p + p); /* now x < 2p */
436
if (((hx - hp) | (lx - lp)) == 0) {
442
if (hp < 0x00200000) {
464
* Return correctly rounded sqrt.
465
* ------------------------------------------
466
* | Use the hardware sqrt if you have one |
467
* ------------------------------------------
469
* Bit by bit method using integer arithmetic. (Slow, but portable)
471
* Scale x to y in [1,4) with even powers of 2:
472
* find an integer k such that 1 <= (y=x*2^(2k)) < 4, then
473
* sqrt(x) = 2^k * sqrt(y)
474
* 2. Bit by bit computation
475
* Let q = sqrt(y) truncated to i bit after binary point (q = 1),
478
* s = 2*q , and y = 2 * ( y - q ). (1)
481
* To compute q from q , one checks whether
488
* If (2) is false, then q = q ; otherwise q = q + 2 .
491
* With some algebric manipulation, it is not difficult to see
492
* that (2) is equivalent to
497
* The advantage of (3) is that s and y can be computed by
499
* the following recurrence formula:
502
* s = s , y = y ; (4)
507
* s = s + 2 , y = y - s - 2 (5)
510
* One may easily use induction to prove (4) and (5).
511
* Note. Since the left hand side of (3) contain only i+2 bits,
512
* it does not necessary to do a full (53-bit) comparison
515
* After generating the 53 bits result, we compute one more bit.
516
* Together with the remainder, we can decide whether the
517
* result is exact, bigger than 1/2ulp, or less than 1/2ulp
518
* (it will never equal to 1/2ulp).
519
* The rounding mode can be detected by checking whether
520
* huge + tiny is equal to huge, and whether huge - tiny is
521
* equal to huge for some floating point number "huge" and "tiny".
525
* sqrt(+-0) = +-0 ... exact
527
* sqrt(-ve) = NaN ... with invalid signal
528
* sqrt(NaN) = NaN ... with invalid signal for signaling NaN
532
* Returns the square root of its argument.
533
* @param x The argument, a double.
534
* @return Returns the square root of x.
536
static public double sqrt(double x) {
537
long ix = Double.doubleToLongBits(x);
539
/* take care of Inf and NaN */
540
if ((ix & 0x7ff0000000000000L) == 0x7ff0000000000000L) {
542
/* sqrt(NaN)=NaN, sqrt(+inf)=+inf sqrt(-inf)=sNaN */
546
/* take care of zero */
549
} else if (x == 0.0) {
550
return x; /* sqrt(+-0) = +-0 */
555
ix &= 0x000fffffffffffffL;
557
/* add implicit bit, if not sub-normal */
559
ix |= 0x0010000000000000L;
562
m -= 1023L; /* unbias exponent */
563
if ((m & 1) != 0) { /* odd m, double x to make it even */
566
m >>= 1; /* m = [m/2] */
569
/* generate sqrt(x) bit by bit */
571
long q = 0L; /* q = sqrt(x) */
573
long r = 0x0020000000000000L; /* r = moving bit from right to left */
591
/* assemble result */
592
ix = (m << 52) | (0x000fffffffffffffL & (q >> 1));
593
return Double.longBitsToDouble(ix);
596
static private final double[] halF = { 0.5, -0.5 };
597
static private final double twom1000 = 0x1.0p-1000; /* 2**-1000=9.33263618503218878990e-302 */
598
static private final double o_threshold = 0x1.62e42fefa39efp9; /* 7.09782712893383973096e+02 */
599
static private final double u_threshold = -0x1.74910d52d3051p9; /* -7.45133219101941108420e+02 */
600
static private final double[] ln2HI = {
601
0x1.62e42feep-1, /* 6.93147180369123816490e-01 */
603
}; /* -6.93147180369123816490e-01 */
604
static private final double[] ln2LO = {
605
0x1.a39ef35793c76p-33, /* 1.90821492927058770002e-10 */
606
-0x1.a39ef35793c76p-33
607
}; /* -1.90821492927058770002e-10 */
608
static private final double invln2 = 0x1.71547652b82fep0; /* 1.44269504088896338700e+00 */
609
static private final double P1 = 0x1.555555555553ep-3; /* 1.66666666666666019037e-01 */
610
static private final double P2 = -0x1.6c16c16bebd93p-9; /* -2.77777777770155933842e-03 */
611
static private final double P3 = 0x1.1566aaf25de2cp-14; /* 6.61375632143793436117e-05 */
612
static private final double P4 = -0x1.bbd41c5d26bf1p-20; /* -1.65339022054652515390e-06 */
613
static private final double P5 = 0x1.6376972bea4dp-25; /* 4.13813679705723846039e-08 */
616
* Returns the exponential of x.
619
* 1. Argument reduction:
620
* Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
621
* Given x, find r and integer k such that
623
* x = k*ln2 + r, |r| <= 0.5*ln2.
625
* Here r will be represented as r = hi-lo for better
628
* 2. Approximation of exp(r) by a special rational function on
629
* the interval [0,0.34658]:
631
* R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
632
* We use a special Reme algorithm on [0,0.34658] to generate
633
* a polynomial of degree 5 to approximate R. The maximum error
634
* of this polynomial approximation is bounded by 2**-59. In
636
* R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
637
* (where z=r*r, and the values of P1 to P5 are listed below)
640
* | 2.0+P1*z+...+P5*z - R(z) | <= 2
642
* The computation of exp(r) thus becomes
644
* exp(r) = 1 + -------
647
* = 1 + r + ----------- (for better accuracy)
651
* R1(r) = r - (P1*r + P2*r + ... + P5*r ).
653
* 3. Scale back to obtain exp(x):
654
* From step 1, we have
655
* exp(x) = 2^k * exp(r)
658
* exp(INF) is INF, exp(NaN) is NaN;
659
* exp(-INF) is 0, and
660
* for finite argument, only exp(0)=1 is exact.
663
* according to an error analysis, the error is always less than
664
* 1 ulp (unit in the last place).
668
* if x > 7.09782712893383973096e+02 then exp(x) overflow
669
* if x < -7.45133219101941108420e+02 then exp(x) underflow
673
* Returns the exponential of its argument.
674
* @param x The argument, a double.
675
* @return Returns e to the power x.
677
static public double exp(double x) {
687
hx = __HI(x); /* high word of x */
688
xsb = (hx >>> 31) & 1; /* sign bit of x */
689
hx &= 0x7fffffff; /* high word of |x| */
691
/* filter out non-finite argument */
692
if (hx >= 0x40862E42) { /* if |x|>=709.78... */
693
if (hx >= 0x7ff00000) {
694
if (((hx & 0xfffff) | __LO(x)) != 0) {
695
return x + x; /* NaN */
697
return ((xsb == 0) ? x : 0.0); /* exp(+-inf)={inf,0} */
700
if (x > o_threshold) {
701
return huge * huge; /* overflow */
703
if (x < u_threshold) {
704
return twom1000 * twom1000; /* underflow */
708
/* argument reduction */
709
if (hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */
710
if (hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */
715
k = (int) ((invln2 * x) + halF[xsb]);
717
hi = x - (t * ln2HI[0]); /* t*ln2HI is exact here */
721
} else if (hx < 0x3e300000) { /* when |x|<2**-28 */
722
if ((huge + x) > one) {
723
return one + x; /* trigger inexact */
729
/* x is now in primary range */
731
c = x - (t * (P1 + (t * (P2 + (t * (P3 + (t * (P4 + (t * P5)))))))));
733
return one - (((x * c) / (c - 2.0)) - x);
735
y = one - ((lo - ((x * c) / (2.0 - c))) - hi);
738
long iy = Double.doubleToLongBits(y);
740
iy += ((long) k << 52);
742
iy += ((k + 1000L) << 52);
744
return Double.longBitsToDouble(iy);
747
static private final double ln2_hi = 0x1.62e42feep-1; /* 6.93147180369123816490e-01 */
748
static private final double ln2_lo = 0x1.a39ef35793c76p-33; /* 1.90821492927058770002e-10 */
749
static private final double Lg1 = 0x1.5555555555593p-1; /* 6.666666666666735130e-01 */
750
static private final double Lg2 = 0x1.999999997fa04p-2; /* 3.999999999940941908e-01 */
751
static private final double Lg3 = 0x1.2492494229359p-2; /* 2.857142874366239149e-01 */
752
static private final double Lg4 = 0x1.c71c51d8e78afp-3; /* 2.222219843214978396e-01 */
753
static private final double Lg5 = 0x1.7466496cb03dep-3; /* 1.818357216161805012e-01 */
754
static private final double Lg6 = 0x1.39a09d078c69fp-3; /* 1.531383769920937332e-01 */
755
static private final double Lg7 = 0x1.2f112df3e5244p-3; /* 1.479819860511658591e-01 */
758
* Return the logrithm of x
761
* 1. Argument Reduction: find k and f such that
763
* where sqrt(2)/2 < 1+f < sqrt(2) .
765
* 2. Approximation of log(1+f).
766
* Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
767
* = 2s + 2/3 s**3 + 2/5 s**5 + .....,
769
* We use a special Reme algorithm on [0,0.1716] to generate
770
* a polynomial of degree 14 to approximate R The maximum error
771
* of this polynomial approximation is bounded by 2**-58.45. In
774
* R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s
775
* (the values of Lg1 to Lg7 are listed in the program)
778
* | Lg1*s +...+Lg7*s - R(z) | <= 2
780
* Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
781
* In order to guarantee error in log below 1ulp, we compute log
783
* log(1+f) = f - s*(f - R) (if f is not too large)
784
* log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
786
* 3. Finally, log(x) = k*ln2 + log(1+f).
787
* = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
788
* Here ln2 is split into two floating point number:
790
* where n*ln2_hi is always exact for |n| < 2000.
793
* log(x) is NaN with signal if x < 0 (including -INF) ;
794
* log(+INF) is +INF; log(0) is -INF with signal;
795
* log(NaN) is that NaN with no signal.
798
* according to an error analysis, the error is always less than
799
* 1 ulp (unit in the last place).
803
* Returns the natural logarithm of its argument.
804
* @param x The argument, a double.
805
* @return Returns the natural (base e) logarithm of x.
807
static public double log(double x) {
823
hx = __HI(x); /* high word of x */
824
lx = __LO(x); /* low word of x */
827
if (hx < 0x00100000) { /* x < 2**-1022 */
828
if (((hx & 0x7fffffff) | lx) == 0) {
829
return -two54 / zero; /* log(+-0)=-inf */
832
return (x - x) / zero; /* log(-#) = NaN */
835
x *= two54; /* subnormal number, scale up x */
836
hx = __HI(x); /* high word of x */
838
if (hx >= 0x7ff00000) {
841
k += ((hx >> 20) - 1023);
843
i = (hx + 0x95f64) & 0x100000;
844
x = setHI(x, hx | (i ^ 0x3ff00000)); /* normalize x or x/2 */
847
if ((0x000fffff & (2 + hx)) < 3) { /* |f| < 2**-20 */
854
return (dk * ln2_hi) + (dk * ln2_lo);
856
R = f * f * (0.5 - (0.33333333333333333 * f));
861
return (dk * ln2_hi) - ((R - (dk * ln2_lo)) - f);
870
t1 = w * (Lg2 + (w * (Lg4 + (w * Lg6))));
871
t2 = z * (Lg1 + (w * (Lg3 + (w * (Lg5 + (w * Lg7))))));
877
return f - (hfsq - (s * (hfsq + R)));
879
return (dk * ln2_hi) -
880
((hfsq - ((s * (hfsq + R)) + (dk * ln2_lo))) - f);
884
return f - (s * (f - R));
886
return (dk * ln2_hi) - (((s * (f - R)) - (dk * ln2_lo)) - f);
892
* Returns the sine of its argument.
893
* @param x The argument, a double, assumed to be in radians.
894
* @return Returns the sine of x.
896
static public double sin(double x) {
901
ix &= 0x7fffffff; /* |x| ~< pi/4 */
903
if (ix <= 0x3fe921fb) {
904
return __kernel_sin(x, z, 0);
905
} else if (ix >= 0x7ff00000) {
907
/* sin(Inf or NaN) is NaN */
911
/* argument reduction needed */
912
double[] y = new double[2];
913
n = __ieee754_rem_pio2(x, y);
916
return __kernel_sin(y[0], y[1], 1);
918
return __kernel_cos(y[0], y[1]);
920
return -__kernel_sin(y[0], y[1], 1);
922
return -__kernel_cos(y[0], y[1]);
927
static private final double S1 = -1.66666666666666324348e-01; /* 0xBFC55555, 0x55555549 */
928
static private final double S2 = 8.33333333332248946124e-03; /* 0x3F811111, 0x1110F8A6 */
929
static private final double S3 = -1.98412698298579493134e-04; /* 0xBF2A01A0, 0x19C161D5 */
930
static private final double S4 = 2.75573137070700676789e-06; /* 0x3EC71DE3, 0x57B1FE7D */
931
static private final double S5 = -2.50507602534068634195e-08; /* 0xBE5AE5E6, 0x8A2B9CEB */
932
static private final double S6 = 1.58969099521155010221e-10; /* 0x3DE5D93A, 0x5ACFD57C */
935
* kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854
936
* Input x is assumed to be bounded by ~pi/4 in magnitude.
937
* Input y is the tail of x. * Input iy indicates whether y is 0. (if iy=0, y assume to be 0).
940
* 1. Since sin(-x) = -sin(x), we need only to consider positive x.
941
* 2. if x < 2^-27 (hx<0x3e400000 0), return x with inexact if x!=0.
942
* 3. sin(x) is approximated by a polynomial of degree 13 on
945
* sin(x) ~ x + S1*x + ... + S6*x
948
* |sin(x) 2 4 6 8 10 12 | -58
949
* |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2
952
* 4. sin(x+y) = sin(x) + sin'(x')*y
953
* ~ sin(x) + (1-x*x/2)*y
954
* For better accuracy, let
956
* r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6))))
958
* sin(x) = x + (S1*x + (x *(r-y/2)+y))
960
static double __kernel_sin(double x, double y, int iy) {
965
ix = __HI(x) & 0x7fffffff; /* high word of x */
966
if (ix < 0x3e400000) { /* |x| < 2**-27 */
968
return x; /* generate inexact */
973
r = S2 + (z * (S3 + (z * (S4 + (z * (S5 + (z * S6)))))));
975
return x + (v * (S1 + (z * r)));
977
return x - (((z * ((half * y) - (v * r))) - y) - (v * S1));
982
* Returns the cosine of its argument.
983
* @param x The argument, a double, assumed to be in radians.
984
* @return Returns the cosine of x.
986
static public double cos(double x) {
991
/* High word of x. */
996
if (ix <= 0x3fe921fb) {
997
return __kernel_cos(x, z);
999
/* cos(Inf or NaN) is NaN */
1000
} else if (ix >= 0x7ff00000) {
1003
/* argument reduction needed */
1005
double[] y = new double[2];
1006
n = __ieee754_rem_pio2(x, y);
1009
return __kernel_cos(y[0], y[1]);
1011
return -__kernel_sin(y[0], y[1], 1);
1013
return -__kernel_cos(y[0], y[1]);
1015
return __kernel_sin(y[0], y[1], 1);
1020
static private final double one = 0x1.0p0; /* 1.00000000000000000000e+00 */
1021
static private final double C1 = 0x1.555555555554cp-5; /* 4.16666666666666019037e-02 */
1022
static private final double C2 = -0x1.6c16c16c15177p-10; /* -1.38888888888741095749e-03 */
1023
static private final double C3 = 0x1.a01a019cb159p-16; /* 2.48015872894767294178e-05 */
1024
static private final double C4 = -0x1.27e4f809c52adp-22; /* -2.75573143513906633035e-07 */
1025
static private final double C5 = 0x1.1ee9ebdb4b1c4p-29; /* 2.08757232129817482790e-09 */
1026
static private final double C6 = -0x1.8fae9be8838d4p-37; /* -1.13596475577881948265e-11 */
1029
* kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
1030
* Input x is assumed to be bounded by ~pi/4 in magnitude.
1031
* Input y is the tail of x.
1034
* 1. Since cos(-x) = cos(x), we need only to consider positive x.
1035
* 2. if x < 2^-27 (hx<0x3e400000 0), return 1 with inexact if x!=0.
1036
* 3. cos(x) is approximated by a polynomial of degree 14 on
1039
* cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x
1040
* where the remez error is
1042
* | 2 4 6 8 10 12 14 | -58
1043
* |cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x +C6*x )| <= 2
1047
* 4. let r = C1*x +C2*x +C3*x +C4*x +C5*x +C6*x , then
1048
* cos(x) = 1 - x*x/2 + r
1049
* since cos(x+y) ~ cos(x) - sin(x)*y
1051
* a correction term is necessary in cos(x) and hence
1052
* cos(x+y) = 1 - (x*x/2 - (r - x*y))
1053
* For better accuracy when x > 0.3, let qx = |x|/4 with
1054
* the last 32 bits mask off, and if x > 0.78125, let qx = 0.28125.
1056
* cos(x+y) = (1-qx) - ((x*x/2-qx) - (r-x*y)).
1057
* Note that 1-qx and (x*x/2-qx) is EXACT here, and the
1058
* magnitude of the latter is at least a quarter of x*x/2,
1059
* thus, reducing the rounding error in the subtraction.
1061
static private double __kernel_cos(double x, double y) {
1068
ix = __HI(x) & 0x7fffffff; /* ix = |x|'s high word*/
1069
if (ix < 0x3e400000) {
1072
if (((int) x) == 0) {
1073
return one; /* generate inexact */
1078
(z * (C2 + (z * (C3 + (z * (C4 + (z * (C5 + (z * C6))))))))));
1079
if (ix < 0x3FD33333) {
1082
return one - ((0.5 * z) - ((z * r) - (x * y)));
1084
if (ix > 0x3fe90000) { /* x > 0.78125 */
1087
qx = set(ix - 0x00200000, 0); /* x/4 */
1089
hz = (0.5 * z) - qx;
1091
return a - (hz - ((z * r) - (x * y)));
1096
* Returns the tangent of its argument.
1097
* @param x The argument, a double, assumed to be in radians.
1098
* @return Returns the tangent of x.
1100
static public double tan(double x) {
1108
if (ix <= 0x3fe921fb) {
1109
return __kernel_tan(x, z, 1);
1110
} else if (ix >= 0x7ff00000) {
1112
/* tan(Inf or NaN) is NaN */
1113
return x - x; /* NaN */
1116
/* argument reduction needed */
1117
double[] y = new double[2];
1118
n = __ieee754_rem_pio2(x, y);
1120
/* 1 -- n even -1 -- n odd */
1121
return __kernel_tan(y[0], y[1], 1 - ((n & 1) << 1));
1125
static private final double pio4 = 0x1.921fb54442d18p-1; /* 7.85398163397448278999e-01 */
1126
static private final double pio4lo = 0x1.1a62633145c07p-55; /* 3.06161699786838301793e-17 */
1127
static private final double[] T = {
1128
0x1.5555555555563p-2, /* 3.33333333333334091986e-01 */
1129
0x1.111111110fe7ap-3, /* 1.33333333333201242699e-01 */
1130
0x1.ba1ba1bb341fep-5, /* 5.39682539762260521377e-02 */
1131
0x1.664f48406d637p-6, /* 2.18694882948595424599e-02 */
1132
0x1.226e3e96e8493p-7, /* 8.86323982359930005737e-03 */
1133
0x1.d6d22c9560328p-9, /* 3.59207910759131235356e-03 */
1134
0x1.7dbc8fee08315p-10, /* 1.45620945432529025516e-03 */
1135
0x1.344d8f2f26501p-11, /* 5.88041240820264096874e-04 */
1136
0x1.026f71a8d1068p-12, /* 2.46463134818469906812e-04 */
1137
0x1.47e88a03792a6p-14, /* 7.81794442939557092300e-05 */
1138
0x1.2b80f32f0a7e9p-14, /* 7.14072491382608190305e-05 */
1139
-0x1.375cbdb605373p-16, /* -1.85586374855275456654e-05 */
1140
0x1.b2a7074bf7ad4p-16 /* 2.59073051863633712884e-05 */
1144
* __kernel_tan( x, y, k )
1145
* kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854
1146
* Input x is assumed to be bounded by ~pi/4 in magnitude.
1147
* Input y is the tail of x. * Input k indicates whether tan (if k=1) or
1148
* -1/tan (if k= -1) is returned. * * Algorithm
1149
* 1. Since tan(-x) = -tan(x), we need only to consider positive x.
1150
* 2. if x < 2^-28 (hx<0x3e300000 0), return x with inexact if x!=0.
1151
* 3. tan(x) is approximated by a odd polynomial of degree 27 on
1154
* tan(x) ~ x + T1*x + ... + T13*x
1157
* |tan(x) 2 4 26 | -59.2
1158
* |----- - (1+T1*x +T2*x +.... +T13*x )| <= 2
1161
* Note: tan(x+y) = tan(x) + tan'(x)*y
1162
* ~ tan(x) + (1+x*x)*y
1163
* Therefore, for better accuracy in computing tan(x+y), let
1165
* r = x *(T2+x *(T3+x *(...+x *(T12+x *T13))))
1168
* tan(x+y) = x + (T1*x + (x *(r+y)+y)) *
1169
* 4. For x in [0.67434,pi/4], let y = pi/4 - x, then
1170
* tan(x) = tan(pi/4-y) = (1-tan(y))/(1+tan(y))
1171
* = 1 - 2*(tan(y) - (tan(y)^2)/(1+tan(y)))
1173
static private double __kernel_tan(double x, double y, int iy) {
1182
hx = __HI(x); /* high word of x */
1183
ix = hx & 0x7fffffff; /* high word of |x| */
1184
if (ix < 0x3e300000) { /* x < 2**-28 */
1185
if ((int) x == 0) { /* generate inexact */
1186
if (((ix | __LO(x)) | (iy + 1)) == 0) {
1187
return one / abs(x);
1189
return (iy == 1) ? x : (-one / x);
1193
if (ix >= 0x3FE59428) {
1209
* Break x^5*(T[1]+x^2*T[2]+...) into
1210
* x^5(T[1]+x^4*T[3]+...+x^20*T[11]) +
1211
* x^5(x^2*(T[2]+x^4*T[4]+...+x^22*[T12]))
1215
(w * (T[5] + (w * (T[7] + (w * (T[9] + (w * T[11])))))))));
1218
(w * (T[6] + (w * (T[8] + (w * (T[10] + (w * T[12]))))))))));
1220
r = y + (z * ((s * (r + v)) + y));
1223
if (ix >= 0x3FE59428) {
1225
return (double) (1 - ((hx >> 30) & 2)) * (v -
1226
(2.0 * (x - (((w * w) / (w + v)) - r))));
1232
/* if allow error up to 2 ulp, simply return -1.0/(x+r) here */
1233
/* compute -1.0/(x+r) accurately */
1236
/* if allow error up to 2 ulp, simply return -1.0/(x+r) here */
1237
/* compute -1.0/(x+r) accurately */
1249
return t + (a * (s + (t * v)));
1253
static private final double pio2_hi = 0x1.921fb54442d18p0; /* 1.57079632679489655800e+00 */
1254
static private final double pio2_lo = 0x1.1a62633145c07p-54; /* 6.12323399573676603587e-17 */
1255
static private final double pio4_hi = 0x1.921fb54442d18p-1; /* 7.85398163397448278999e-01 */
1257
/* coefficient for R(x^2) */
1258
static private final double pS0 = 0x1.5555555555555p-3; /* 1.66666666666666657415e-01 */
1259
static private final double pS1 = -0x1.4d61203eb6f7dp-2; /* -3.25565818622400915405e-01 */
1260
static private final double pS2 = 0x1.9c1550e884455p-3; /* 2.01212532134862925881e-01 */
1261
static private final double pS3 = -0x1.48228b5688f3bp-5; /* -4.00555345006794114027e-02 */
1262
static private final double pS4 = 0x1.9efe07501b288p-11; /* 7.91534994289814532176e-04 */
1263
static private final double pS5 = 0x1.23de10dfdf709p-15; /* 3.47933107596021167570e-05 */
1264
static private final double qS1 = -0x1.33a271c8a2d4bp1; /* -2.40339491173441421878e+00 */
1265
static private final double qS2 = 0x1.02ae59c598ac8p1; /* 2.02094576023350569471e+00 */
1266
static private final double qS3 = -0x1.6066c1b8d0159p-1; /* -6.88283971605453293030e-01 */
1267
static private final double qS4 = 0x1.3b8c5b12e9282p-4; /* 7.70381505559019352791e-02 */
1272
* Since asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ...
1273
* we approximate asin(x) on [0,0.5] by
1274
* asin(x) = x + x*x^2*R(x^2)
1276
* R(x^2) is a rational approximation of (asin(x)-x)/x^3
1277
* and its remez error is bounded by
1278
* |(asin(x)-x)/x^3 - R(x^2)| < 2^(-58.75)
1281
* asin(x) = pi/2-2*asin(sqrt((1-x)/2))
1282
* Let y = (1-x), z = y/2, s := sqrt(z), and pio2_hi+pio2_lo=pi/2;
1284
* asin(x) = pi/2 - 2*(s+s*z*R(z))
1285
* = pio2_hi - (2*(s+s*z*R(z)) - pio2_lo)
1286
* For x<=0.98, let pio4_hi = pio2_hi/2, then
1288
* c = sqrt(z) - f = (z-f*f)/(s+f) ...f+c=sqrt(z)
1290
* asin(x) = pi/2 - 2*(s+s*z*R(z))
1291
* = pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo)
1292
* = pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c))
1295
* if x is NaN, return x itself;
1296
* if |x|>1, return NaN with invalid signal.
1301
* Returns the inverse (arc) sine of its argument.
1302
* @param x The argument, a double.
1303
* @return Returns the angle, in radians, whose sine is x.
1304
* It is in the range [-pi/2,pi/2].
1306
static public double asin(double x) {
1318
ix = hx & 0x7fffffff;
1319
if (ix >= 0x3ff00000) { /* |x|>= 1 */
1320
if (((ix - 0x3ff00000) | __LO(x)) == 0) {
1322
/* asin(1)=+-pi/2 with inexact */
1323
return (x * pio2_hi) + (x * pio2_lo);
1325
return (x - x) / (x - x); /* asin(|x|>1) is NaN */
1326
} else if (ix < 0x3fe00000) { /* |x|<0.5 */
1327
if (ix < 0x3e400000) { /* if |x| < 2**-27 */
1328
if ((huge + x) > one) {
1329
return x; /* return x with inexact if x!=0*/
1336
(t * (pS2 + (t * (pS3 + (t * (pS4 + (t * pS5))))))))));
1337
q = one + (t * (qS1 + (t * (qS2 + (t * (qS3 + (t * qS4)))))));
1346
(t * (pS1 + (t * (pS2 + (t * (pS3 + (t * (pS4 + (t * pS5))))))))));
1347
q = one + (t * (qS1 + (t * (qS2 + (t * (qS3 + (t * qS4)))))));
1349
if (ix >= 0x3FEF3333) { /* if |x| > 0.975 */
1351
t = pio2_hi - ((2.0 * (s + (s * w))) - pio2_lo);
1355
c = (t - (w * w)) / (s + w);
1357
p = (2.0 * s * r) - (pio2_lo - (2.0 * c));
1358
q = pio4_hi - (2.0 * w);
1359
t = pio4_hi - (p - q);
1361
return ((hx > 0) ? t : (-t));
1366
* acos(x) = pi/2 - asin(x)
1367
* acos(-x) = pi/2 + asin(x)
1369
* acos(x) = pi/2 - (x + x*x^2*R(x^2)) (see asin.c)
1371
* acos(x) = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2)))
1372
* = 2asin(sqrt((1-x)/2))
1373
* = 2s + 2s*z*R(z) ...z=(1-x)/2, s=sqrt(z)
1374
* = 2f + (2c + 2s*z*R(z))
1375
* where f=hi part of s, and c = (z-f*f)/(s+f) is the correction term
1376
* for f so that f+c ~ sqrt(z).
1378
* acos(x) = pi - 2asin(sqrt((1-|x|)/2))
1379
* = pi - 0.5*(s+s*z*R(z)), where z=(1-|x|)/2,s=sqrt(z)
1382
* if x is NaN, return x itself;
1383
* if |x|>1, return NaN with invalid signal.
1385
* Function needed: sqrt
1389
* Returns the inverse (arc) cosine of its argument.
1390
* @param x The argument, a double.
1391
* @return Returns the angle, in radians, whose cosine is x.
1392
* It is in the range [0,pi].
1394
static public double acos(double x) {
1406
ix = hx & 0x7fffffff;
1407
if (ix >= 0x3ff00000) { /* |x| >= 1 */
1408
if (((ix - 0x3ff00000) | __LO(x)) == 0) { /* |x|==1 */
1410
return 0.0; /* acos(1) = 0 */
1412
return PI + (2.0 * pio2_lo); /* acos(-1)= pi */
1415
return (x - x) / (x - x); /* acos(|x|>1) is NaN */
1417
if (ix < 0x3fe00000) { /* |x| < 0.5 */
1418
if (ix <= 0x3c600000) {
1419
return pio2_hi + pio2_lo; /*if|x|<2**-57*/
1424
(z * (pS2 + (z * (pS3 + (z * (pS4 + (z * pS5))))))))));
1425
q = one + (z * (qS1 + (z * (qS2 + (z * (qS3 + (z * qS4)))))));
1427
return pio2_hi - (x - (pio2_lo - (x * r)));
1428
} else if (hx < 0) { /* x < -0.5 */
1429
z = (one + x) * 0.5;
1432
(z * (pS2 + (z * (pS3 + (z * (pS4 + (z * pS5))))))))));
1433
q = one + (z * (qS1 + (z * (qS2 + (z * (qS3 + (z * qS4)))))));
1436
w = (r * s) - pio2_lo;
1437
return PI - (2.0 * (s + w));
1438
} else { /* x > 0.5 */
1439
z = (one - x) * 0.5;
1443
c = (z - (df * df)) / (s + df);
1446
(z * (pS2 + (z * (pS3 + (z * (pS4 + (z * pS5))))))))));
1447
q = one + (z * (qS1 + (z * (qS2 + (z * (qS3 + (z * qS4)))))));
1450
return 2.0 * (df + w);
1454
static private final double[] atanhi = {
1455
0x1.dac670561bb4fp-2, /* 4.63647609000806093515e-01 atan(0.5)hi */
1456
0x1.921fb54442d18p-1, /* 7.85398163397448278999e-01 atan(1.0)hi */
1457
0x1.f730bd281f69bp-1, /* 9.82793723247329054082e-01 atan(1.5)hi */
1458
0x1.921fb54442d18p0 /* 1.57079632679489655800e+00 atan(inf)hi */
1460
static private final double[] atanlo = {
1461
0x1.a2b7f222f65e2p-56, /* 2.26987774529616870924e-17 atan(0.5)lo */
1462
0x1.1a62633145c07p-55, /* 3.06161699786838301793e-17 atan(1.0)lo */
1463
0x1.007887af0cbbdp-56, /* 1.39033110312309984516e-17 atan(1.5)lo */
1464
0x1.1a62633145c07p-54 /* 6.12323399573676603587e-17 atan(inf)lo */
1466
static private final double[] aT = {
1467
0x1.555555555550dp-2, /* 3.33333333333329318027e-01 */
1468
-0x1.999999998ebc4p-3, /* -1.99999999998764832476e-01 */
1469
0x1.24924920083ffp-3, /* 1.42857142725034663711e-01 */
1470
-0x1.c71c6fe231671p-4, /* -1.11111104054623557880e-01 */
1471
0x1.745cdc54c206ep-4, /* 9.09088713343650656196e-02 */
1472
-0x1.3b0f2af749a6dp-4, /* -7.69187620504482999495e-02 */
1473
0x1.10d66a0d03d51p-4, /* 6.66107313738753120669e-02 */
1474
-0x1.dde2d52defd9ap-5, /* -5.83357013379057348645e-02 */
1475
0x1.97b4b24760debp-5, /* 4.97687799461593236017e-02 */
1476
-0x1.2b4442c6a6c2fp-5, /* -3.65315727442169155270e-02 */
1477
0x1.0ad3ae322da11p-6 /* 1.62858201153657823623e-02 */
1482
* 1. Reduce x to positive by atan(x) = -atan(-x).
1483
* 2. According to the integer k=4t+0.25 chopped, t=x, the argument
1484
* is further reduced to one of the following intervals and the
1485
* arctangent of t is evaluated by the corresponding formula:
1487
* [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...)
1488
* [7/16,11/16] atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) )
1489
* [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) )
1490
* [19/16,39/16] atan(x) = atan(3/2) + atan( (t-1.5)/(1+1.5t) )
1491
* [39/16,INF] atan(x) = atan(INF) + atan( -1/t )
1494
* The hexadecimal values are the intended ones for the following
1495
* constants. The decimal values may be used, provided that the
1496
* compiler will convert from decimal to binary accurately enough
1497
* to produce the hexadecimal values shown.
1501
* Returns the inverse (arc) tangent of its argument.
1502
* @param x The argument, a double.
1503
* @return Returns the angle, in radians, whose tangent is x.
1504
* It is in the range [-pi/2,pi/2].
1506
static public double atan(double x) {
1516
ix = hx & 0x7fffffff;
1517
if (ix >= 0x44100000) { /* if |x| >= 2^66 */
1518
if ((ix > 0x7ff00000) || ((ix == 0x7ff00000) && (__LO(x) != 0))) {
1519
return x + x; /* NaN */
1522
return atanhi[3] + atanlo[3];
1524
return -atanhi[3] - atanlo[3];
1527
if (ix < 0x3fdc0000) { /* |x| < 0.4375 */
1528
if (ix < 0x3e200000) { /* |x| < 2^-29 */
1529
if ((huge + x) > one) {
1530
return x; /* raise inexact */
1536
if (ix < 0x3ff30000) { /* |x| < 1.1875 */
1537
if (ix < 0x3fe60000) { /* 7/16 <=|x|<11/16 */
1539
x = ((2.0 * x) - one) / (2.0 + x);
1540
} else { /* 11/16<=|x|< 19/16 */
1542
x = (x - one) / (x + one);
1545
if (ix < 0x40038000) { /* |x| < 2.4375 */
1547
x = (x - 1.5) / (one + (1.5 * x));
1548
} else { /* 2.4375 <= |x| < 2^66 */
1555
/* end of argument reduction */
1559
/* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
1562
(w * (aT[4] + (w * (aT[6] + (w * (aT[8] + (w * aT[10]))))))))));
1564
(w * (aT[3] + (w * (aT[5] + (w * (aT[7] + (w * aT[9]))))))));
1566
return x - (x * (s1 + s2));
1568
z = atanhi[id] - (((x * (s1 + s2)) - atanlo[id]) - x);
1569
return (hx < 0) ? (-z) : z;
1573
static private final double pi_o_4 = 0x1.921fb54442d18p-1; /* 7.8539816339744827900e-01 */
1574
static private final double pi_o_2 = 0x1.921fb54442d18p0; /* 1.5707963267948965580e+00 */
1575
static private final double pi_lo = 0x1.1a62633145c07p-53; /* 1.2246467991473531772e-16 */
1579
* 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
1580
* 2. Reduce x to positive by (if x and y are unexceptional):
1581
* ARG (x+iy) = arctan(y/x) ... if x > 0,
1582
* ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0,
1586
* ATAN2((anything), NaN ) is NaN;
1587
* ATAN2(NAN , (anything) ) is NaN;
1588
* ATAN2(+-0, +(anything but NaN)) is +-0 ;
1589
* ATAN2(+-0, -(anything but NaN)) is +-pi ;
1590
* ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;
1591
* ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;
1592
* ATAN2(+-(anything but INF and NaN), -INF) is +-pi;
1593
* ATAN2(+-INF,+INF ) is +-pi/4 ;
1594
* ATAN2(+-INF,-INF ) is +-3pi/4;
1595
* ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
1598
* The hexadecimal values are the intended ones for the following
1599
* constants. The decimal values may be used, provided that the
1600
* compiler will convert from decimal to binary accurately enough
1601
* to produce the hexadecimal values shown.
1605
* Returns angle corresponding to a Cartesian point.
1606
* @param x The first argument, a double.
1607
* @param y The second argument, a double.
1608
* @return Returns the angle, in radians, the the line
1609
* from (0,0) to (x,y) makes with the x-axis.
1610
* It is in the range [-pi,pi].
1612
static public double atan2(double y, double x) {
1624
ix = hx & 0x7fffffff;
1627
iy = hy & 0x7fffffff;
1629
if (((ix | ((lx | -lx) >>> 31)) > 0x7ff00000) ||
1630
((iy | ((ly | -ly) >>> 31)) > 0x7ff00000)) { /* x or y is NaN */
1633
if (((hx - 0x3ff00000) | lx) == 0) {
1634
return atan(y); /* x=1.0 */
1636
m = ((hy >> 31) & 1) | ((hx >> 30) & 2); /* 2*sign(x)+sign(y) */
1639
if ((iy | ly) == 0) {
1643
return y; /* atan(+-0,+anything)=+-0 */
1645
return PI + tiny; /* atan(+0,-anything) = pi */
1647
return -PI - tiny; /* atan(-0,-anything) =-pi */
1652
if ((ix | lx) == 0) {
1653
return ((hy < 0) ? (-pi_o_2 - tiny) : (pi_o_2 + tiny));
1657
if (ix == 0x7ff00000) {
1658
if (iy == 0x7ff00000) {
1661
return pi_o_4 + tiny; /* atan(+INF,+INF) */
1663
return -pi_o_4 - tiny; /* atan(-INF,+INF) */
1665
return (3.0 * pi_o_4) + tiny; /*atan(+INF,-INF)*/
1667
return (-3.0 * pi_o_4) - tiny; /*atan(-INF,-INF)*/
1672
return zero; /* atan(+...,+INF) */
1674
return -zero; /* atan(-...,+INF) */
1676
return PI + tiny; /* atan(+...,-INF) */
1678
return -PI - tiny; /* atan(-...,-INF) */
1684
if (iy == 0x7ff00000) {
1685
return (hy < 0) ? (-pi_o_2 - tiny) : (pi_o_2 + tiny);
1689
k = (iy - ix) >> 20;
1693
z = pi_o_2 + (0.5 * pi_lo);
1694
} else if ((hx < 0) && (k < -60)) {
1696
/* |y|/x < -2**60 */
1700
/* safe to do y/x */
1701
z = atan(abs(y / x));
1706
return z; /* atan(+,+) */
1708
return setHI(z, __HI(z) ^ 0x80000000); /* atan(-,+) */
1710
return PI - (z - pi_lo); /* atan(+,-) */
1711
default: /* case 3 */
1712
return (z - pi_lo) - PI; /* atan(-,-) */
1718
* __kernel_sin ... sine function on [-pi/4,pi/4]
1719
* __ieee754_rem_pio2 ... argument reduction routine
1722
* Let S,C and T denote the sin, cos and tan respectively on
1723
* [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
1724
* in [-pi/4 , +pi/4], and let n = k mod 4.
1727
* n sin(x) cos(x) tan(x)
1728
* ----------------------------------------------------------
1733
* ----------------------------------------------------------
1736
* Let trig be any of sin, cos, or tan.
1737
* trig(+-INF) is NaN, with signals;
1738
* trig(NaN) is that NaN;
1741
* TRIG(x) returns trig(x) nearly rounded
1744
* Table of constants for 2/pi, 396 Hex digits (476 decimal) of 2/pi
1746
static private final int[] two_over_pi = {
1747
0xa2f983, 0x6e4e44, 0x1529fc, 0x2757d1, 0xf534dd, 0xc0db62, 0x95993c,
1748
0x439041, 0xfe5163, 0xabdebb, 0xc561b7, 0x246e3a, 0x424dd2, 0xe00649,
1749
0x2eea09, 0xd1921c, 0xfe1deb, 0x1cb129, 0xa73ee8, 0x8235f5, 0x2ebb44,
1750
0x84e99c, 0x7026b4, 0x5f7e41, 0x3991d6, 0x398353, 0x39f49c, 0x845f8b,
1751
0xbdf928, 0x3b1ff8, 0x97ffde, 0x05980f, 0xef2f11, 0x8b5a0a, 0x6d1f6d,
1752
0x367ecf, 0x27cb09, 0xb74f46, 0x3f669e, 0x5fea2d, 0x7527ba, 0xc7ebe5,
1753
0xf17b3d, 0x0739f7, 0x8a5292, 0xea6bfb, 0x5fb11f, 0x8d5d08, 0x560330,
1754
0x46fc7b, 0x6babf0, 0xcfbc20, 0x9af436, 0x1da9e3, 0x91615e, 0xe61b08,
1755
0x659985, 0x5f14a0, 0x68408d, 0xffd880, 0x4d7327, 0x310606, 0x1556ca,
1756
0x73a8c9, 0x60e27b, 0xc08c6b
1758
static private final int[] npio2_hw = {
1759
0x3ff921fb, 0x400921fb, 0x4012d97c, 0x401921fb, 0x401f6a7a,
1760
0x4022d97c, 0x4025fdbb, 0x402921fb, 0x402c463a, 0x402f6a7a,
1761
0x4031475c, 0x4032d97c, 0x40346b9c, 0x4035fdbb, 0x40378fdb,
1762
0x403921fb, 0x403ab41b, 0x403c463a, 0x403dd85a, 0x403f6a7a,
1763
0x40407e4c, 0x4041475c, 0x4042106c, 0x4042d97c, 0x4043a28c,
1764
0x40446b9c, 0x404534ac, 0x4045fdbb, 0x4046c6cb, 0x40478fdb,
1765
0x404858eb, 0x404921fb
1767
static private final double zero = 0.00000000000000000000e+00; // 0x0000000000000000
1768
static private final double half = 0x1.0p-1; /* 5.00000000000000000000e-01 */
1769
static private final double two24 = 0x1.0p24; /* 1.67772160000000000000e+07 */
1770
static private final double invpio2 = 0x1.45f306dc9c883p-1; /* 6.36619772367581382433e-01 53 bits of 2/pi */
1771
static private final double pio2_1 = 0x1.921fb544p0; /* 1.57079632673412561417e+00 first 33 bit of pi/2 */
1772
static private final double pio2_1t = 0x1.0b4611a626331p-34; /* 6.07710050650619224932e-11 pi/2 - pio2_1 */
1773
static private final double pio2_2 = 0x1.0b4611a6p-34; /* 6.07710050630396597660e-11 second 33 bit of pi/2 */
1774
static private final double pio2_2t = 0x1.3198a2e037073p-69; /* 2.02226624879595063154e-21 pi/2 - (pio2_1+pio2_2) */
1775
static private final double pio2_3 = 0x1.3198a2ep-69; /* 2.02226624871116645580e-21 third 33 bit of pi/2 */
1776
static private final double pio2_3t = 0x1.b839a252049c1p-104; /* 8.47842766036889956997e-32 pi/2 - (pio2_1+pio2_2+pio2_3) */
1779
* Return the remainder of x % pi/2 in y[0]+y[1]
1781
static private int __ieee754_rem_pio2(double x, double[] y) {
1794
hx = __HI(x); /* high word of x */
1795
ix = hx & 0x7fffffff;
1796
if (ix <= 0x3fe921fb) {
1798
/* |x| ~<= pi/4 , no need for reduction */
1804
if (ix < 0x4002d97c) {
1806
/* |x| < 3pi/4, special case with n=+-1 */
1809
if (ix != 0x3ff921fb) { /* 33+53 bit pi is good enough */
1811
y[1] = (z - y[0]) - pio2_1t;
1812
} else { /* near pi/2, use 33+33+53 bit pi */
1815
y[1] = (z - y[0]) - pio2_2t;
1818
} else { /* negative x */
1820
if (ix != 0x3ff921fb) { /* 33+53 bit pi is good enough */
1822
y[1] = (z - y[0]) + pio2_1t;
1823
} else { /* near pi/2, use 33+33+53 bit pi */
1826
y[1] = (z - y[0]) + pio2_2t;
1832
if (ix <= 0x413921fb) { /* |x| ~<= 2^19*(pi/2), medium size */
1834
n = (int) ((t * invpio2) + half);
1836
r = t - (fn * pio2_1);
1837
w = fn * pio2_1t; /* 1st round good to 85 bit */
1838
if ((n < 32) && (ix != npio2_hw[n - 1])) {
1839
y[0] = r - w; /* quick check no cancellation */
1843
i = j - (((__HI(y[0])) >> 20) & 0x7ff);
1844
if (i > 16) { /* 2nd iteration needed, good to 118 */
1848
w = (fn * pio2_2t) - ((t - r) - w);
1850
i = j - (((__HI(y[0])) >> 20) & 0x7ff);
1851
if (i > 49) { /* 3rd iteration need, 151 bits acc */
1852
t = r; /* will cover all possible cases */
1855
w = (fn * pio2_3t) - ((t - r) - w);
1860
y[1] = (r - y[0]) - w;
1871
* all other (large) arguments
1873
if (ix >= 0x7ff00000) {
1875
/* x is inf or NaN */
1876
y[0] = y[1] = x - x;
1880
/* set z = scalbn(|x|,ilogb(x)-23) */
1881
double[] tx = new double[3];
1882
long lx = Double.doubleToLongBits(x);
1883
long exp = (0x7ff0000000000000L & lx) >> 52;
1886
lx &= 0x7fffffffffffffffL;
1887
z = Double.longBitsToDouble(lx);
1888
for (i = 0; i < 2; i++) {
1889
tx[i] = (double) ((int) (z));
1890
z = (z - tx[i]) * two24;
1894
while (tx[nx - 1] == zero)
1895
nx--; /* skip zero term */
1896
n = __kernel_rem_pio2(tx, y, (int) exp, nx);
1897
//System.out.println("KERNEL");
1898
//System.out.println("tx "+tx[0]+" "+tx[1]+" "+tx[2]);
1899
//System.out.println("y "+y[0]+" "+y[1]);
1900
//System.out.println("exp "+(int)exp+" nx "+nx+" n "+n);
1910
* __kernel_rem_pio2(x,y,e0,nx)
1911
* double x[],y[]; int e0,nx; int two_over_pi[];
1913
* __kernel_rem_pio2 return the last three digits of N with
1915
* so that |y| < pi/2.
1917
* The method is to compute the integer (mod 8) and fraction parts of
1918
* (2/pi)*x without doing the full multiplication. In general we
1919
* skip the part of the product that are known to be a huge integer
1920
* (more accurately, = 0 mod 8 ). Thus the number of operations are
1921
* independent of the exponent of the input.
1923
* (2/pi) is represented by an array of 24-bit integers in two_over_pi[].
1926
* x[] The input value (must be positive) is broken into nx
1927
* pieces of 24-bit integers in double precision format.
1928
* x[i] will be the i-th 24 bit of x. The scaled exponent
1929
* of x[0] is given in input parameter e0 (i.e., x[0]*2^e0
1930
* match x's up to 24 bits.
1932
* Example of breaking a double positive z into x[0]+x[1]+x[2]:
1937
* z = (z-x[i])*2**24
1940
* y[] ouput result in an array of double precision numbers.
1941
* The dimension of y[] is:
1942
* 24-bit precision 1
1943
* 53-bit precision 2
1944
* 64-bit precision 2
1945
* 113-bit precision 3
1946
* The actual value is the sum of them. Thus for 113-bit
1947
* precison, one may have to do something like:
1949
* long double t,w,r_head, r_tail;
1950
* t = (long double)y[2] + (long double)y[1];
1951
* w = (long double)y[0];
1953
* r_tail = w - (r_head - t);
1955
* e0 The exponent of x[0]
1957
* nx dimension of x[]
1959
* prec an integer indicating the precision:
1960
* 0 24 bits (single)
1961
* 1 53 bits (double)
1962
* 2 64 bits (extended)
1966
* integer array, contains the (24*i)-th to (24*i+23)-th
1967
* bit of 2/pi after binary point. The corresponding
1970
* two_over_pi[i] * 2^(-24(i+1)).
1972
* External function:
1973
* double scalbn(), floor();
1976
* Here is the description of some local variables:
1978
* jk jk+1 is the initial number of terms of two_over_pi[] needed
1979
* in the computation. The recommended value is 2,3,4,
1980
* 6 for single, double, extended,and quad.
1982
* jz local integer variable indicating the number of
1983
* terms of two_over_pi[] used.
1987
* jv index for pointing to the suitable two_over_pi[] for the
1988
* computation. In general, we want
1989
* ( 2^e0*x[0] * two_over_pi[jv-1]*2^(-24jv) )/8
1990
* is an integer. Thus
1991
* e0-3-24*jv >= 0 or (e0-3)/24 >= jv
1992
* Hence jv = max(0,(e0-3)/24).
1994
* jp jp+1 is the number of terms in PIo2[] needed, jp = jk.
1996
* q[] double array with integral value, representing the
1997
* 24-bits chunk of the product of x and 2/pi.
1999
* q0 the corresponding exponent of q[0]. Note that the
2000
* exponent for q[i] would be q0-24*i.
2002
* PIo2[] double precision array, obtained by cutting pi/2
2003
* into 24 bits chunks.
2005
* f[] two_over_pi[] in floating point
2007
* iq[] integer array by breaking up q[] in 24-bits chunk.
2009
* fq[] final product of x*(2/pi) in fq[0],..,fq[jk]
2011
* ih integer. If >0 it indicates q[] is >= 0.5, hence
2012
* it also indicates the *sign* of the result.
2017
* The hexadecimal values are the intended ones for the following
2018
* constants. The decimal values may be used, provided that the
2019
* compiler will convert from decimal to binary accurately enough
2020
* to produce the hexadecimal values shown.
2022
static final private double[] PIo2 = {
2023
0x1.921fb4p0, /* 1.57079625129699707031e+00 */
2024
0x1.4442dp-24, /* 7.54978941586159635335e-08 */
2025
0x1.846988p-48, /* 5.39030252995776476554e-15 */
2026
0x1.8cc516p-72, /* 3.28200341580791294123e-22 */
2027
0x1.01b838p-96, /* 1.27065575308067607349e-29 */
2028
0x1.a25204p-120, /* 1.22933308981111328932e-36 */
2029
0x1.382228p-145, /* 2.73370053816464559624e-44 */
2030
0x1.9f31dp-169 /* 2.16741683877804819444e-51 */
2032
static final private double twon24 = 0x1.0p-24; /* 5.96046447753906250000e-08 */
2034
static private int __kernel_rem_pio2(double[] x, double[] y, int e0, int nx) {
2050
double[] f = new double[20];
2051
double[] q = new double[20];
2052
double[] fq = new double[20];
2053
int[] iq = new int[20];
2059
/* determine jx,jv,q0, note that 3>q0 */
2065
q0 = e0 - (24 * (jv + 1));
2067
/* set up f[0] to f[jx+jk] where f[jx+jk] = two_over_pi[jv+jk] */
2070
for (i = 0; i <= m; i++, j++)
2071
f[i] = ((j < 0) ? zero : (double) two_over_pi[j]);
2073
/* compute q[0],q[1],...q[jk] */
2074
for (i = 0; i <= jk; i++) {
2075
for (j = 0, fw = 0.0; j <= jx; j++)
2076
fw += (x[j] * f[(jx + i) - j]);
2082
while (true) { // recompute:
2084
/* distill q[] into iq[] reversingly */
2085
for (i = 0, j = jz, z = q[jz]; j > 0; i++, j--) {
2086
fw = (double) ((int) (twon24 * z));
2087
iq[i] = (int) (z - (two24 * fw));
2092
z = scalbn(z, q0); /* actual value of z */
2093
z -= (8.0 * floor(z * 0.125)); /* trim off integer >= 8 */
2097
if (q0 > 0) { /* need iq[jz-1] to determine n */
2098
i = (iq[jz - 1] >> (24 - q0));
2100
iq[jz - 1] -= (i << (24 - q0));
2101
ih = iq[jz - 1] >> (23 - q0);
2102
} else if (q0 == 0) {
2103
ih = iq[jz - 1] >> 23;
2104
} else if (z >= 0.5) {
2107
if (ih > 0) { /* q > 0.5 */
2110
for (i = 0; i < jz; i++) { /* compute 1-q */
2115
iq[i] = 0x1000000 - j;
2118
iq[i] = 0xffffff - j;
2121
if (q0 > 0) { /* rare case: chance is 1 in 12 */
2124
iq[jz - 1] &= 0x7fffff;
2127
iq[jz - 1] &= 0x3fffff;
2134
z -= scalbn(one, q0);
2139
/* check if recomputation is needed */
2142
for (i = jz - 1; i >= jk; i--)
2144
if (j == 0) { /* need recomputation */
2145
for (k = 1; iq[jk - k] == 0; k++)
2146
; /* k = no. of terms needed */for (i = jz + 1;
2147
i <= (jz + k); i++) { /* add q[jz+1] to q[jz+k] */
2148
f[jx + i] = (double) two_over_pi[jv + i];
2149
for (j = 0, fw = 0.0; j <= jx; j++)
2150
fw += (x[j] * f[(jx + i) - j]);
2154
continue; //goto recompute;
2160
/* chop off zero terms */
2164
while (iq[jz] == 0) {
2168
} else { /* break z into 24-bit if necessary */
2171
fw = (double) ((int) (twon24 * z));
2172
iq[jz] = (int) (z - (two24 * fw));
2181
/* convert integer "bit" chunk to floating-point value */
2182
fw = scalbn(one, q0);
2183
for (i = jz; i >= 0; i--) {
2184
q[i] = fw * (double) iq[i];
2188
/* compute PIo2[0,...,jp]*q[jz,...,0] */
2189
for (i = jz; i >= 0; i--) {
2190
for (fw = 0.0, k = 0; (k <= jp) && (k <= (jz - i)); k++)
2191
fw += (PIo2[k] * q[i + k]);
2195
/* compress fq[] into y[] */
2197
for (i = jz; i >= 0; i--)
2199
y[0] = (ih == 0) ? fw : (-fw);
2201
for (i = 1; i <= jz; i++)
2203
y[1] = ((ih == 0) ? fw : (-fw));
2207
static final private double[] bp = { 1.0, 1.5, };
2208
static final private double[] dp_h = {
2210
}; /* 5.84962487220764160156e-01 */
2211
static final private double[] dp_l = {
2212
0.0, 0x1.cfdeb43cfd006p-27
2213
}; /* 1.35003920212974897128e-08 */
2214
static final private double two53 = 0x1.0p53; /* 9007199254740992.0 */
2216
/* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */
2217
static final private double L1 = 0x1.3333333333303p-1; /* 5.99999999999994648725e-01 */
2218
static final private double L2 = 0x1.b6db6db6fabffp-2; /* 4.28571428578550184252e-01 */
2219
static final private double L3 = 0x1.55555518f264dp-2; /* 3.33333329818377432918e-01 */
2220
static final private double L4 = 0x1.17460a91d4101p-2; /* 2.72728123808534006489e-01 */
2221
static final private double L5 = 0x1.d864a93c9db65p-3; /* 2.30660745775561754067e-01 */
2222
static final private double L6 = 0x1.a7e284a454eefp-3; /* 2.06975017800338417784e-01 */
2223
static final private double lg2 = 0x1.62e42fefa39efp-1; /* 6.93147180559945286227e-01 */
2224
static final private double lg2_h = 0x1.62e43p-1; /* 6.93147182464599609375e-01 */
2225
static final private double lg2_l = -1.90465429995776804525e-09; /* 0xbe205c610ca86c39 */
2226
static final private double ovt = 8.0085662595372944372e-17; /* -(1024-log2(ovfl+.5ulp)) */
2227
static final private double cp = 0x1.ec709dc3a03fdp-1; /* 9.61796693925975554329e-01 = 2/(3ln2) */
2228
static final private double cp_h = 0x1.ec709ep-1; /* 9.61796700954437255859e-01 = (float)cp */
2229
static final private double cp_l = -0x1.e2fe0145b01f5p-28; /* -7.02846165095275826516e-09 = tail of cp_h*/
2230
static final private double ivln2 = 0x1.71547652b82fep0; /* 1.44269504088896338700e+00 = 1/ln2 */
2231
static final private double ivln2_h = 0x1.715476p0; /* 1.44269502162933349609e+00 = 24b 1/ln2*/
2232
static final private double ivln2_l = 0x1.4ae0bf85ddf44p-26; /* 1.92596299112661746887e-08 = 1/ln2 tail*/
2235
* Returns x to the power y
2238
* Method: Let x = 2 * (1+f)
2239
* 1. Compute and return log2(x) in two pieces:
2240
* log2(x) = w1 + w2,
2241
* where w1 has 53-24 = 29 bit trailing zeros.
2242
* 2. Perform y*log2(x) = n+y' by simulating muti-precision
2243
* arithmetic, where |y'|<=0.5.
2244
* 3. Return x**y = 2**n*exp(y'*log2)
2247
* 1. (anything) ** 0 is 1
2248
* 2. (anything) ** 1 is itself
2249
* 3. (anything) ** NAN is NAN
2250
* 4. NAN ** (anything except 0) is NAN
2251
* 5. +-(|x| > 1) ** +INF is +INF
2252
* 6. +-(|x| > 1) ** -INF is +0
2253
* 7. +-(|x| < 1) ** +INF is +0
2254
* 8. +-(|x| < 1) ** -INF is +INF
2255
* 9. +-1 ** +-INF is NAN
2256
* 10. +0 ** (+anything except 0, NAN) is +0
2257
* 11. -0 ** (+anything except 0, NAN, odd integer) is +0
2258
* 12. +0 ** (-anything except 0, NAN) is +INF
2259
* 13. -0 ** (-anything except 0, NAN, odd integer) is +INF
2260
* 14. -0 ** (odd integer) = -( +0 ** (odd integer) )
2261
* 15. +INF ** (+anything except 0,NAN) is +INF
2262
* 16. +INF ** (-anything except 0,NAN) is +0
2263
* 17. -INF ** (anything) = -0 ** (-anything)
2264
* 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
2265
* 19. (-anything except 0 and inf) ** (non-integer) is NAN
2268
* pow(x,y) returns x**y nearly rounded. In particular
2269
* pow(integer,integer)
2270
* always returns the correct integer provided it is
2275
* Returns x to the power y.
2276
* @param x The base.
2277
* @param y The exponent.
2278
* @return x to the power y.
2280
static public double pow(double x, double y) {
2312
ix = hx & 0x7fffffff;
2313
iy = hy & 0x7fffffff;
2315
/* y==zero: x**0 = 1 */
2316
if ((iy | ly) == 0) {
2320
/* +-NaN return x+y */
2321
if ((ix > 0x7ff00000) || ((ix == 0x7ff00000) && (lx != 0)) ||
2322
(iy > 0x7ff00000) || ((iy == 0x7ff00000) && (ly != 0))) {
2326
/* determine if y is an odd int when x < 0
2327
* yisint = 0 ... y is not an integer
2328
* yisint = 1 ... y is an odd int
2329
* yisint = 2 ... y is an even int
2333
if (iy >= 0x43400000) {
2334
yisint = 2; /* even integer y */
2335
} else if (iy >= 0x3ff00000) {
2336
k = (iy >> 20) - 0x3ff; /* exponent */
2338
j = ly >>> (52 - k);
2339
if ((j << (52 - k)) == ly) {
2340
yisint = 2 - (j & 1);
2342
} else if (ly == 0) {
2344
if ((j << (20 - k)) == iy) {
2345
yisint = 2 - (j & 1);
2351
/* special value of y */
2353
if (iy == 0x7ff00000) { /* y is +-inf */
2354
if (((ix - 0x3ff00000) | lx) == 0) {
2355
return y - y; /* inf**+-1 is NaN */
2356
} else if (ix >= 0x3ff00000) { /* (|x|>1)**+-inf = inf,0 */
2357
return (hy >= 0) ? y : zero;
2358
} else { /* (|x|<1)**-,+inf = inf,0 */
2359
return (hy < 0) ? (-y) : zero;
2362
if (iy == 0x3ff00000) { /* y is +-1 */
2369
if (hy == 0x40000000) {
2370
return x * x; /* y is 2 */
2372
if (hy == 0x3fe00000) { /* y is 0.5 */
2373
if (hx >= 0) { /* x >= +0 */
2381
/* special value of x */
2383
if ((ix == 0x7ff00000) || (ix == 0) || (ix == 0x3ff00000)) {
2384
z = ax; /*x is +-0,+-inf,+-1*/
2386
z = one / z; /* z = (1/|x|) */
2389
if (((ix - 0x3ff00000) | yisint) == 0) {
2390
z = (z - z) / (z - z); /* (-1)**non-int is NaN */
2391
} else if (yisint == 1) {
2392
z = -z; /* (x<0)**odd = -(|x|**odd) */
2399
/* (x<0)**(non-int) is NaN */
2400
if ((((hx >> 31) + 1) | yisint) == 0) {
2401
return (x - x) / (x - x);
2405
if (iy > 0x41e00000) { /* if |y| > 2**31 */
2406
if (iy > 0x43f00000) { /* if |y| > 2**64, must o/uflow */
2407
if (ix <= 0x3fefffff) {
2408
return ((hy < 0) ? (huge * huge) : (tiny * tiny));
2410
if (ix >= 0x3ff00000) {
2411
return ((hy > 0) ? (huge * huge) : (tiny * tiny));
2415
/* over/underflow if x is not close to one */
2416
if (ix < 0x3fefffff) {
2417
return ((hy < 0) ? (huge * huge) : (tiny * tiny));
2419
if (ix > 0x3ff00000) {
2420
return ((hy > 0) ? (huge * huge) : (tiny * tiny));
2423
/* now |1-x| is tiny <= 2**-20, suffice to compute
2424
log(x) by x-x^2/2+x^3/3-x^4/4 */
2425
t = x - 1; /* t has 20 trailing zeros */
2426
w = (t * t) * (0.5 - (t * (0.3333333333333333333333 - (t * 0.25))));
2427
u = ivln2_h * t; /* ivln2_h has 21 sig. bits */
2428
v = (t * ivln2_l) - (w * ivln2);
2440
/* take care subnormal number */
2441
if (ix < 0x00100000) {
2446
n += (((ix) >> 20) - 0x3ff);
2447
j = ix & 0x000fffff;
2449
/* determine interval */
2450
ix = j | 0x3ff00000; /* normalize ix */
2452
k = 0; /* |x|<sqrt(3/2) */
2453
} else if (j < 0xBB67A) {
2454
k = 1; /* |x|<sqrt(3) */
2462
/* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
2463
u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
2464
v = one / (ax + bp[k]);
2467
s_h = setLO(s_h, 0);
2469
/* t_h=ax+bp[k] High */
2471
t_h = setHI(t_h, ((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18));
2472
t_l = ax - (t_h - bp[k]);
2473
s_l = v * ((u - (s_h * t_h)) - (s_h * t_l));
2475
/* compute log(ax) */
2479
(s2 * (L3 + (s2 * (L4 + (s2 * (L5 + (s2 * L6))))))))));
2480
r += (s_l * (s_h + s));
2483
t_h = setLO(t_h, 0);
2484
t_l = r - ((t_h - 3.0) - s2);
2486
/* u+v = s*(1+...) */
2488
v = (s_l * t_h) + (t_l * s);
2490
/* 2/(3log2)*(s+...) */
2492
p_h = setLO(p_h, 0);
2493
p_l = v - (p_h - u);
2494
z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */
2495
z_l = (cp_l * p_h) + (p_l * cp) + dp_l[k];
2497
/* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */
2499
t1 = (((z_h + z_l) + dp_h[k]) + t);
2501
t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
2504
s = one; /* s (sign of result -ve**odd) = -1 else = 1 */
2505
if ((((hx >> 31) + 1) | (yisint - 1)) == 0) {
2506
s = -one; /* (-ve)**(odd int) */
2509
/* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
2512
p_l = ((y - y1) * t1) + (y * t2);
2517
if (j >= 0x40900000) { /* z >= 1024 */
2518
if (((j - 0x40900000) | i) != 0) { /* if z > 1024 */
2519
return s * huge * huge; /* overflow */
2521
if ((p_l + ovt) > (z - p_h)) {
2522
return s * huge * huge; /* overflow */
2525
} else if ((j & 0x7fffffff) >= 0x4090cc00) { /* z <= -1075 */
2526
if (((j - 0xc090cc00) | i) != 0) { /* z < -1075 */
2527
return s * tiny * tiny; /* underflow */
2529
if (p_l <= (z - p_h)) {
2530
return s * tiny * tiny; /* underflow */
2536
* compute 2**(p_h+p_l)
2539
k = (i >> 20) - 0x3ff;
2541
if (i > 0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */
2542
n = j + (0x00100000 >> (k + 1));
2543
k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */
2545
t = setHI(t, (n & ~(0x000fffff >> k)));
2546
n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
2555
v = ((p_l - (t - p_h)) * lg2) + (t * lg2_l);
2559
t1 = z - (t * (P1 + (t * (P2 + (t * (P3 + (t * (P4 + (t * P5)))))))));
2560
r = ((z * t1) / (t1 - 2.0)) - (w + (z * w));
2564
if ((j >> 20) <= 0) {
2565
z = scalbn(z, n); /* subnormal output */
2575
* copysign(double x, double y)
2576
* copysign(x,y) returns a value with the magnitude of x and
2577
* with the sign bit of y.
2579
static private double copysign(double x, double y) {
2580
long ix = Double.doubleToRawLongBits(x);
2581
long iy = Double.doubleToRawLongBits(y);
2582
ix = (0x7fffffffffffffffL & ix) | (0x8000000000000000L & iy);
2583
return Double.longBitsToDouble(ix);
2586
static private final double two54 = 0x1.0p54; /* 1.80143985094819840000e+16 */
2587
static private final double twom54 = 0x1.0p-54; /* 5.55111512312578270212e-17 */
2590
* scalbn (double x, int n)
2591
* scalbn(x,n) returns x* 2**n computed by exponent
2592
* manipulation rather than by actually performing an
2593
* exponentiation or a multiplication.
2595
static private double scalbn(double x, int n) {
2601
k = (hx & 0x7ff00000) >> 20; /* extract exponent */
2602
if (k == 0) { /* 0 or subnormal x */
2603
if ((lx | (hx & 0x7fffffff)) == 0) {
2608
k = ((hx & 0x7ff00000) >> 20) - 54;
2610
return tiny * x; /*underflow*/
2614
return x + x; /* NaN or Inf */
2618
return huge * copysign(huge, x); /* overflow */
2623
return setHI(x, (hx & 0x800fffff) | (k << 20));
2626
if (n > 50000) { /* in case integer overflow in n+k */
2627
return huge * copysign(huge, x); /*overflow*/
2630
return tiny * copysign(tiny, x); /*underflow*/
2632
k += 54; /* subnormal result */
2633
return twom54 * setHI(x, (hx & 0x800fffff) | (k << 20));
2636
static private double set(int newHiPart, int newLowPart) {
2637
return Double.longBitsToDouble((((long) newHiPart) << 32) | newLowPart);
2640
static private double setLO(double x, int newLowPart) {
2641
long lx = Double.doubleToRawLongBits(x);
2642
lx &= 0xFFFFFFFF00000000L;
2644
return Double.longBitsToDouble(lx);
2647
static private double setHI(double x, int newHiPart) {
2648
long lx = Double.doubleToRawLongBits(x);
2649
lx &= 0x00000000FFFFFFFFL;
2650
lx |= (((long) newHiPart) << 32);
2651
return Double.longBitsToDouble(lx);
2654
static private int __HI(double x) {
2655
return (int) (0xFFFFFFFF & (Double.doubleToRawLongBits(x) >> 32));
2658
static private int __LO(double x) {
2659
return (int) (0xFFFFFFFF & Double.doubleToRawLongBits(x));
b'\\ No newline at end of file'