-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (72 loc) · 1.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var DP1 = 7.853981554508209228515625E-1;
var DP2 = 7.94662735614792836714E-9;
var DP3 = 3.06161699786838294307E-17;
var P = [-13093.6939181,1153516.64839,-17956525.1976];
var Q = [13681.2963471,-1320892.3444,25008380.1823,-53869575.5929];
var lossth = 1.073741824e9;
var ldexp = function(mantissa, exponent) {
return exponent > 1023 // avoid multiplying by infinity
? mantissa * Math.pow(2, 1023) * Math.pow(2, exponent - 1023)
: exponent < -1074 // avoid multiplying by zero
? mantissa * Math.pow(2, -1074) * Math.pow(2, exponent + 1074)
: mantissa * Math.pow(2, exponent);
}
var polevl = function(x, p) {
var ans = p[0];
for (var i = 1; i < p.length; i++) {
ans = ans * x + p[i];
}
return ans;
}
var p1evl = function(x, p) {
var ans = x + p[0];
for (var i = 1; i < p.length; i++) {
ans = ans * x + p[i];
}
return ans;
}
exports.tan = function(xx) {
if (xx == 0) {
return 0;
}
var x, y, z, zz, j, sign;
if( xx < 0 ) {
x = -xx;
sign = false;
} else {
x = xx;
sign = true;
}
if ((x % (Math.PI/2)) < 1e-18 ) {
return Number.NaN;
}
if( x > lossth ) {
throw "Holy crap this won't work";
}
// compute x modulus PI/4
y = Math.floor( x/(Math.PI/4) );
z = ldexp(y, -3);
z = Math.floor(z);
z = y - ldexp( z, 3 );
/* integer and fractional part modulo one octant */
j = (z | 0);
/* map zeros and singularities to origin */
if( j & 1 ) {
j += 1;
y += 1.0;
}
z = ((x - y * DP1) - y * DP2) - y * DP3;
zz = z * z;
if( zz > 1.0e-14 ) {
y = z + z * (zz * polevl( zz, P, 2 )/p1evl(zz, Q, 4));
} else {
y = z;
}
if( j & 2 ) {
y = -1.0/y;
}
if (!sign) {
y = -y;
}
return y;
}