-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogram.q
61 lines (52 loc) · 1.28 KB
/
program.q
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
use "lang/reflect" = reflect
use "lang/math" = math
class Real like Number {
num n
num d
constructor(this, num n, num d) {
this.n = n
this.d = d
print(this.n, this.d)
this.__valueUpdated()
}
constructor(this, num x) {
fraction = x % 1
mul = 10 ^ (string(fraction).size() - 2)
this.n = x * mul
this.d = mul
this.__valueUpdated()
this.optimize()
}
void __valueUpdated(this) {
reflect.setNumberValue(this, this.n / this.d)
}
void optimize(this) {
m = math.gcd(this.n, this.d)
this.n /= m
this.d /= m
}
override string(this) {
return string(this.n) + "/" + string(this.d)
}
override +(this, v) {
if v instanceof Real {
m = math.lcm(this.d, v.d)
this.n = this.n * (m / this.d) + v.n * (m / v.d)
this.d = m
} else if v instanceof Number {
this.n += v * this.d
} else {
throw UnsupportedOperationException() % {
"left" = this,
"right" = v,
"operation" = "+"
}
}
this.optimize()
this.__valueUpdated()
return this
}
}
a = Real(0.5)
b = Real(1, 3)
print(a + b)