/
Any-num.pm
72 lines (64 loc) · 1.89 KB
/
Any-num.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
class Any is also {
our Int multi method ceiling() is export {
Q:PIR {
$N0 = self
$I0 = ceil $N0
%r = box $I0
}
}
our Str multi method chr() is export {
Q:PIR {
$I0 = self
$S0 = chr $I0
%r = 'prefix:~'($S0)
}
}
our Complex multi method cis() is export {
(1.0).unpolar(self)
}
our Int multi method floor() is export {
Q:PIR {
$N0 = self
$I0 = floor $N0
%r = box $I0
}
}
our Num method rand() {
Q:PIR {
$N0 = self
$N1 = rand $N0
%r = box $N1
}
}
our Int multi method round() is export {
Q:PIR {
$N0 = self
$N0 = $N0 + 0.5
$I0 = floor $N0
%r = box $I0
}
}
# Used by the :Trig subs and methods in the Int and Num classes.
our multi method !to-radians($base) {
given $base {
when /:i ^d/ { self * pi/180.0 } # Convert from degrees.
when /:i ^g/ { self * pi/200.0 } # Convert from gradians.
when /:i ^r/ { self } # Convert from radians.
when Num { self * 2.0 * pi } # Convert from revolutions.
default { die "Unable to convert to base: $base" }
}
}
our Num multi method !from-radians(Num $x, $base) {
given $base {
when /:i ^d/ { $x * 180/pi } # Convert to degrees.
when /:i ^g/ { $x * 200/pi } # Convert to gradians.
when /:i ^r/ { $x } # Convert to radians.
when Num { $x /(2 * pi) } # Convert to revolutions.
default { die "Unable to convert to base: $base" }
}
}
}
our Num sub rand (*@args) {
die "too many arguments passed - 0 params expected" if @args;
1.rand
}