Index

This file was automatically generated from http://svn.pugscode.org/pugs/docs/Perl6/API/Math/Basic.pod on Wed Jun 6 22:16:47 2007 GMT, revision 16639.

Math::Basic API reference


Title

Math::Basic API reference

Typical Usage

use Math::Basic

Tags

The following export tags are available:

 :UNI
 :compat
 :constants

Constants

To export the constants provided by Math::Basic use the :constants tag when importing:

 use Math::Basic :constants;
e
 constant Num Math::Basic::e

The constant e, roughly equal to 2.71828182845905.

euler_constant
 constant Num Math::Basic::γ
 constant Num Math::Basic::euler_constant

The Euler constant (or Euler-Mascheroni constant or simply γ), roughly equal to 0.57721566490153286. This constant should not be confused with e.

golden_ratio
 constant Num Math::Basic::φ
 constant Num Math::Basic::phi
 constant Num Math::Basic::golden_ratio

The golden ratio, roughly equal to 1.6180339887498948.

i
 constant Complex Math::Basic::i

The constant i, which is defined as the square root of -1.

one
 constant Complex Math::Basic::one

The constant value 1.

pi
 constant Num Math::Basic::Ï€
 constant Num Math::Basic::pi

The constant pi, roughly equal to 3.14159265358979.

zero
 constant Complex Math::Basic::zero

The constant value 0.

Exported functions

The following functions are exported by default:

abs
 our Num multi Math::Basic::abs ( Num $x )
 our Int multi Math::Basic::abs ( Int $x )
 our Complex multi Math::Basic::abs ( Complex $x )

The absolute value or modulus of $x, as denoted in mathematics by |x|.

The absolute value of a complex number is a generalization of this basic operation.

ceil
ceiling
 our Int multi Math::Basic::ceiling ( Num $x )
 our Complex multi Math::Basic::ceiling ( Complex $x )
 our Int multi Math::Basic::ceil ( Num $x )
 our Complex multi Math::Basic::ceil ( Complex $x )

The ceiling of a real number, $x, is defined as the smallest integer which is not less than $x.

In the case of complex numbers, TODO.

floor
 our Int multi Math::Basic::floor ( Num $x )
 our Complex multi Math::Basic::floor ( Complex $x )

The floor of a real number, $x, is defined as the largest integer which is not greater than $x.

In the case of complex numbers, TODO.

round
 our int enum Math::Basic::Dir <<RoundUp RoundDown RoundPositive RoundNegative RoundEven RoundOdd RoundRandom>>;
 our Int multi Math::Basic::round ( Num $x, Math::Basic::Dir :$dir = RoundUp )
 our Complex multi Math::Basic::round ( Complex $x, Math::Basic::Dir :$dir = RoundUp)

A number, when rounded, is equal to the nearest whole number. If the number falls exactly between two whole numbers, then the one which matches the direction described by :dir is returned (by default the number with the larger absolute value is returned). The rounding modes that :dir can describe are:

 RoundUp       - The default. Toward the larger absolute value.
 RoundUown     - Toward the smaller absolute value.
 RoundPositive - Toward positive infinity.
 RoundNegative - Toward negative infinity.
 RoundEven     - Toward the even of the two nearest.
 RoundOdd      - Toward the odd of the two nearest.
 RoundRandom   - Select one of the nearest randomly.

Here is an example of the usage:

 my $n = 0.5;
 my $n_rounded = round :dir(RoundEven), $n;

For complex numbers, round has the following behavior: TODO

int
truncate
 our Int multi Math::Basic::int ( Num $x )
 our Int multi Math::Basic::int ( Num $x )
 our Complex multi Math::Basic::truncate ( Complex $x )
 our Complex multi Math::Basic::truncate ( Complex $x )

Truncation returns the whole-number portion of a real number. Another way to define this operation is that, of the whole numbers closest to $x, the one closest to zero is returned.

For complex numbers, truncate has the following behavior: TODO

exp
 our Num multi Math::Basic::exp ( Num $exponent, Num :$base = Num::e )
log
log10
 our Num multi Math::Basic::log ( Num $x, Num :$base )
 our Num multi Math::Basic::log10 (Num $x);
rand
 our Num multi Math::Basic::rand ( Num $x = 1 )
sign
 our Int multi Math::Basic::sign ( Num $x )
srand
 our multi Math::Basic::srand ( Num $seed = default_seed_algorithm())
sqrt
√
 our Num multi Math::Basic::sqrt ( Num $x )
 our Num multi prefix:Math::Basic::<√> ( Num $x )

√ is only exported with the :UNI tag.

Methods

These functions are also provided as methods, which you can call on the basic numeric types:

Num

Provides: abs, floor, ceiling, ceil, round, truncate, int, exp, log, log10, rand, sign, and sqrt.

Example:

 my Num $n = 3.141;
 say "Square root of $n is {$n.sqrt}";
Int

Provides: abs, exp, log, log10, rand, sign, and sqrt.

Example:

 my Int $i = 2;
 say "log base 10 of $i = {$i.log10}";
Complex

Provides: abs, floor, ceiling, ceil, round, truncate, int, exp, log, log10, rand, sign, and sqrt.

Exmple:

 my Complex $c = (2,10.3);
 say "Floor of complex number $c is: {$c.floor}";