Rules / Types, classes & roles / basic

Numeric Reference

Number or object that can act as a number

What it is #

Position in the hierarchy #

Inherits from
Does
Consumed byReal, Complex

Routines #

Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.

method Numeric #

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

Returns 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 narrow #

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

Returns Numeric:D.

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

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        log(Numeric:D, Numeric $base = e --> Numeric:D)
multi method log(Numeric:D: Numeric $base = e --> Numeric:D)

Returns Numeric:D.

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:D  --> Numeric:D)
multi method log10(Numeric:D: --> Numeric:D)

Returns Numeric:D.

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:D)
multi method log2(Numeric:D:)

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:D, Numeric:D $base = e --> Numeric:D)
multi method exp(Numeric:D: Numeric:D $base = e --> Numeric:D)

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.

method roots #

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

Returns Positional.

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

routine abs #

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

Returns Real:D.

Returns the absolute value of the number.

routine sqrt #

multi        sqrt(Numeric:D --> Numeric:D)
multi method sqrt(Numeric:D --> 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 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

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

Examples, run three ways #

Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.

2 no-output · 1 ok

role Numeric { ... }

Not executed: the documentation states no expected output for this example.

Int         narrowest
Rat
FatRat
Num
Complex     widest

Not executed: the documentation states no expected output for this example.

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

Documentation, Rakudo and Raku++ all agree.