1 | import { isInteger, log2, log10, cbrt, expm1, sign, toFixed, log1p } from '../../utils/number';
|
2 | var n1 = 'number';
|
3 | var n2 = 'number, number';
|
4 | export function absNumber(a) {
|
5 | return Math.abs(a);
|
6 | }
|
7 | absNumber.signature = n1;
|
8 | export function addNumber(a, b) {
|
9 | return a + b;
|
10 | }
|
11 | addNumber.signature = n2;
|
12 | export function subtractNumber(a, b) {
|
13 | return a - b;
|
14 | }
|
15 | subtractNumber.signature = n2;
|
16 | export function multiplyNumber(a, b) {
|
17 | return a * b;
|
18 | }
|
19 | multiplyNumber.signature = n2;
|
20 | export function divideNumber(a, b) {
|
21 | return a / b;
|
22 | }
|
23 | divideNumber.signature = n2;
|
24 | export function unaryMinusNumber(x) {
|
25 | return -x;
|
26 | }
|
27 | unaryMinusNumber.signature = n1;
|
28 | export function unaryPlusNumber(x) {
|
29 | return x;
|
30 | }
|
31 | unaryPlusNumber.signature = n1;
|
32 | export function cbrtNumber(x) {
|
33 | return cbrt(x);
|
34 | }
|
35 | cbrtNumber.signature = n1;
|
36 | export function ceilNumber(x) {
|
37 | return Math.ceil(x);
|
38 | }
|
39 | ceilNumber.signature = n1;
|
40 | export function cubeNumber(x) {
|
41 | return x * x * x;
|
42 | }
|
43 | cubeNumber.signature = n1;
|
44 | export function expNumber(x) {
|
45 | return Math.exp(x);
|
46 | }
|
47 | expNumber.signature = n1;
|
48 | export function expm1Number(x) {
|
49 | return expm1(x);
|
50 | }
|
51 | expm1Number.signature = n1;
|
52 | export function fixNumber(x) {
|
53 | return x > 0 ? Math.floor(x) : Math.ceil(x);
|
54 | }
|
55 | fixNumber.signature = n1;
|
56 | export function floorNumber(x) {
|
57 | return Math.floor(x);
|
58 | }
|
59 | floorNumber.signature = n1;
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | export function gcdNumber(a, b) {
|
68 | if (!isInteger(a) || !isInteger(b)) {
|
69 | throw new Error('Parameters in function gcd must be integer numbers');
|
70 | }
|
71 |
|
72 |
|
73 | var r;
|
74 |
|
75 | while (b !== 0) {
|
76 | r = a % b;
|
77 | a = b;
|
78 | b = r;
|
79 | }
|
80 |
|
81 | return a < 0 ? -a : a;
|
82 | }
|
83 | gcdNumber.signature = n2;
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | export function lcmNumber(a, b) {
|
92 | if (!isInteger(a) || !isInteger(b)) {
|
93 | throw new Error('Parameters in function lcm must be integer numbers');
|
94 | }
|
95 |
|
96 | if (a === 0 || b === 0) {
|
97 | return 0;
|
98 | }
|
99 |
|
100 |
|
101 |
|
102 | var t;
|
103 | var prod = a * b;
|
104 |
|
105 | while (b !== 0) {
|
106 | t = b;
|
107 | b = a % t;
|
108 | a = t;
|
109 | }
|
110 |
|
111 | return Math.abs(prod / a);
|
112 | }
|
113 | lcmNumber.signature = n2;
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | export function logNumber(x) {
|
121 | return Math.log(x);
|
122 | }
|
123 | logNumber.signature = n1;
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | export function log10Number(x) {
|
131 | return log10(x);
|
132 | }
|
133 | log10Number.signature = n1;
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | export function log2Number(x) {
|
141 | return log2(x);
|
142 | }
|
143 | log2Number.signature = n1;
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | export function log1pNumber(x) {
|
151 | return log1p(x);
|
152 | }
|
153 | log1pNumber.signature = n1;
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | export function modNumber(x, y) {
|
163 | if (y > 0) {
|
164 |
|
165 |
|
166 |
|
167 | return x - y * Math.floor(x / y);
|
168 | } else if (y === 0) {
|
169 | return x;
|
170 | } else {
|
171 |
|
172 |
|
173 | throw new Error('Cannot calculate mod for a negative divisor');
|
174 | }
|
175 | }
|
176 | modNumber.signature = n2;
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | export function nthRootNumber(a, root) {
|
186 | var inv = root < 0;
|
187 |
|
188 | if (inv) {
|
189 | root = -root;
|
190 | }
|
191 |
|
192 | if (root === 0) {
|
193 | throw new Error('Root must be non-zero');
|
194 | }
|
195 |
|
196 | if (a < 0 && Math.abs(root) % 2 !== 1) {
|
197 | throw new Error('Root must be odd when a is negative.');
|
198 | }
|
199 |
|
200 |
|
201 | if (a === 0) {
|
202 | return inv ? Infinity : 0;
|
203 | }
|
204 |
|
205 | if (!isFinite(a)) {
|
206 | return inv ? 0 : a;
|
207 | }
|
208 |
|
209 | var x = Math.pow(Math.abs(a), 1 / root);
|
210 |
|
211 |
|
212 | x = a < 0 ? -x : x;
|
213 | return inv ? 1 / x : x;
|
214 |
|
215 |
|
216 |
|
217 | |
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | }
|
235 | nthRootNumber.signature = n2;
|
236 | export function signNumber(x) {
|
237 | return sign(x);
|
238 | }
|
239 | signNumber.signature = n1;
|
240 | export function sqrtNumber(x) {
|
241 | return Math.sqrt(x);
|
242 | }
|
243 | sqrtNumber.signature = n1;
|
244 | export function squareNumber(x) {
|
245 | return x * x;
|
246 | }
|
247 | squareNumber.signature = n1;
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 | export function xgcdNumber(a, b) {
|
257 |
|
258 | var t;
|
259 |
|
260 | var q;
|
261 |
|
262 | var r;
|
263 |
|
264 | var x = 0;
|
265 | var lastx = 1;
|
266 | var y = 1;
|
267 | var lasty = 0;
|
268 |
|
269 | if (!isInteger(a) || !isInteger(b)) {
|
270 | throw new Error('Parameters in function xgcd must be integer numbers');
|
271 | }
|
272 |
|
273 | while (b) {
|
274 | q = Math.floor(a / b);
|
275 | r = a - q * b;
|
276 | t = x;
|
277 | x = lastx - q * x;
|
278 | lastx = t;
|
279 | t = y;
|
280 | y = lasty - q * y;
|
281 | lasty = t;
|
282 | a = b;
|
283 | b = r;
|
284 | }
|
285 |
|
286 | var res;
|
287 |
|
288 | if (a < 0) {
|
289 | res = [-a, -lastx, -lasty];
|
290 | } else {
|
291 | res = [a, a ? lastx : 0, lasty];
|
292 | }
|
293 |
|
294 | return res;
|
295 | }
|
296 | xgcdNumber.signature = n2;
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 | export function powNumber(x, y) {
|
305 |
|
306 |
|
307 | if (x * x < 1 && y === Infinity || x * x > 1 && y === -Infinity) {
|
308 | return 0;
|
309 | }
|
310 |
|
311 | return Math.pow(x, y);
|
312 | }
|
313 | powNumber.signature = n2;
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 | export function roundNumber(value) {
|
323 | var decimals = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
324 | return parseFloat(toFixed(value, decimals));
|
325 | }
|
326 | roundNumber.signature = n2;
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 | export function normNumber(x) {
|
334 | return Math.abs(x);
|
335 | }
|
336 | normNumber.signature = n1; |
\ | No newline at end of file |