/
Int.pm
113 lines (98 loc) · 2.28 KB
/
Int.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
class Int is also {
multi method abs() {
Q:PIR {
$I0 = self
$I0 = abs $I0
%r = box $I0
}
}
our Int multi method Int() { self }
our Num multi method Num() {
Q:PIR {
$N0 = self
%r = box $N0
}
}
our Rat multi method Rat() { Rat.new(self, 1); }
our Complex multi method Complex() { Complex.new(self, 0); }
our Str multi method Str() {
~self;
}
# Most of the trig functions for Int are in Any-num.pm, but
# sec is a special case.
our Num multi method sec($base = 'radians') is export {
self.Num.sec($base);
}
our Complex multi method unpolar($angle) is export {
Complex.new(self.Num * $angle.cos("radians"), self.Num * $angle.sin("radians"));
}
}
multi sub abs(Int $x) { $x.abs }
multi sub infix:<+>(Int $a, Int $b) {
Q:PIR {
$P0 = find_lex '$a'
$N0 = $P0
$P1 = find_lex '$b'
$N1 = $P1
$N2 = $N0 + $N1
%r = '!upgrade_to_num_if_needed'($N2)
}
}
multi sub infix:<->(Int $a, Int $b) {
Q:PIR {
$P0 = find_lex '$a'
$N0 = $P0
$P1 = find_lex '$b'
$N1 = $P1
$N2 = $N0 - $N1
%r = '!upgrade_to_num_if_needed'($N2)
}
}
multi sub infix:<*>(Int $a, Int $b) {
Q:PIR {
$P0 = find_lex '$a'
$N0 = $P0
$P1 = find_lex '$b'
$N1 = $P1
$N2 = $N0 * $N1
%r = '!upgrade_to_num_if_needed'($N2)
}
}
multi sub infix:<div>(Int $a, Int $b) {
Q:PIR {
$P0 = find_lex '$a'
$I0 = $P0
$P1 = find_lex '$b'
$I1 = $P1
$I2 = $I0 / $I1
%r = box $I2
}
}
multi sub infix:<%>(Int $a, Int $b) {
Q:PIR {
$P0 = find_lex '$a'
$N0 = $P0
$P1 = find_lex '$b'
$N1 = $P1
$N2 = mod $N0, $N1
%r = '!upgrade_to_num_if_needed'($N2)
}
}
multi sub infix:<**>(Int $a, Int $b) {
Q:PIR {
$P0 = find_lex '$a'
$N0 = $P0
$P1 = find_lex '$b'
$N1 = $P1
$N2 = pow $N0, $N1
%r = '!upgrade_to_num_if_needed'($N2)
}
}
multi sub prefix:<->(Int $a) {
Q:PIR {
$P0 = find_lex '$a'
$N0 = $P0
$N0 = neg $N0
%r = '!upgrade_to_num_if_needed'($N0)
}
}