tagged release 0.7.1
[parrot.git] / languages / perl6 / src / builtins / any-num.pir
blob72e445b32bc1eb5bb3fb31edd082c9866a76a545
1 ## $Id$
3 =head1 NAME
5 src/builtins/any_num.pir -  C<Num>-like functions and methods for C<Any>
7 =head1 DESCRIPTION
9 This file implements the methods and functions of C<Any> that
10 are most closely associated with the C<Num> class or role.
11 We place them here instead of F<src/classes/Any.pir> to keep
12 the size of that file down and to emphasize their generic,
13 "built-in" nature.
15 =head2 Methods
17 =over 4
19 =cut
21 .namespace []
22 .sub 'onload' :anon :init :load
23     $P0 = get_hll_namespace ['Any']
24     '!EXPORT'('abs cis int log polar sqrt truncate unpolar', 'from'=>$P0)
26     ##  pre-seed a random number generator
27     $P0 = new 'Random'
28     set_hll_global ['Any'], '$!random', $P0
29     srand()
30 .end
33 =item abs()
35 =cut
37 .namespace ['Any']
38 .sub 'abs' :method :multi(_)
39     $N0 = self
40     $N1 = abs $N0
41     .return ($N1)
42 .end
45 =item cis($angle)
47 =cut
49 .namespace ['Any']
50 .sub 'cis' :method :multi(_)
51     .return 'unpolar'(1.0, self)
52 .end
55 .sub 'int' :method :multi(_)
56     .return self.'truncate'()
57 .end
59 =item log
61  our Num multi Num::log         ( Num $x: Num :$base )
62  our Num multi Math::Basic::log ( Num $x, Num :$base )
64 Logarithm of base C<$base>, default Natural. Calling with C<$x == 0> is an
65 error.
67 =cut
69 .sub 'log' :method :multi(_)
70     $N0 = self
71     $N1 = ln $N0
72     .return ($N1)
73 .end
76 =item polar
78 =cut
80 .namespace ['Any']
81 .sub 'polar' :method :multi(_)
82     $N0 = self
83     .return 'list'($N0, 0)
84 .end
87 =item rand()
89 =cut
91 .namespace []
92 .sub 'rand'
93     .param pmc x               :slurpy
94     ## 0-argument test, RT#56366
95     unless x goto no_args
96     die "too many arguments passed - 0 params expected"
97   no_args:
98     $P0 = get_hll_global ['Any'], '$!random'
99     $N0 = $P0
100     .return ($N0)
101 .end
103 .namespace ['Any']
104 .sub 'rand' :method
105     $N0 = self
106     $P0 = get_hll_global ['Any'], '$!random'
107     $N1 = $P0
108     $N0 *= $N1
109     .return ($N0)
110 .end
113 =item sqrt()
115 =cut
117 .namespace ['Any']
118 .sub 'sqrt' :method :multi(_)
119     $N0 = self
120     $N1 = sqrt $N0
121     .return ($N1)
122 .end
125 =item srand()
127 =cut
129 .namespace []
130 .sub 'srand'
131     .param num seed            :optional
132     .param int has_seed        :opt_flag
133     if has_seed goto have_seed
134     seed = time
135   have_seed:
136     $P0 = get_hll_global ['Any'], '$!random'
137     $I0 = seed
138     $P0 = $I0
139     .return ()
140 .end
142 .namespace ['Any']
143 .sub 'srand' :method
144     $N0 = self
145     $I0 = $N0
146     $P0 = get_hll_global ['Any'], '$!random'
147     $P0 = $I0
148     .return ()
149 .end
152 =item truncate()
154 =item int
156 =cut
158 .namespace ['Any']
159 .sub 'truncate' :method :multi(_)
160     $N0 = self
161     if $N0 == 0 goto done
162     if $N0 < 0 goto num_ceil
163     floor $N0
164     goto done
165   num_ceil:
166     ceil $N0
167   done:
168     $I0 = $N0
169     .return ($I0)
170 .end
173 =item unpolar($angle)
175 =cut
177 .sub 'unpolar' :method
178     .param num angle
179     .local num mag
180     .local pmc result
181     mag = self
182     result = new 'Complex'
183     $N0 = cos angle
184     $N0 *= mag
185     result[0] = $N0
186     $N0 = sin angle
187     $N0 *= mag
188     result[1] = $N0
189     .return (result)
190 .end
195 =back
197 =cut
199 # Local Variables:
200 #   mode: pir
201 #   fill-column: 100
202 # End:
203 # vim: expandtab shiftwidth=4 ft=pir: