-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixed.l
57 lines (47 loc) · 1.38 KB
/
fixed.l
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
(class +Fixed)
# value scale
(dm T (Value Scale)
(=: value Value)
(=: scale Scale) )
(de fixed (Value Scale)
(default Value 0 Scale 0)
(new '(+Fixed) Value Scale) )
(dm format> ()
(format (: value) (: scale)) )
(dm scale-up> (ScaleInc)
(if (gt0 ScaleInc)
(scale-up> (fixed (* (: value) 10) (inc (: scale))) (dec ScaleInc))
This) )
(dm scale-down> (ScaleDec)
(if (gt0 ScaleDec)
(scale-down> (fixed (/ (: value) 10) (dec (: scale))) (dec ScaleDec))
This) )
(dm normalize> ()
(cond
((=0 (: scale)) This)
((=0 (: value)) (fixed 0 0))
((=0 (% (: value) 10))
(normalize>
(fixed (/ (: value) 10) (dec (: scale)))))
(T This) ) )
(de fixed+ (A B)
(let (First (> (get A 'scale) (get B 'scale))
Larger (if First A B)
Smaller (ifn First A B)
LargerScale (get Larger 'scale)
ScaleDiff (- LargerScale (get Smaller 'scale))
ScaledSmaller (scale-up> Smaller ScaleDiff) )
(normalize>
(fixed
(+ (get Larger 'value) (get ScaledSmaller 'value))
LargerScale) ) ) )
(de fixed- (A B)
(ifn B
(fixed (- (get A 'value)) (get A 'scale))
(fixed+ A (fixed- B)) ) )
(de fixed* (A B)
(normalize>
(fixed
(* (get A 'value) (get B 'value))
(+ (get A 'scale) (get B 'scale)) ) ) )
(de fixed-square (A) (fixed* A A))