The base routine

Combined from primary sources listed below.

In class Metamodel::DefiniteHOW ( Type/Metamodel/DefiniteHOW )§

See primary docmentation in context for method base.

method base_type§

method base_type($definite_type)

Returns the base type for a definite type:

say Any:D.^base_type.^name# OUTPUT: «Any␤»

In role Rational ( Type/Rational )§

See primary docmentation in context for method base.

method base-repeating§

method base-repeating(Rational:D: Int:D() $base = 10)

Returns a list of two strings that, when concatenated, represent the number in base $base. The second element is the one that repeats. For example:

my ($non-rep, $repeating= (19/3).base-repeating(10);
say $non-rep;                               # OUTPUT: «6.␤»
say $repeating;                             # OUTPUT: «3␤»
printf '%s(%s)', $non-rep, $repeating;      # OUTPUT: «6.(3)»

19/3 is 6.333333... with the 3 repeating indefinitely.

If no repetition occurs, the second string is empty:

say (5/2).base-repeating(10).raku;          # OUTPUT: «("2.5", "")␤»

The precision for determining the repeating group is limited to 1000 characters, above that, the second string is ???.

$base defaults to 10.

In role Real ( Type/Real )§

See primary docmentation in context for method base.

method base§

method base(Real:D: Int:D $base where 2..36, $digits--> 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'

The optional $digits argument asks for that many digits of fraction (which may not be negative). If omitted, a reasonable default is chosen based on type. For Int this default is 0. For Num, the default is 8. For Rational, the number of places is scaled to the size of the denominator, with a minimum of 6.

A special value of Whatever (*) can be given as $digits, which functions the same as when $digits is not specified for all Real types except the Rationals. For Rationals, the Whatever indicates that you wish all of the possible digits of the fractional part, but use caution: since there's no detection of repeating fractional parts (the algorithm will eventually stop after generating 2**63 digits).

The final digit produced is always rounded.

say pi.base(10, 3);      # OUTPUT: «3.142␤»
say (1/128).base(10, *); # OUTPUT: «0.0078125␤»
say (1/100).base(10, *); # OUTPUT: «0.01␤»
say (1/3)  .base(10, *); # WRONG: endlessly repeating fractional part

For reverse operation, see parse-base