digits
Returns the digits of an integer or a rational number.
Syntax
-
digits(x[, n[, mode]])-
xis an integer or a rational number -
nis a non-negative integer -
modeis either"fractional"or"significant"
-
Description
-
If
xis an integer, thendigits(x)returns the digits ofxas a list with the MSD first.All digits are exact.
-
If
xis a rational number, thendigits(x, n, mode)returns the digits ofxin a structure with one member,int, for the integer part and another member,frac, for the fractional part. Each member is a list of digits with the MSD first.If
mode = "fractional", all integral digits are returned and up tonfractional digits (trailing, insignificant, zeroes are not included).If
mode = "significant", then thenmost significant digits are returned, except for trailing zeros. Notice that not all integral digits might be returned.If omitted,
ndefaults to64andmodeto"fractional".All digits are exact. No rounding takes place.
-
If
xis a real or complex number, an attempt is made to convert it to an integer (preferably) or an approximate rational number, and then proceed with the result of that conversion. Unlessxhappens to be an integer or a rational number, the resulting digits may not be exact.
Examples
digits(521341)
5 2 1 3 4 1
#digits(1852616517806850212)
19
digits(1/613)
int: (0) frac: (0, 0, 1, 6, 3, 1, 3, 2, 1, 3, 7, 0, 3, 0, 9, 9, 5, 1, 0, 6, 0, 3, 5, 8, 8, 9, 0, 7, 0, 1, 4, 6, 8, 1, 8, 9, 2, 3, 3, 2, 7, 8, 9, 5, 5, 9, 5, 4, 3, 2, 3, 0, 0, 1, 6, 3, 1, 3, 2, 1, 3, 7, 0, 3)
sort(frequencies(digits(1/123456789, 1E6).frac))
(0, 100177) (1, 99612) (2, 100115) (3, 100281) (4, 100006) (5, 100022) (6, 99586) (7, 99791) (8, 100175) (9, 100235)
sort(frequencies(compute(RandomInt(1000000)^2, n, 1, 1000000) @ digits @ first))
(1, 192164) (2, 146925) (3, 123873) (4, 109158) (5, 98564) (6, 90439) (7, 84566) (8, 79092) (9, 75219)