=encoding utf8
=head1 TITLE
DRAFT: Synopsis 32: Setting Library - Numeric
=head1 AUTHORS
Rod Adams
Larry Wall
Aaron Sherman
Mark Stosberg
Carl Mäsak
Moritz Lenz
Tim Nelson
=head1 VERSION
Created: 19 Mar 2009 extracted from S29-functions.pod
Last Modified: 16 July 2010
Version: 10
The document is a draft.
If you read the HTML version, it is generated from the Pod in the specs
repository under
L
so edit it there in the git repository if you would like to make changes.
This documents Bit, Int, Numeric, Rat, Complex, and Bool.
XXX So where are Bit, Int, and Rat
=head1 Function Packages
=head2 Bool
=over 4
=item succ
our Bool multi method succ ( Bool $b: ) is export
Returns C.
=item pred
our Bool multi method pred ( Bool $b: ) is export
Returns C.
=back
=head2 Numeric
C is a role for everything that's a scalar number. So C, C,
C, C and other numeric types do that role. However it is an
abstract interface, so C<$number.WHAT> will never return C.
Users who provide their own scalar numeric types are encouraged to implement the
C role. It is intended that such types such support the basic arithmetic
operators to the extent possible, as well as C<==>. In addition, it is hoped that
comparison operators will at least return consistent results, even if there is no
sensible mathemtical ordering of your type. That allows functions like sort to
not choke and die if they are handed a value of your type. (See also the C
role for scalar numeric types that represent real numbers.)
The following are all defined in the C role:
C provides some constants in addition to the basic
mathematical functions.
constant pi is export = 3.14159_26535_89793_23846_26433_83279_50288;
constant e is export = 2.71828_18284_59045_23536_02874_71352_66249;
constant i is export = 1i;
=over
=item Real
our Real multi method Real ()
If this C is equivalent to a C, return that C.
(For instance, if this number is a C with a zero imaginary part.)
Fail otherwise.
=item Int
our Int multi method Int ()
If this C is equivalent to a C, return the equivalent of
calling C on that C to get an C.
=item Rat
our Rat multi method Rat ( Real $epsilon = 1.0e-6 )
If this C is equivalent to a C, return a C which is
within C<$epsilon> of that C's value.
=item Num
our Num multi method Num ()
If this C is equivalent to a C, return that C as a C
as accurately as is possible.
=item succ
our Numeric multi method succ ( Numeric $x: ) is export
our Int multi method succ ( Int $x: ) is export
Returns the successor of C<$x>. This method is used by C<< prefix:<++> >> and
C<< postfix:<++> >> to increment the value in a container.
=item pred
our Numeric multi method pred ( Numeric $x: ) is export
our Int multi method pred ( Int $x: ) is export
Returns the predecessor of C<$x>. This method is used by C<< prefix:<--> >>
and C<< postfix:<--> >> to decrement the value in a container.
=item abs
our Numeric multi method abs ( Numeric $x: ) is export
Absolute Value.
=item exp
our Numeric multi method exp ( Numeric $exponent: Numeric :$base = Num::e ) is export
Performs similar to C<$base ** $exponent>. C<$base> defaults to the
constant I.
=item log
our Numeric multi method log ( Numeric $x: Numeric $base = Num::e ) is export
Logarithm of base C<$base>, default Natural. Calling with C<$x == 0> is an
error.
=item log10
our Numeric multi method log10 (Numeric $x:) is export
A base C<10> logarithm, otherwise identical to C.
=item rand
our Num term:
Pseudo random number in range C<< 0 ..^ 1 >>. That is, C<0> is
theoretically possible, while C<1> is not. Note that there is no
unary C function in Perl 6, but there is a C method.
For picking a random integer you probably want
to use something like C<(1..6).pick> instead.
=item sqrt
our Numeric multi method sqrt ( Numeric $x: ) is export
Returns the principle square root of the parameter.
=item roots
method roots (Numeric $x: Int $n ) is export
Returns a list of all C<$n>th (complex) roots of C<$x>. Returns C if
C<< $n <= 0 >>, itself if C<$n == 0>, and is free to return a single C
if C<$x> is C or C, or in case of complex numbers if one of the
components is.
=item i
our Complex multi postfix:* ( Numeric $x )
Returns a complex number representing the parameter multiplied by the imaginary
unit C**. Note that there is no C<.i> method. To follow a variable name
with the postfix, it's necessary to use a backslash or parentheses:
$land\i
($land)i
=item to-radians
our Numeric multi method to-radians ( Numeric $x: TrigBase $base ) is export
Convert from C<$base> to radians.
=item from-radians
our Numeric multi method from-radians ( Numeric $x: TrigBase $base ) is export
Convert from radians to C<$base>.
=back
=head2 Real
role Real does Numeric;
C, like C, is an abstract role that represents the interface of
a real scalar number (i.e. neither C nor vector-like). For example
C, C, C and C implement the C role.
Users who provide their own scalar real numeric types are encouraged to implement the
C role. Because real numbers are strictly-totally-ordered and
C types try to emulate that property, it is desirable that any two
C types be mutally compatible, even if they are not aware of each other. The
current proposal requires you to define a C method in your C type, which
converts your type into a neutral C type by restating it in terms of the fundamental
Perl 6 types and calling C on them. This then makes the default C methods
and operators all work with your C type. While the name of this method may changed,
it is hoped that something like this will remain in the spec.
=over
=item Complex
our Complex multi method Complex ()
Returns a C whose real part is this C and whose imaginary part is 0.
=item Str
our Str multi method Str ()
Returns the C as a C. All built-in C types format it as a decimal
number, so for example, the C C<5/4> is returned as C<"1.2">.
=item floor
our Int multi method floor ( Real $x: ) is export
Returns the highest integer not greater than C<$x>.
=item ceiling
our Int multi method ceiling ( Real $x: ) is export
Returns the lowest integer not less than C<$x>.
=item round
our Int multi method round ( Real $x: $scale = 1) is export
Returns the nearest integer to C<$x>. The algorithm is:
floor($x / $scale + 0.5) * $scale
(Other rounding algorithms will be given extended names beginning with "round".)
Functions that round to a particular precision may easily be created with
currying:
constant &roundcents ::= &round.assuming(:scale(1/100));
=item truncate
our Int multi method truncate ( Real $x: ) is export
Returns the closest integer to C<$x> whose absolute value is not greater
than the absolute value of C<$x>. (In other words, just chuck any
fractional part.) This is the default rounding function used by
implicit integer conversions.
You may also truncate using explicit integer casts, either C for
an arbitrarily large integers, or C for native integers.
=item sign
our Int multi method sign ( Real $x: ) is export
Returns 1 when C<$x> is greater than 0, -1 when it is less than 0, 0 when it
is equal to 0, or undefined when the value passed is undefined.
=item srand
multi srand ( Real $seed = default_seed_algorithm())
Seed the generator C uses. C<$seed> defaults to some combination
of various platform dependent characteristics to yield a non-deterministic seed.
Note that you get one C for free when you start a Perl program, so
you I call C yourself if you wish to specify a deterministic seed
(or if you wish to be differently nondeterministic).
=item rand
our Num multi method rand (Real $x:) is export
Pseudo random number in range C<< 0 ..^ $x >>. That is, C<0> is
theoretically possible, while C<$x> is not. For picking a random integer
you probably want to use something like C<(1..6).pick> instead.
=item cis
our Complex multi method cis (Real $angle:) is export
Returns 1.unpolar($angle)
=item unpolar
our Complex multi method unpolar (Real $mag: Real $angle) is export
Returns a complex number specified in polar coordinates. Angle is in radians.
=back
=head2 Num
class Num does Real;
C is a machine-precision numeric real value.
=head2 Complex
C is an immutable type. Each C object stores two numbers,
the real and imaginary part. For all practical purposes a C with
a C in real or imaginary part may be considered a C itself (and
C<(NaN + 1i) ~~ NaN> is C).
Coercion of a C to any C returns the real part (coerced, if
necessary) if the imaginary part is 0, and fails otherwise. Comparison
between a C number and a C must be smart enough not to coerce
the C to a real number blindly.
=over 4
=item new
our Complex multi method new(Real $re, Real $im)
Constructs a C number from real and imaginary part. This is the
method form of C<$re + ($im)i>.
=item polar
our Seq multi method polar (Complex $nim:) is export
Returns (magnitude, angle) corresponding to the complex number.
The magnitude is non-negative, and the angle in the range C<-π ..^ π>.
=item re
our Real multi method re()
Returns the real part of the complex number.
=item im
our Real multi method im()
Returns the imaginary part of a complex number.
=back
=head2 Trigonometric functions
The following are also defined in C. The trig functions
depend on the current (lexically scoped) trig base:
enum TrigBase is export ;
constant $?TRIGBASE = Radians;
=over 4
=item I
Numeric multi method func ( Numeric $x: TrigBase $base = CALLER::<$?TRIGBASE> ) is export
where I is one of:
sin, cos, tan, asin, acos, atan, sec, cosec, cotan, asec, acosec,
acotan, sinh, cosh, tanh, asinh, acosh, atanh, sech, cosech, cotanh,
asech, acosech, acotanh.
Performs the various trigonometric functions.
Option C<$base> is used to declare how you measure your angles.
Given the value of an arc representing a single full revolution.
$base Subdivisions of circle
---- ----------------------
Radians 2*pi
Degrees 360
Gradians 400
Circles 1
To change the base within your own lexical scope, it suffices to redefine the
compiler constant with the C pragma:
use trigbase Degrees;
In addition to setting the new lexical C<$?TRIGBASE>, this also
curries a new set of functions into the current lexical scope that
assume the new base. (Note that methods calls cannot be curried, so
methods must still look up the caller's trigbase. The optimizer may,
of course, optimize these into fast function calls.)
=item atan2
our Real multi method atan2 ( Real $y: Real $x = 1, TrigBase $base = CALLER::<$?TRIGBASE> )
our Real multi atan2 ( Real $y, Real $x = 1, TrigBase $base = CALLER::<$?TRIGBASE> )
This second form of C computes the arctangent of C<$y/$x>, and takes
the quadrant into account. Otherwise behaves as other trigonometric functions.
=back
=head2 Int
An C is an immutable, integral number of arbitrary size.
=head2 Rat
class Rat does Real;
An immutable rational number, represented by two Cs, a numerator and
a denominator. All interface methods return values as if the numerator and
denominator were stored in a normal form: both numerator and denominator are
minimal in their magnitude, and the denominator is positive. So
C return C<2>, because the normal form is C<-1/2>.
=over
=item new
multi method new(Int $num, Int $denom)
Constructs a C object from the numerator and denominator.
Fails if C<$denom == 0>.
=item nude
our Seq[Int] multi method nude()
Returns a C of numerator and denominator
=item denominator
our Int multi method denominator()
Returns the denominator
=item numerator
our Int multi method numerator()
Returns the numerator
=back
=head1 Additions
Please post errors and feedback to perl6-language. If you are making
a general laundry list, please separate messages by topic.
=for vim:set expandtab sw=4:
*