parrotcode: Perl6 math functions | |
Contents | Language Implementations | Perl6 |
src/builtins/math.pir - Perl6 math functions
our Num multi Num::abs ( Num $x )
our Num multi Math::Basic::abs ( Num $x )
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::sqrt ( Num $x )
our Complex multi Complex::sqrt ( Num $x )
our Complex multi Complex::sqrt ( Complex $x )
our Num multi Math::Basic::sqrt ( Num $x )
$x ** 0.5
our Int multi Num::truncate ( Num $x )
our &Num::int ::= &Num::truncate;
int()
cast, for historic reasons. But see Int constructor above for a rounded version. 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. our Num multi Num::log ( Num $x: Num :$base )
our Num multi Math::Basic::log ( Num $x, Num :$base )
$base
, default Natural. Calling with $x == 0
is an error. &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.## XXX: conjectural as perl6 subs do not support adverbs yet ## once this is correct, it will likely become a generated function, as will: ## sin, cos, tan, asin, acos, atan, sec, cosec, cotan, asec, acosec, ## acotan, sinh, cosh, tanh, asinh, acosh, atanh, sech, cosech, cotanh, ## asech, acosech, acotanh .sub 'sin' .param num a .param pmc adverbs :slurpy :named .local num converter converter = 1 $I0 = exists adverbs['$base'] unless $I0 goto doit $P0 = adverbs['base'] unless $P0 goto doit $S0 = $P0 downcase $S0 $S1 = substr $S0, 0, 1 $I0 = index 'rdg123456789', $S1 if $I0 == -1 goto err_unrecognized_base unless $I0 goto doit converter = atan 1 if $I0 == 1 goto deg if $I0 == 2 goto grad user_defined: $N0 = $S0 $N0 /= 8 converter /= $N0 goto doit deg: converter /= 45 goto doit grad: converter /= 50 doit: a *= converter $N0 = sin a .return ($N0) err_unrecognized_base: $S1 = "sin: unrecognized base '" $S1 .= $S0 $S1 .= "'" .return 'die'($S1) .end
## vim: expandtab sw=4
Hey! The above document had some coding errors, which are explained below:
|