role Real

role Real does Numeric { ... }

Common role for non-Complex numbers.

Methods

Rat

method Rat(Real:D: Real $epsilon = 1e-6)

Converts the number to a Rat with the precision $epsilon.

rand

sub term:<rand> returns Num:D
method rand(Real:D:) returns Real:D

Returns a pseudo-random number between zero and the number.

The term form returns a pseudo-random Num between 0e0 and 1e0.

sign

method sign(Real:D:)

Returns -1 if the number is negative, 0 if it is zero and 1 otherwise.

round

method round(Real:D: $scale = 1)

Rounds the number to scale $scale. If $scale is 1, rounds to an integer. If scale is 0.1, rounds to one digit after the comma etc.

floor

method floor(Real:D) returns Int:D

Return the largest integer not greater than the number.

ceiling

method ceiling(Real:D) returns Int:D

Returns the smallest integer not less than the number.

truncate

method truncate(Real:D) returns Int:D

Rounds the number towards zero.

base

method base(Real:D: Int:D $base where 2..36) returns Str:D

Converts the number to a string, using $base as base. For $base larger than ten, capital latin letters are used.

255.base(16)    # 'FF'
Full-size type graph image as SVG

Methods supplied by role Numeric

Real does role Numeric, which provides the following methods:

Real

method Real(Numeric:D:) returns Real:D

If this Numeric is equivalent to a Real, return that Real. Fail with X::Numeric::Real otherwise.

Int

method Int(Numeric:D:) returns Int:D

If this Numeric is equivalent to a Real, return the equivalent of calling truncate on that Real to get an Int. Fail with X::Numeric::Real otherwise.

Rat

method Rat(Numeric:D: Real $epsilon = 1.0e-6) returns Rat:D

If this Numeric is equivalent to a Real, return a Rat which is within $epsilon of that Real's value. Fail with X::Numeric::Real otherwise.

Num

method Num(Numeric:D:) returns Num:D

If this Numeric is equivalent to a Real, return that Real as a Num as accurately as is possible. Fail with X::Numeric::Real otherwise.

ACCEPTS

multi method ACCEPTS(Numeric:D: $other)

Returns True if $other is numerically the same as the invocant.

log

multi sub    log(Numeric:D, Numeric $base = e) returns Numeric:D
multi method log(Numeric:D: Numeric $base = e) returns Numeric:D

Calculates the logarithm to base $base. Defaults to the natural logarithm.

log10

multi sub    log10(Numeric:D ) returns Numeric:D
multi method log10(Numeric:D:) returns Numeric:D

Calculates the logarithm to base 10.

exp

multi sub    exp(Numeric:D, Numeric:D $base = e) returns Numeric:D
multi method exp(Numeric:D: Numeric:D $base = e) returns Numeric:D

Returns $base to the power of the number, or e to the power of the number if called without a second argument.

roots

multi method roots(Numeric:D: Int:D $n) returns Positional

Returns a list of the $n complex roots, which evaluate to the original number when raised to the $nth power.

abs

multi sub    abs(Numeric:D ) returns Real:D
multi method abs(Numeric:D:) returns Real:D

Returns the absolute value of the number.

sqrt

mulit sub    sqrt(Numeric:D) returns Numeric:D
mulit method sqrt(Numeric:D) returns Numeric:D

Returns a square root of the number. For real numbers the positive square root is returned.

On negative real numbers, sqrt returns NaN rather than a complex number, in order to not confuse people who are not familiar with complex arithemtic. If you want to calculate complex square roots, coerce to Complex first, or use the roots method.

conj

multi method conj(Numeric:D) returns Numeric:D

Returns the complex conjugate of the number. Returns the number itself for real numbers.

Bool

multi method Bool(Numeric:D:)

Returns False if the number is equivalent to zero, and True otherwise.

succ

method succ(Numerid:D:)

Returns the number incremented by one (successor).

pred

method pred(Numerid:D:)

Returns the number decremented by one (predecessor).