NAME ^

src/builtins/math.pir - Perl6 math functions

Math::Basic ^

Functions ^

abs

 our Num multi Num::abs ( Num $x )
 our Num multi Math::Basic::abs ( Num $x )
Absolute Value.

floor

 our Int multi Num::floor ( Num $x )
Returns the highest integer not greater than $x.

ceiling

 our Int multi Num::ceiling ( Num $x )
 &Num::ceil ::= &Num::ceiling;
Returns the lowest integer not less than $x.

round

 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".)

sign

 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;
 }
sqrt

 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

truncate

 our Int multi Num::truncate ( Num $x )
 our &Num::int ::= &Num::truncate;
Returns the closest integer to $x whose absolute value is not greater than the absolute value of $x. (In other words, just chuck any fractional part.) This is the default rounding function used by an int() cast, for historic reasons. But see Int constructor above for a rounded version.

exp

 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.

log

 our Num multi Num::log         ( Num $x: Num :$base )
 our Num multi Math::Basic::log ( Num $x, Num :$base )
Logarithm of base $base, default Natural. Calling with $x == 0 is an error.

log10

 &log10 := &log.assuming:base(10);
e

 constant Num Num::e = exp(1);
pi

 constant Num Num::pi = atan(1,1) * 4;
 constant Int Int::pi = 3;
radcalc

TODO: Functions ^

rand

 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.

srand

 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).

i

 constant Complex Complex::i = Complex::sqrt(-1);

Math::Trig ^

Functions ^

Standard Trig Functions

 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 trigonmetric 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.

atan

 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]


parrot