-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmath.lisp
184 lines (111 loc) · 2.75 KB
/
math.lisp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
(in-package :arc-compat.internal)
(in-readtable :common-lisp)
(in-suite arc-compat)
;;; Math
;;;============================================================================
;;; *
;;; -
;;; /
;;; expt
;;; mod
;;; rand
;;; sqrt
;;; trunc -> ac.lisp
(defalias number cl:numberp
"Tests if n is a number (int or num).")
(def positive (x)
"Tests if x is a number and is positive (>= 0). This fails on complex numbers."
(cl:and (number x)
(cl:plusp x)))
(tst positive
(== (positive 0)
nil )
(== (positive -2)
nil )
(== (positive "a")
nil ))
;;; abs
;;; round
(def roundup (n)
"Rounds n to the nearest value. Halfs are rounded away from zero."
(withs (base (trunc n) rem (abs (- n base)))
(if (>= rem 1/2)
(funcall (if (> n 0) #'+ #'-) base 1)
base)))
(tst roundup
(== (roundup 1/2)
1 )
(== (roundup 1.5)
2 )
(== (roundup 0.4)
0 )
(== (roundup -1.5)
-2 ))
(def to-nearest (n quantum)
"Rounds down to the nearest multiple of n."
(* (roundup (/ n quantum)) quantum))
(def nearest (n quantum)
"Rounds down to the nearest multiple of n. Was 'to-nearest' in arc0-2."
(* (roundup (/ n quantum)) quantum))
(tst nearest
(== (nearest 3.14d0 0.1d0)
3.1d0)
(== (nearest -5 10)
-10))
(def median (ns)
"Returns the median of the list (the element at the midpoint of the list when
sorted)."
(ref (sort #'> ns) (trunc (/ (len ns) 2))))
;;; tst :: median -> test-after
(def avg (ns)
"Computes the average of a list of numbers."
(/ (apply #'+ ns) (len ns)))
(tst avg
(== (avg (list #C(1 2) 0.4d0))
#C(0.7d0 1.0d0)))
(def even (obj)
"Tests if n is even. Argument n must be an integer."
(cl:evenp obj))
(def odd (obj)
"Tests if n is odd. Argument n must be an integer."
(cl:oddp obj))
(def best (f seq)
"Returns the 'best' element of list as determined by function compare."
(if (no seq)
nil
(let wins (car seq)
(each elt (cdr seq)
(if (funcall f elt wins) (= wins elt)))
wins)))
(tst best
(== (best #'> '() )
nil )
(== (best #'> '(3 1 4 5 9 6))
9)
(== (best #'> '("3" "1" "4" "5" "9" "6"))
"9"))
(def max args
"Returns the maximum arg. The args must be comparable with >."
(best #'> args))
(tst max
(== (max 10 -3 5)
10 )
(== (max "cat" "dog" "bird")
"dog" ))
(def min args
"Returns the minimum arg. The args must be comparable with <."
(best #'< args))
(tst min
(== (min 10 -3 5)
-3)
(== (min "cat" "dog" "bird")
"bird"))
(def multiple (x y)
"Tests if x is a multiple of base."
(is 0 (mod x y)))
(tst multiple
(== (multiple 10 5)
t)
(== (multiple 11 5)
nil))
;;; eof