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§
multi method Numeric(Numeric: --> Numeric)multi method Numeric(Numeric: --> Numeric)
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 narrow§
method narrow(Numeric --> Numeric)
Returns the number converted to the narrowest type that can hold it without loss of precision.
say (4.0 + 0i).narrow.raku; # OUTPUT: «4»say (4.0 + 0i).narrow.^name; # OUTPUT: «Int»
method ACCEPTS§
multi method ACCEPTS(Numeric: )
Returns True
if $other
can be coerced to Numeric
and is numerically equal to the invocant (or both evaluate to NaN
).
routine log§
multi log(Numeric, Numeric = e --> Numeric)multi method log(Numeric: Numeric = e --> Numeric)
Calculates the logarithm to base $base
. Defaults to the natural logarithm. Throws an exception if $base
is 1
.
Returns NaN
for negative arguments. As of 6.e language version (early implementation exists in Rakudo compiler 2023.02+), will return a Complex
value for negative arguments.
routine log10§
multi log10(Numeric --> Numeric)multi method log10(Numeric: --> Numeric)
Calculates the logarithm to base 10. Returns -Inf
for 0
.
Returns NaN
for negative arguments. As of 6.e language version (early implementation exists in Rakudo compiler 2023.02+), will return a Complex
value for negative arguments.
routine log2§
multi log2(Numeric)multi method log2(Numeric:)
Calculates the logarithm to base 2. Returns -Inf
for 0
.
Returns NaN
for negative arguments. As of 6.e language version (early implementation exists in Rakudo compiler 2023.02+), will return a Complex
value for negative arguments.
routine exp§
multi exp(Numeric, Numeric = e --> Numeric)multi method exp(Numeric: Numeric = e --> Numeric)
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: Int --> Positional)
Returns a list of the $n
complex roots, which evaluate to the original number when raised to the $n
th power.
routine abs§
multi abs(Numeric --> Real)multi method abs(Numeric: --> Real)
Returns the absolute value of the number.
routine sqrt§
multi sqrt(Numeric --> Numeric)multi method sqrt(Numeric --> Numeric)
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.
As of 6.e language version (early implementation exists in Rakudo compiler 2023.02+), will return a Complex
value for negative arguments.
method conj§
multi method conj(Numeric --> Numeric)
Returns the complex conjugate of the number. Returns the number itself for real numbers.
method Bool§
multi method Bool(Numeric:)
Returns False
if the number is equivalent to zero, and True
otherwise.
method succ§
method succ(Numeric:)
Returns the number incremented by one (successor).
method pred§
method pred(Numeric:)
Returns the number decremented by one (predecessor).