-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.ts
45 lines (40 loc) · 1.02 KB
/
math.ts
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
import { gcd, normalize, number, rational } from './core';
export function add(x: rational, y: rational): rational {
const n = gcd(x[1], y[1]);
// console.log({x, y, n});
const b = x[1] === n ? y[1] : x[1];
let a: number;
if (x[1] === b) {
/* times up y */
a = y[0] * (b / y[1]) + x[0];
} else {
/* times up x */
a = x[0] * (b / x[1]) + y[0];
}
return [a, b];
}
export function minus(x: rational, y: rational): rational {
return add(x, [-y[0], y[1]]);
}
export function multiply(x: rational, y: rational): rational {
return [x[0] * y[0], x[1] * y[1]];
}
export function divide(x: rational, y: rational): rational {
return multiply(x, [y[1], y[0]]);
}
export function pow(x: rational, y: rational): rational {
y = normalize(y);
if (y[0] === 0) {
return [1, 1];
}
if (y[1] === 1 && y[0] > 0) {
let [a, b] = x;
const [aa, bb] = x;
for (let n = y[0]; n > 0; n--) {
a *= aa;
b *= bb;
}
return [a, b];
}
return rational(Math.pow(number(x), number(y)));
}