role Numeric

Number or object that can act as a number

role Numeric { ... }

Common role for numbers and types that can act as numbers.

Binary numeric operations return an object of the "wider" type:

Int         narrowest
Rat
FatRat
Num
Complex     widest

So for example the product of a Rat and an Int is a Rat.

Unary operations that in pure math usually return an irrational number generally return Num in Raku.

Methods

method Numeric

Defined as:

multi method Numeric(Numeric:D: --> Numeric:D)
multi method Numeric(Numeric:U: --> Numeric:D)

The :D variant simply returns the invocant. The :U variant issues a warning about using an uninitialized value in numeric context and then returns self.new.

method Int

method Int(Numeric:D: --> 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.

method Rat

method Rat(Numeric:D: Real $epsilon = 1.0e-6 --> 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.

method Num

method Num(Numeric:D: --> 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.

method narrow

method narrow(Numeric:D --> Numeric:D)

Returns the number converted to the narrowest type that can hold it without loss of precision.

say (4.0 + 0i).narrow.perl;     # OUTPUT: «4␤» 
say (4.0 + 0i).narrow.^name;    # OUTPUT: «Int␤»

method ACCEPTS

multi method ACCEPTS(Numeric:D: $other)

Returns True if $other can be coerced to Numeric and is numerically equal to the invocant (or both evaluate to NaN).

routine log

multi sub    log(Numeric:DNumeric $base = e --> Numeric:D)
multi method log(Numeric:D: Numeric $base = e --> Numeric:D)

Calculates the logarithm to base $base. Defaults to the natural logarithm. Returns NaN if $base is negative. Throws an exception if $base is 1.

routine log10

multi sub    log10(Numeric:D  --> Numeric:D)
multi method log10(Numeric:D: --> Numeric:D)

Calculates the logarithm to base 10. Returns NaN for negative arguments and -Inf for 0.

routine exp

multi sub    exp(Numeric:DNumeric:D $base = e --> Numeric:D)
multi method exp(Numeric:D: Numeric:D $base = e --> Numeric:D)

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

method roots

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

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

routine abs

multi sub    abs(Numeric:D  --> Real:D)
multi method abs(Numeric:D: --> Real:D)

Returns the absolute value of the number.

routine sqrt

multi sub    sqrt(Numeric:D --> Numeric:D)
multi method sqrt(Numeric:D --> 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 arithmetic. If you want to calculate complex square roots, coerce to Complex first, or use the roots method.

method conj

multi method conj(Numeric:D --> Numeric:D)

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

method Bool

multi method Bool(Numeric:D:)

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

method succ

method succ(Numeric:D:)

Returns the number incremented by one (successor).

method pred

method pred(Numeric:D:)

Returns the number decremented by one (predecessor).

Type Graph

Type relations for Numeric
perl6-type-graph Numeric Numeric Real Real Real->Numeric Mu Mu Any Any Any->Mu Cool Cool Cool->Any Complex Complex Complex->Numeric Complex->Cool Stringy Stringy Str Str Str->Cool Str->Stringy ComplexStr ComplexStr ComplexStr->Complex ComplexStr->Str Instant Instant Instant->Real Instant->Cool Rational Rational Rational->Real Duration Duration Duration->Real Duration->Cool Int Int Int->Real Int->Cool Num Num Num->Real Num->Cool atomicint atomicint atomicint->Int Endian Endian Endian->Int Order Order Order->Int Bool Bool Bool->Int Signal Signal Signal->Int IntStr IntStr IntStr->Str IntStr->Int PromiseStatus PromiseStatus PromiseStatus->Int int int int->Int NumStr NumStr NumStr->Str NumStr->Num Rat Rat Rat->Cool Rat->Rational FatRat FatRat FatRat->Cool FatRat->Rational

Expand above chart