Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 285 lines (167 sloc) 6.945 kb
d27427e S16: Added a few attributes to trees
wayland authored
1
2 =encoding utf8
3
04840a3 [Spec] treat all authors equally
lwall authored
4 =head1 TITLE
d27427e S16: Added a few attributes to trees
wayland authored
5
a323a5c S16/S32: Moved Temporal and Tree stuff from S16 to S32
wayland authored
6 DRAFT: Synopsis 32: Setting Library - Numeric
d27427e S16: Added a few attributes to trees
wayland authored
7
04840a3 [Spec] treat all authors equally
lwall authored
8 =head1 AUTHORS
9
10 Rod Adams <rod@rodadams.net>
11 Larry Wall <larry@wall.org>
12 Aaron Sherman <ajs@ajs.com>
13 Mark Stosberg <mark@summersault.com>
14 Carl Mäsak <cmasak@gmail.com>
15 Moritz Lenz <moritz@faui2k3.org>
16 Tim Nelson <wayland@wayland.id.au>
17
18 =head1 VERSION
19
20 Created: 19 Mar 2009 extracted from S29-functions.pod
21
9349eb2 [S32] clean up some numeric spec fossils
lwall authored
22 Last Modified: 1 Sep 2009
23 Version: 3
d27427e S16: Added a few attributes to trees
wayland authored
24
25 The document is a draft.
26
9d5a38d P6 Synopsis : ws changes - remove trailing spaces
Darren_Duncan authored
27 If you read the HTML version, it is generated from the pod in the pugs
d27427e S16: Added a few attributes to trees
wayland authored
28 repository under /docs/Perl6/Spec/S32-setting-library/Numeric.pod so edit it there in
29 the SVN repository if you would like to make changes.
30
9d5a38d P6 Synopsis : ws changes - remove trailing spaces
Darren_Duncan authored
31 This documents Bit, Int, Num, Rat, Complex, and Bool.
bba26cb Numeric.pod: Added some notes on what needs to be documented here
wayland authored
32
33 XXX So where are Bit, Int, and Rat
34
d27427e S16: Added a few attributes to trees
wayland authored
35 =head1 Function Packages
36
37 =head2 Bool
38
39 =over 4
40
41 =item succ
42
43 our Bool multi method succ ( Bool $b: ) is export
44
45 Returns C<Bool::True>.
46
47 =item pred
48
49 our Bool multi method pred ( Bool $b: ) is export
50
51 Returns C<Bool::False>.
52
53 =back
54
55 =head2 Num
56
57 The following are all defined in the C<Num> role:
58
59 B<API document>: L<Num>
60
9349eb2 [S32] clean up some numeric spec fossils
lwall authored
61 C<Num> provides some constants in addition to the basic
62 mathematical functions.
d27427e S16: Added a few attributes to trees
wayland authored
63
9349eb2 [S32] clean up some numeric spec fossils
lwall authored
64 constant pi is export = 3.14159_26535_89793_23846_26433_83279_50288;
65 constant e is export = 2.71828_18284_59045_23536_02874_71352_66249;
d27427e S16: Added a few attributes to trees
wayland authored
66
67 =over
68
69 =item succ
70
71 our Num multi method succ ( Num $x: ) is export
a68ae91 Fix typo in Numeric
leto authored
72 our Int multi method succ ( Int $x: ) is export
d27427e S16: Added a few attributes to trees
wayland authored
73
74 Returns the successor of C<$x>. This method is used by C<< prefix:<++> >> and
75 C<< postfix:<++> >> to increment the value in a container.
76
77 =item pred
78
79 our Num multi method pred ( Num $x: ) is export
80 our Int multi method pred ( Int $x: ) is export
81
82 Returns the predeccessor of C<$x>. This method is used by C<< prefix:<--> >>
83 and C<< postfix:<--> >> to decrement the value in a container.
84
85 =item abs
86
87 our Num multi method abs ( Num $x: ) is export
88
89 Absolute Value.
90
91 =item floor
92
93 our Int multi method floor ( Num $x: ) is export
94
95 Returns the highest integer not greater than C<$x>.
96
97 =item ceiling
98
99 our Int multi method ceiling ( Num $x: ) is export
100
101 Returns the lowest integer not less than C<$x>.
102
103 =item round
104
105 our Int multi method round ( Num $x: ) is export
106
107 Returns the nearest integer to C<$x>. The algorithm is C<floor($x + 0.5)>.
108 (Other rounding algorithms will be given extended names beginning with "round".)
109
110 =item truncate
111
112 our Int multi method truncate ( Num $x: ) is export
113
114 Returns the closest integer to C<$x> whose absolute value is not greater
115 than the absolute value of C<$x>. (In other words, just chuck any
9349eb2 [S32] clean up some numeric spec fossils
lwall authored
116 fractional part.) This is the default rounding function used by
117 implicit integer conversions.
118
119 You may also truncate using explicit integer casts, either C<Int()> for
120 an arbitrarily large integers, or C<int()> for native integers.
d27427e S16: Added a few attributes to trees
wayland authored
121
122 =item exp
123
124 our Num multi method exp ( Num $exponent: Num :$base = Num::e ) is export
125
126 Performs similar to C<$base ** $exponent>. C<$base> defaults to the
127 constant I<e>.
128
129 =item log
130
54e1a1a [S32/Num] log's base is positional
moritz authored
131 our Num multi method log ( Num $x: Num $base = Num::e ) is export
d27427e S16: Added a few attributes to trees
wayland authored
132
133 Logarithm of base C<$base>, default Natural. Calling with C<$x == 0> is an
134 error.
135
136 =item log10
137
138 our Num multi method log10 (Num $x:) is export
139
140 A base C<10> logarithm, othewise identical to C<log>.
141
142 =item rand
143
144 our Num term:<rand>
145
9349eb2 [S32] clean up some numeric spec fossils
lwall authored
146 Pseudo random number in range C<< 0 ..^ 1 >>. That is, C<0> is
147 theoretically possible, while C<1> is not. Note that there is no
148 unary C<rand> function in Perl 6, so just multiply C<rand> by your
149 desired multiplier. For picking a random integer you probably want
150 to use something like C<(1..6).pick> instead.
d27427e S16: Added a few attributes to trees
wayland authored
151
152 =item sign
153
154 our Int multi method sign ( Num $x: ) is export
155
156 Returns 1 when C<$x> is greater than 0, -1 when it is less than 0, 0 when it
157 is equal to 0, or undefined when the value passed is undefined.
158
159 =item srand
160
161 multi method srand ( Num $seed: )
162 multi srand ( Num $seed = default_seed_algorithm())
163
164 Seed the generator C<rand> uses. C<$seed> defaults to some combination
165 of various platform dependent characteristics to yield a non-deterministic seed.
166 Note that you get one C<srand()> for free when you start a Perl program, so
167 you I<must> call C<srand()> yourself if you wish to specify a deterministic seed
168 (or if you wish to be differently nondeterministic).
169
170 =item sqrt
171
172 our Num multi method sqrt ( Num $x: ) is export
173
174 Returns the square root of the parameter.
175
176 =item roots
177
178 (in Num) method roots (Num $x: Int $n --> List of Num) is export
179
180 Returns a list of all C<$n>th (complex) roots of C<$x>
181
182 =item cis
183
184 our Complex multi method cis (Num $angle:) is export
185
186 Returns 1.unpolar($angle)
187
188 =item unpolar
189
190 our Complex multi method unpolar (Num $mag: Num $angle) is export
191
192 Returns a complex number specified in polar coordinates. Angle is in radians.
193
194 =item i
195
9349eb2 [S32] clean up some numeric spec fossils
lwall authored
196 our Num multi postfix:<i> ( Num $x )
d27427e S16: Added a few attributes to trees
wayland authored
197
198 Returns a complex number representing the parameter multiplied by the imaginary
9349eb2 [S32] clean up some numeric spec fossils
lwall authored
199 unit C<i>. Note that there is no C<.i> method. To follow a variable name
200 with the postfix, it's necessary to use a backslash or parentheses:
201
202 $land\i
203 ($land)i
d27427e S16: Added a few attributes to trees
wayland authored
204
205 =back
206
207
208 =head2 Complex
209
210 =over 4
211
212 =item polar
213
214 our Seq multi method polar (Complex $nim:) is export
215
216 Returns (magnitude, angle) corresponding to the complex number.
217 The magnitude is non-negative, and the angle in the range C<-π ..^ π>.
218
5f2db9e Changed real to re, and imaginary to im, as perl moritz_++ suggestion
wayland authored
219 =item re
3f4315f Added real/imaginary to Complex, and true/not to Object
wayland authored
220
5f2db9e Changed real to re, and imaginary to im, as perl moritz_++ suggestion
wayland authored
221 method re() {...}
3f4315f Added real/imaginary to Complex, and true/not to Object
wayland authored
222
9d5a38d P6 Synopsis : ws changes - remove trailing spaces
Darren_Duncan authored
223 Returns the real part of the complex number.
3f4315f Added real/imaginary to Complex, and true/not to Object
wayland authored
224
5f2db9e Changed real to re, and imaginary to im, as perl moritz_++ suggestion
wayland authored
225 =item im
3f4315f Added real/imaginary to Complex, and true/not to Object
wayland authored
226
5f2db9e Changed real to re, and imaginary to im, as perl moritz_++ suggestion
wayland authored
227 method im() {...}
3f4315f Added real/imaginary to Complex, and true/not to Object
wayland authored
228
9d5a38d P6 Synopsis : ws changes - remove trailing spaces
Darren_Duncan authored
229 Returns the imaginary part of a complex number.
3f4315f Added real/imaginary to Complex, and true/not to Object
wayland authored
230
d27427e S16: Added a few attributes to trees
wayland authored
231 =back
232
f674f3b [Numeric] kill :Trig tag, always include trig functions.
lwall authored
233 =head2 Trigonometric functions
d27427e S16: Added a few attributes to trees
wayland authored
234
f674f3b [Numeric] kill :Trig tag, always include trig functions.
lwall authored
235 The following are also defined in C<Num>. The trig functions
236 depend on the current (lexically scoped) trig base:
237
238 enum TrigBase is export <Radians Degrees Gradians Circles>;
239 constant $?TRIGBASE = Radians;
d27427e S16: Added a few attributes to trees
wayland authored
240
241 =over 4
242
243 =item I<Standard Trig Functions>
244
f674f3b [Numeric] kill :Trig tag, always include trig functions.
lwall authored
245 Num multi method func ( Num $x: TrigBase $base = $?TRIGBASE ) is export
d27427e S16: Added a few attributes to trees
wayland authored
246
247 where I<func> is one of:
248 sin, cos, tan, asin, acos, atan, sec, cosec, cotan, asec, acosec,
249 acotan, sinh, cosh, tanh, asinh, acosh, atanh, sech, cosech, cotanh,
250 asech, acosech, acotanh.
251
252 Performs the various trigonometric functions.
253
254 Option C<$base> is used to declare how you measure your angles.
255 Given the value of an arc representing a single full revolution.
256
f674f3b [Numeric] kill :Trig tag, always include trig functions.
lwall authored
257 $base Subdivisions of circle
258 ---- ----------------------
259 Radians 2*pi
260 Degrees 360
261 Gradians 400
262 Circles 1
d27427e S16: Added a few attributes to trees
wayland authored
263
f674f3b [Numeric] kill :Trig tag, always include trig functions.
lwall authored
264 To change the base within your own lexical scope, it suffices to redefine the
265 compiler constant:
d27427e S16: Added a few attributes to trees
wayland authored
266
f674f3b [Numeric] kill :Trig tag, always include trig functions.
lwall authored
267 constant $?TRIGBASE = Degrees;
d27427e S16: Added a few attributes to trees
wayland authored
268
269 =item atan2
270
f1054bb [S32/Numeric] correct parameter name in atan2 method form
skids authored
271 our Num multi method atan2 ( Num $y: Num $x = 1 )
d27427e S16: Added a few attributes to trees
wayland authored
272 our Num multi atan2 ( Num $y, Num $x = 1 )
273
274 This second form of C<atan> computes the arctangent of C<$y/$x>, and takes
275 the quadrant into account. Otherwise behaves as other trigonometric functions.
276
277 =back
278
279 =head1 Additions
280
281 Please post errors and feedback to perl6-language. If you are making
282 a general laundry list, please separate messages by topic.
f674f3b [Numeric] kill :Trig tag, always include trig functions.
lwall authored
283
284 =for vim:set expandtab sw=4:
Something went wrong with that request. Please try again.