4 title => 'number bases',
7 "Cheat sheets summarising various software programs and standards.",
10 sheet cheat reference software overview summary help keyboard map unicode
12 stylesheet => [qw'light dark circus mono red'],
15 my @cols = (2, 6, 8, 9, 10, 12, 16, 18, 20);
16 my @morecols = (2 .. 6, 8, 9, 10, 12, 16, 18, 20, 24, 32, 36, 64);
17 my @char = (0..9, 'A'..'Z', 'a'..'z');
21 <h2>Radix economy</h2>
25 my ($val, $radix) = @_;
26 return $radix * int(log($val) / log($radix) + 1);
31 print '<th>', $_ for @morecols;
32 for my $max (100, 255, 1024) {
33 print '<tr><th>⍳', $max;
34 for my $radix (@morecols) {
35 printf '<td style="text-align:right">%.1f',
36 sum(map { radix_economy($_, $radix) } 1 .. $max) / $max;
41 <h2>Reciprocal fractions (n⁻¹)</h2>
45 print '<th>', $_ for @cols;
50 my $places = $count<<1;
53 my ($num, $radix) = @_;
60 for my $place (1 .. $places) {
61 # add a digit in requested base (left shift)
62 $out .= $char[ $num->blsft(1, $radix) ];
64 # no remaining fractional part
65 $class = $out eq '1' ? 'l5' : $place == 1 ? 'l4' : 'l3';
68 $zeros++ if $out =~ /^0+$/;
70 for my $check ($zeros .. length($out)>>1) {
71 if (substr($out, -$check) eq substr($out, -$check*2, $check)) {
72 $class = $check == 1 ? 'l2' : 'l1';
73 substr($out, -$check) = '';
74 substr($out, -$check, 0) = '<span style="text-decoration:overline">';
80 printf '<td%s style="text-align:left">%s', $class && qq( class="$class"), $out;
83 for my $n (2 .. $count) {
86 for my $radix (@cols) {
87 my $accuracy = int($places * log($radix) / log(10));
88 Math::BigFloat->accuracy($accuracy);
89 showfrac(scalar Math::BigFloat->new(1)->bdiv($n, $accuracy+1), $radix);
97 <h2>Duplication (2ⁿ)</h2>
101 my ($int, $radix) = @_;
104 push @digits, $char[$int % $radix];
107 splice @digits, 3 * $_, 0, ' ' for reverse 1 .. @digits/3;
108 return join '', reverse @digits;
111 @cols = grep { not $_ ~~ [2,8,16] } @cols, 36;
113 print '<th>', $_ for @cols;
115 for my $n (3 .. 16, 18, 20, 24, 30, 32, 36, 40, 48, 50, 60, 64) {
118 for my $radix (@cols) {
119 print '<td style="text-align:right">', showint(2 ** $n, $radix);