/
any-num.pir
210 lines (154 loc) Β· 3.14 KB
/
any-num.pir
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
## $Id$
=head1 NAME
src/builtins/any_num.pir - C<Num>-like functions and methods for C<Any>
=head1 DESCRIPTION
This file implements the methods and functions of C<Any> that
are most closely associated with the C<Num> class or role.
We place them here instead of F<src/classes/Any.pir> to keep
the size of that file down and to emphasize their generic,
"built-in" nature.
=head2 Methods
=over 4
=cut
.namespace []
.sub 'onload' :anon :init :load
$P0 = get_hll_namespace ['Any']
'!EXPORT'('abs,cis,int,log,chr,polar,sqrt,truncate,unpolar', 'from'=>$P0)
## pre-seed a random number generator
$P0 = new 'Random'
set_hll_global ['Any'], '$!random', $P0
srand()
.end
=item abs()
=cut
.namespace ['Any']
.sub 'abs' :method :multi(_)
$N0 = self
$N1 = abs $N0
.return ($N1)
.end
=item chr()
=cut
.namespace ['Any']
.sub 'chr' :method :multi(_)
$I0 = self
$S0 = chr $I0
.return ($S0)
.end
=item cis($angle)
=cut
.namespace ['Any']
.sub 'cis' :method :multi(_)
.tailcall 'unpolar'(1.0, self)
.end
.sub 'int' :method :multi(_)
.tailcall self.'truncate'()
.end
=item log
our Num multi Num::log ( Num $x: Num :$base )
our Num multi Math::Basic::log ( Num $x, Num :$base )
Logarithm of base C<$base>, default Natural. Calling with C<$x == 0> is an
error.
=cut
.sub 'log' :method :multi(_)
$N0 = self
$N1 = ln $N0
.return ($N1)
.end
=item polar
=cut
.namespace ['Any']
.sub 'polar' :method :multi(_)
$N0 = self
.tailcall 'list'($N0, 0)
.end
=item rand()
=cut
.namespace []
.sub 'rand'
.param pmc x :slurpy
## 0-argument test, RT#56366
unless x goto no_args
die "too many arguments passed - 0 params expected"
no_args:
$P0 = get_hll_global ['Any'], '$!random'
$N0 = $P0
.return ($N0)
.end
.namespace ['Any']
.sub 'rand' :method
$N0 = self
$P0 = get_hll_global ['Any'], '$!random'
$N1 = $P0
$N0 *= $N1
.return ($N0)
.end
=item sqrt()
=cut
.namespace ['Any']
.sub 'sqrt' :method :multi(_)
$N0 = self
$N1 = sqrt $N0
.return ($N1)
.end
=item srand()
=cut
.namespace []
.sub 'srand'
.param num seed :optional
.param int has_seed :opt_flag
if has_seed goto have_seed
seed = time
have_seed:
$P0 = get_hll_global ['Any'], '$!random'
$I0 = seed
$P0 = $I0
.return ()
.end
.namespace ['Any']
.sub 'srand' :method
$N0 = self
$I0 = $N0
$P0 = get_hll_global ['Any'], '$!random'
$P0 = $I0
.return ()
.end
=item truncate()
=item int
=cut
.namespace ['Any']
.sub 'truncate' :method :multi(_)
$N0 = self
if $N0 == 0 goto done
if $N0 < 0 goto num_ceil
floor $N0
goto done
num_ceil:
ceil $N0
done:
$I0 = $N0
.return ($I0)
.end
=item unpolar($angle)
=cut
.sub 'unpolar' :method
.param num angle
.local num mag
.local pmc result
mag = self
result = new 'Complex'
$N0 = cos angle
$N0 *= mag
result[0] = $N0
$N0 = sin angle
$N0 *= mag
result[1] = $N0
.return (result)
.end
=back
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: