parrotcode: Perl6 math functions | |
Contents | Language Implementations | Perl6 |
src/builtins/math.pir - Perl6 math functions
our Int multi Num::floor ( Num $x )Returns the highest integer not greater than $x.
our Int multi Num::ceiling ( Num $x ) &Num::ceil ::= &Num::ceiling;Returns the lowest integer not less than $x.
our Int multi Num::round ( Num $x ) our Int multi Int ( Num $x )Returns the nearest integer to $x. The algorithm is floor($x + 0.5). (Other rounding algorithms will be given extended names beginning with "round".)
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; }or more succinctly:
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 )Performs similar to
$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 )Pseudo random number in range
0 ..^ $x
. That is, 0
is theoretically possible, while $x
is not.
multi Math::Basic::srand ( Num $seed = default_seed_algorithm())Seed the generator
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' )where func 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
:$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.Note that module currying can be used within a lexical scope to specify a consistent base so you don't have to supply it with every call:
my module Trig ::= Math::Trig.assuming(:base<degrees>);This overrides the default of "radians".NOTE: These only work with radians so far.
our Num multi Math::Trig::atan2 ( Num $y, Num $x = 1 : Num :$base )This second form of
atan
computes the arctangent of $y/$x, and takes the quadrant into account. Otherwise behaves as other trigonometric functions.[Note: changed atan back to atan2, or the default $x = 1 will confuse MMD. The other alternative would be to remove the default. --law]
|