parrotcode: Perl6 math functions | |
Contents | Language Implementations | Perl6 |
src/builtins/math.pir - Perl6 math functions
our Int multi Num::floor ( Num $x )
our Int multi Num::ceiling ( Num $x )
&Num::ceil ::= &Num::ceiling;
our Int multi Num::round ( Num $x )
our Int multi Int ( Num $x )
our Int multi Num::sign ( Num $x )
our Int multi Math::Basic::sign ( Num $x )
if !defined($x) { return undef };
if $x < 0 { return -1 };
if $x > 0 { return 1 };
if $x == 0 { return 0 };
fail;
}
our Int multi Math::Basic::sign ( Num $x )
$x <=> 0;
}
our Num multi Num::exp ( Num $exponent: Num :$base = Num::e )
our Num multi Math::Basic::exp ( Num $exponent, Num :$base = Num::e )
$base ** $exponent
. $base
defaults to the constant e. &log10 := &log.assuming:base(10);
constant Num Num::e = exp(1);
constant Num Num::pi = atan(1,1) * 4;
constant Int Int::pi = 3;
our Num multi Math::Basic::rand ( Num $x = 1 )
0 ..^ $x
. That is, 0
is theoretically possible, while $x
is not. multi Math::Basic::srand ( Num $seed = default_seed_algorithm())
rand
uses. $seed
defaults to some combination of various platform dependent characteristics to yield a non-deterministic seed. Note that you get one srand()
for free when you start a Perl program, so you must call srand()
yourself if you wish to specify a deterministic seed (or if you wish to be differently nondeterministic). constant Complex Complex::i = Complex::sqrt(-1);
our Num multi Num::func ( Num $x : :$base = 'radians' )
our Num multi Math::Trig::func ( Num $x, :$base = 'radians' )
:$base
is used to declare how you measure your angles. Given the value of an arc representing a single full revolution. $base Result
---- -------
/:i ^r/ Radians (2*pi)
/:i ^d/ Degrees (360)
/:i ^g/ Gradians (400)
Num Units of 1 revolution.
my module Trig ::= Math::Trig.assuming(:base<degrees>);
our Num multi Math::Trig::atan2 ( Num $y, Num $x = 1 : Num :$base )
atan
computes the arctangent of $y/$x, and takes the quadrant into account. Otherwise behaves as other trigonometric functions.
|