-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.1.1 Example
39 lines (33 loc) · 866 Bytes
/
2.1.1 Example
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
//2.1.1 Example
function add_rat(x,y) {
return make_rat(numer(x)*denom(y) + numer(y)*denom(x),
denom(x) * denom(y));
}
function sub_rat(x,y) {
return make_rat(numer(x)*denom(y) - numer(y)*denom(x),
denom(x) * denom(y));
}
function mul_rat(x,y) {
return make_rat(numer(x)*numer(y),
denom(x)*denom(y));
}
function div_rat(x,y) {
return make_rat(numer(x)*denom(y),
denom(x)*numer(y));
}
function equal_rat(x,y) {
return numer(x)*denom(y) === numer(y)*denom(x);
}
function make_rat(n,d) {
var g = gcd(n,d);
return pair(n / g, d / g);
}
function gcd(a,b) {
return b === 0 ? a : gcd(b, a % b);
}
function print_rat(x) {
return print(numer(x) + "/" + denom(x));
}
var one_half = make_rat(1,2);
var one_third = make_rat(1,3);
print_rat(add_rat(one_third,one_third));