/
Int.pm
73 lines (58 loc) · 1.6 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
# XXX Should be my...
our sub upgrade_to_num_if_needed($test) {
Q:PIR {
.local num test
$P0 = find_lex '$test'
test = $P0
if test > 2147483647.0 goto upgrade
if test < -2147483648.0 goto upgrade
$I0 = test
.return ($I0)
upgrade:
.return (test)
}
}
augment class Int does Real {
multi method ACCEPTS(Int $other) {
self == $other;
}
multi method ACCEPTS($other) {
self.Num.ACCEPTS($other);
}
method Bridge() {
self.Num;
}
our Bool multi method Bool() { self != 0 ?? Bool::True !! Bool::False }
our Int method Int() { self; }
our Int method Rat(Real $epsilon = 1.0e-6) { self / 1; }
our Num method Num() {
pir::box__PN(pir::set__NP(self));
}
# Next has been moved to Rat.pm for the moment.
# our Rat multi method Rat() { Rat.new(self, 1); }
}
our multi sub infix:<+>(Int $a, Int $b) {
upgrade_to_num_if_needed(pir::add__NNN($a, $b))
}
our multi sub infix:<->(Int $a, Int $b) {
upgrade_to_num_if_needed(pir::sub__NNN($a, $b))
}
our multi sub infix:<*>(Int $a, Int $b) {
upgrade_to_num_if_needed(pir::mul__NNN($a, $b))
}
our multi sub infix:<div>(Int $a, Int $b) {
pir::box__PI(pir::div__III($a, $b))
}
our multi sub infix:<%>(Int $a, Int $b) {
upgrade_to_num_if_needed(pir::mod__NNN($a, $b))
}
our multi sub infix:<**>(Int $a, Int $b) {
if $b >= 0 {
upgrade_to_num_if_needed(pir::pow__NNN($a, $b))
} else {
pir::pow__NNN($a, $b)
}
}
our multi sub prefix:<->(Int $a) {
upgrade_to_num_if_needed(pir::neg__NN($a))
}