/
Numeric.pm
310 lines (246 loc) · 8.27 KB
/
Numeric.pm
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
role Numeric {
method ACCEPTS($other) {
if self.isNaN {
$other.isNaN;
} else {
$other == self;
}
}
method Numeric() {
self;
}
# NOTE: Real is defined as failing if the number in
# question is not convertable to Real, eg a Complex
# with a non-zero imaginary part.
method Real() {
note "$.WHAT() needs a version of .Real";
fail "$.WHAT() needs a version of .Real";
}
method Int() {
self.Real.Int;
}
method Rat(::Real $epsilon = 1.0e-6) {
self.Real.Rat($epsilon);
}
method Num() {
self.Real.Num;
}
method Str() {
note "$.WHAT() needs a version of .Str";
fail "$.WHAT() needs a version of .Str";
}
method reals() {
note "$.WHAT() needs a version of .reals";
fail "$.WHAT() needs a version of .reals";
}
method isNaN() {
?(self.reals.grep({.isNaN}));
}
method succ(Numeric $x:) {
$x + 1;
}
method pred(Numeric $x:) {
$x - 1;
}
method abs(Numeric $x:) {
note "$.WHAT() needs a version of .abs";
fail "$.WHAT() needs a version of .abs";
}
method exp(Numeric $exponent: Numeric $base = e) {
note "$.WHAT() needs a version of .exp";
fail "$.WHAT() needs a version of .exp";
}
method ln(Numeric $x:) {
note "$.WHAT() needs a version of .ln";
fail "$.WHAT() needs a version of .ln";
}
method log(Numeric $x: Numeric $base = e) {
$x.ln / $base.ln;
}
method log10(Numeric $x:) {
self.log(10);
}
method sqrt(Numeric $x:) {
note "$.WHAT() needs a version of .sqrt";
fail "$.WHAT() needs a version of .sqrt";
}
method roots(Numeric $x: Int $n) {
note "$.WHAT() needs a version of .roots";
fail "$.WHAT() needs a version of .roots";
}
INIT {
our @trig-base-conversions = (1.0, pi / 180.0, pi / 200.0, 2.0 * pi);
}
# Used by the :Trig subs and methods in the Int and Num classes.
method to-radians(Numeric $x: $base) {
$x * pir::get_global__Ps('@trig-base-conversions')[$base];
}
method from-radians(Numeric $x: $base) {
$x / pir::get_global__Ps('@trig-base-conversions')[$base];
}
method sign(Numeric $x:) {
note "sign is only defined for Reals, you have a $.WHAT()";
fail "sign is only defined for Reals, you have a $.WHAT()";
}
method floor(Numeric $x:) {
note "floor is only defined for Reals, you have a $.WHAT()";
fail "floor is only defined for Reals, you have a $.WHAT()";
}
method ceiling(Numeric $x:) {
note "ceiling is only defined for Reals, you have a $.WHAT()";
fail "ceiling is only defined for Reals, you have a $.WHAT()";
}
method truncate(Numeric $x:) {
note "truncate is only defined for Reals, you have a $.WHAT()";
fail "truncate is only defined for Reals, you have a $.WHAT()";
}
method round(Numeric $x: $scale = 1) {
note "round is only defined for Reals, you have a $.WHAT()";
fail "round is only defined for Reals, you have a $.WHAT()";
}
method cis(Numeric $angle:) {
note "cis is only defined for Reals, you have a $.WHAT()";
fail "cis is only defined for Reals, you have a $.WHAT()";
}
method unpolar(Numeric $mag: Numeric $angle) {
note "unpolar is only defined for Reals, you have a $.WHAT()";
fail "unpolar is only defined for Reals, you have a $.WHAT()";
}
method rand(Numeric $x:) {
$x.Real.rand;
}
method sin(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .sin";
fail "$.WHAT() needs a version of .sin";
}
method asin(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .asin";
fail "$.WHAT() needs a version of .asin";
}
method cos(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .cos";
fail "$.WHAT() needs a version of .cos";
}
method acos(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .acos";
fail "$.WHAT() needs a version of .acos";
}
method tan(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .tan";
fail "$.WHAT() needs a version of .tan";
}
method atan(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .atan";
fail "$.WHAT() needs a version of .atan";
}
method sec(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .sec";
fail "$.WHAT() needs a version of .sec";
}
method asec(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .asec";
fail "$.WHAT() needs a version of .asec";
}
method cosec(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .cosec";
fail "$.WHAT() needs a version of .cosec";
}
method acosec(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .acosec";
fail "$.WHAT() needs a version of .acosec";
}
method cotan(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .cotan";
fail "$.WHAT() needs a version of .cotan";
}
method acotan(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .acotan";
fail "$.WHAT() needs a version of .acotan";
}
method sinh(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .sinh";
fail "$.WHAT() needs a version of .sinh";
}
method asinh(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .asinh";
fail "$.WHAT() needs a version of .asinh";
}
method cosh(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .cosh";
fail "$.WHAT() needs a version of .cosh";
}
method acosh(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .acosh";
fail "$.WHAT() needs a version of .acosh";
}
method tanh(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .tanh";
fail "$.WHAT() needs a version of .tanh";
}
method atanh(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .atanh";
fail "$.WHAT() needs a version of .atanh";
}
method sech(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .sech";
fail "$.WHAT() needs a version of .sech";
}
method asech(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .asech";
fail "$.WHAT() needs a version of .asech";
}
method cosech(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .cosech";
fail "$.WHAT() needs a version of .cosech";
}
method acosech(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .acosech";
fail "$.WHAT() needs a version of .acosech";
}
method cotanh(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .cotanh";
fail "$.WHAT() needs a version of .cotanh";
}
method acotanh(Numeric $x: $base = Radians) {
note "$.WHAT() needs a version of .acotanh";
fail "$.WHAT() needs a version of .acotanh";
}
method atan2(Numeric $y: Numeric $x = 1, $base = Radians) {
note "atan2 is only defined for Reals, you have a $.WHAT()";
fail "atan2 is only defined for Reals, you have a $.WHAT()";
}
}
multi sub postfix:<i>(Numeric $z) {
$z * 1i;
}
multi sub infix:<cmp>(Numeric $a, Numeric $b) {
my @a = $a.reals;
my @b = $b.reals;
@a.push(0 xx +@b - +@a) if (+@a < +@b);
@b.push(0 xx +@a - +@b) if (+@b < +@a);
[||] (@a Z<=> @b);
}
multi sub infix:«<=>»(Numeric $a, Numeric $b) {
$a cmp $b;
}
multi sub infix:«==»(Numeric $a, Numeric $b) {
($a <=> $b) == 0;
}
multi sub infix:«!=»(Numeric $a, Numeric $b) {
($a <=> $b) != 0;
}
multi sub infix:«<»(Numeric $a, Numeric $b) {
($a <=> $b) < 0;
}
multi sub infix:«>»(Numeric $a, Numeric $b) {
($a <=> $b) > 0;
}
multi sub infix:«<=»(Numeric $a, Numeric $b) {
($a <=> $b) <= 0;
}
multi sub infix:«>=»(Numeric $a, Numeric $b) {
($a <=> $b) >= 0;
}
multi sub srand(Numeric $seed) {
srand($seed.Real);
}