-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
186 lines (152 loc) · 3.28 KB
/
utils.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Custom implementation of mathematical functions
const PI = 3.141592653589793;
const initialPrecision = 1e-4;
function abs(x) {
return x >= 0 ? x : -x;
}
function factorial(n) {
if (n === 0 || n === 1) return 1;
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}
function sqrt(x) {
if (x === 0 || x === 1) return x;
let guess = x / 2; // initial guess
// iterate until the guess is close enough to the square root
while (abs(guess * guess - x) > initialPrecision) {
guess = (guess + x / guess) / 2;
}
return guess.toFixed();
}
function pow(base, exponent) {
if (exponent === 0) return 1;
else if (exponent > 0) {
let result = 1;
for (let i = 0; i < exponent; i++) {
result *= base;
}
return result;
} else {
let result = 1;
for (let i = 0; i > exponent; i--) {
result /= base;
}
return result;
}
}
function cos(x, precision = initialPrecision) {
x = x % (2 * PI); // Ensure x is within one period (from 0 to 2PI)
let result = 1;
let delta = 1;
let i = 2;
let sign = -1;
while (abs(delta) > precision) {
delta *= pow(x, 2) / (factorial(i) * factorial(i - 1));
result += sign * delta;
sign *= -1;
i += 2;
}
return result;
}
function acos(x, precision = initialPrecision) {
// handle values outside [-1, 1]
if (x < -1 || x > 1) return NaN;
let result = PI / 2; // initial guess for x within [-1, 1]
let delta = 1;
let i = 1;
let sign = 1;
while (abs(delta) > precision) {
delta *=
(factorial(2 * i - 1) /
(pow(2, 2 * i - 1) * factorial(i) * factorial(i))) *
pow(x, 2 * i - 1);
result -= sign * delta;
sign *= -1;
i++;
}
return result;
}
function sin(x, precision = initialPrecision) {
// making sure x is within [0, 2PI], and posittive
while (x < 0) {
x += 2 * PI;
}
while (x >= 2 * PI) {
x -= 2 * PI;
}
let result = 0;
let delta = x;
let i = 1;
let sign = -1;
while (abs(delta) > precision) {
result += delta;
delta *= -(x * x) / (2 * i * (2 * i + 1));
i++;
sign *= -1;
}
return result;
}
function asin(x, precision = 1e-15) {
// making sure values are within [-1, 1]
if (x < -1 || x > 1) return NaN;
let result = x;
let delta = x;
let i = 1;
while (abs(delta) > precision) {
delta *=
(factorial(2 * i - 1) /
(pow(4, i) * factorial(i) * factorial(i) * (2 * i + 1))) *
pow(x, 2 * i + 1);
result += delta;
i++;
}
return result;
}
function tan(x, precision = initialPrecision) {
// making sure tath x is within [-PI/2, PI/2]
while (x < -PI / 2) {
x += PI;
}
while (x >= PI / 2) {
x -= PI;
}
let result = 0;
let delta = x;
let divisor = 1;
let numerator = x;
let sign = 1;
for (let i = 1; abs(delta) > precision; i++) {
result += delta;
divisor = 2 * i + 1;
numerator *= x * x;
delta = (sign * numerator) / divisor;
sign *= -1;
}
return result;
}
function atan(x, precision = 1e-15) {
let result = 0;
let delta = x;
let i = 1;
let sign = 1;
while (precision < abs(delta)) {
result += sign * delta;
delta *= (x * x) / (2 * i + 1);
sign *= -1;
i++;
}
return result;
}
module.exports = {
cos,
acos,
sin,
asin,
tan,
atan,
sqrt,
pow,
};