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