tagged release 0.6.4
[parrot.git] / languages / perl6 / src / builtins / any-num.pir
blob1f6b3a3ba2aec721e99eab73d39d5e5bcbc5805f
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 polar sqrt truncate int 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 =item polar
57 =cut
59 .namespace ['Any']
60 .sub 'polar' :method :multi(_)
61     $N0 = self
62     .return 'list'($N0, 0)
63 .end
66 =item rand()
68 =cut
70 .namespace []
71 .sub 'rand'
72     .param pmc x               :slurpy
73     ## 0-argument test, RT#56366
74     unless x goto no_args
75     die "too many arguments passed - 0 params expected"
76   no_args:
77     $P0 = get_hll_global ['Any'], '$!random'
78     $N0 = $P0
79     .return ($N0)
80 .end
82 .namespace ['Any']
83 .sub 'rand' :method
84     $N0 = self
85     $P0 = get_hll_global ['Any'], '$!random'
86     $N1 = $P0
87     $N0 *= $N1
88     .return ($N0)
89 .end
92 =item sqrt()
94 =cut
96 .namespace ['Any']
97 .sub 'sqrt' :method :multi(_)
98     $N0 = self
99     $N1 = sqrt $N0
100     .return ($N1)
101 .end
104 =item srand()
106 =cut
108 .namespace []
109 .sub 'srand'
110     .param num seed            :optional
111     .param int has_seed        :opt_flag
112     if has_seed goto have_seed
113     seed = time
114   have_seed:
115     $P0 = get_hll_global ['Any'], '$!random'
116     $I0 = seed
117     $P0 = $I0
118     .return ()
119 .end
121 .namespace ['Any']
122 .sub 'srand' :method
123     $N0 = self
124     $I0 = $N0
125     $P0 = get_hll_global ['Any'], '$!random'
126     $P0 = $I0
127     .return ()
128 .end
131 =item truncate()
133 =item int
135 =cut
137 .namespace ['Any']
138 .sub 'truncate' :method :multi(_)
139     $N0 = self
140     if $N0 == 0 goto done
141     if $N0 < 0 goto num_ceil
142     floor $N0
143     goto done
144   num_ceil:
145     ceil $N0
146   done:
147     $I0 = $N0
148     .return ($I0)
149 .end
151 .sub 'int' :method :multi(_)
152     .return self.'truncate'()
153 .end
155 =item unpolar($angle)
157 =cut
159 .sub 'unpolar' :method
160     .param num angle
161     .local num mag
162     .local pmc result
163     mag = self
164     result = new 'Complex'
165     $N0 = cos angle
166     $N0 *= mag
167     result[0] = $N0
168     $N0 = sin angle
169     $N0 *= mag
170     result[1] = $N0
171     .return (result)
172 .end
175 =back
177 =cut
179 # Local Variables:
180 #   mode: pir
181 #   fill-column: 100
182 # End:
183 # vim: expandtab shiftwidth=4 ft=pir: