-
Notifications
You must be signed in to change notification settings - Fork 0
/
FNumber.ooc
79 lines (62 loc) · 1.32 KB
/
FNumber.ooc
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
//this class can use numbers as fractions =)
FNumber: class {
up,down: Int //upper and lower part of the fraction
init: func {
init(0,1)
}
init: func ~vals(=up,=down) {
}
init: func ~val(=up) {
down = 1
}
print: func {
if(down == 1)
printf("%d",up)
else
printf("%d/%d",up,down)
}
reduce: func {
if ( up % down == 0 ) {
up /= down
down = 1
}
}
}
operator +(f1,f2: FNumber) -> FNumber {
if(f1 down == f2 down) {
return FNumber new(f1 up + f2 up,f1 down)
}
ret := FNumber new(f1 up * f2 down + f2 up * f1 down,f1 down * f2 down)
if ( ret up % ret down == 0 ) {
ret up /= ret down
}
return ret
}
operator -(f1,f2: FNumber) -> FNumber {
if(f1 down == f2 down) {
return FNumber new(f1 up - f2 up,f1 down)
}
ret := FNumber new(f1 up * f2 down - f2 up * f1 down,f1 down * f2 down)
ret reduce()
return ret
}
operator *(f1,f2: FNumber) -> FNumber {
ret := FNumber new(f1 up * f2 up,f1 down * f2 down)
ret reduce()
return ret
}
operator /(f1,f2: FNumber) -> FNumber {
ret := FNumber new(f1 up * f2 down,f1 down * f2 up)
ret reduce()
return ret
}
operator *(f1: FNumber, n: Int) -> FNumber {
ret := FNumber new(f1 up * n,f1 down)
ret reduce()
return ret
}
operator /(f1: FNumber, n: Int) -> FNumber {
ret := FNumber new(f1 up, f1 down * n)
ret reduce()
return ret
}