Numeric methods
FullAsk a number about itself — primality, magnitude, roots, and radix.
Numbers carry a set of query and conversion methods. A representative handful:
is-prime #
say 17.is-prime; say 18.is-prime;
True
Falseis-prime works on arbitrarily large integers (it uses a probabilistic test).
abs and sqrt #
say (-5).abs; say 10.sqrt;
5
3.1622776601683795abs keeps the type (an Int stays an Int); sqrt returns a Num.
expmod — modular exponentiation #
expmod(base, exp, mod) computes base ** exp mod mod without ever building the huge intermediate power — the workhorse behind modular arithmetic and cryptography.
say expmod(2, 10, 1000); say 7.expmod(256, 13);
24
92 ** 10 is 1024, so mod 1000 is 24; the method form 7.expmod(256, 13) reads the base as the invocant.
narrow — the simplest exact type #
.narrow returns the value as the simplest type that still holds it exactly — an integral Rat collapses to Int, a genuine fraction stays a Rat.
say (6/3).narrow.^name; say (3/4).narrow.^name;
Int
RatBit positions — msb / lsb #
.msb and .lsb give the index (from 0) of the most- and least-significant set bit — the position of the top 1 and the bottom 1 in the binary form.
say 8.msb; say 12.lsb; say 255.msb;
3
2
78 is 1000, so its only bit is at index 3; 12 is 1100, whose lowest set bit is at index 2; 255 is eight ones, top bit at index 7.
base — render in another radix #
base(n) renders an integer in radix n (2–36) as a string.
say 255.base(2); say 255.base(16);
11111111
FFNotes #
.Int,.Rat,.Num, and.Complexmove a number through the numeric tower;.narrowpicks the simplest type that holds the value exactly.- Rounding methods (
.floor,.ceiling,.round,.truncate) live on Rounding. .chrturns a codepoint number into its character (65.chrisA), the inverse of.ordon a string.