latin: rm4scc barcode row
[sheet.git] / base.plp
1 <(common.inc.plp)><:
2
3 Html({
4         title => 'number bases',
5         version => 'v1.1',
6         description => [
7                 "Cheat sheets summarising various software programs and standards.",
8         ],
9         keywords => [qw'
10                 sheet cheat reference software overview summary help keyboard map unicode
11         '],
12         stylesheet => [qw'light dark circus mono red'],
13 });
14
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');
18 :>
19 <h1>Number bases</h1>
20
21 <h2>Radix economy</h2>
22 <table>
23 <:
24 sub radix_economy {
25         my ($val, $radix) = @_;
26         return $radix * int(log($val) / log($radix) + 1);
27 }
28
29 use List::Util 'sum';
30 print '<tr><th>';
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;
37         }
38 }
39 :></table>
40
41 <h2>Reciprocal fractions (n⁻¹)</h2>
42 <table>
43 <:
44 print '<tr><th>';
45 print '<th>', $_ for @cols;
46
47 use Math::BigFloat;
48
49 my $count = 40;
50 my $places = $count<<1;
51
52 sub showfrac {
53         my ($num, $radix) = @_;
54
55         my $out = '';
56         my $class = '';
57         my $zeros = 0;
58
59 ADD_DIGITS:
60         for my $place (1 .. $places) {
61                 # add a digit in requested base (left shift)
62                 $out .= $char[ $num->blsft(1, $radix) ];
63                 $num->bmod(1) or do {
64                         # no remaining fractional part
65                         $class = $out eq '1' ? 'l5' : $place == 1 ? 'l4' : 'l3';
66                         last;
67                 };
68                 $zeros++ if $out =~ /^0+$/;
69
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">';
75                                 $check .= '</span>';
76                                 last ADD_DIGITS;
77                         }
78                 }
79         }
80         printf '<td%s style="text-align:left">%s', $class && qq( class="$class"), $out;
81 }
82
83 for my $n (2 .. $count) {
84         print '<tr>';
85         print '<th>', $n;
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);
90         }
91 }
92
93 :></table>
94
95 <hr>
96
97 <h2>Duplication (2ⁿ)</h2>
98 <table>
99 <:
100 use 5.010;
101 sub showint {
102         my ($int, $radix) = @_;
103         my @digits;
104         while ($int >= 1) {
105                 push @digits, $char[$int % $radix];
106                 $int /= $radix;
107         }
108         splice @digits, 3 * $_, 0, '&nbsp;' for reverse 1 .. @digits/3;
109         return join '', reverse @digits;
110 }
111
112 @cols = grep { not $_ ~~ [2,8,16] } @cols, 36;
113 print '<tr><th>';
114 print '<th>', $_ for @cols;
115
116 for my $n (3 .. 16, 18, 20, 24, 30, 32, 36, 40, 48, 50, 60, 64) {
117         print '<tr>';
118         print '<th>', $n;
119         for my $radix (@cols) {
120                 print '<td style="text-align:right">', showint(2 ** $n, $radix);
121         }
122         print '<th>', {
123                  4 => 'nibble',
124                  8 => 'octet',
125                 16 => '2o',
126                 24 => '3o',
127                 32 => '4o',
128                 40 => '5o, Tebi',
129                 48 => '6o',
130                 64 => 'o²',
131                 10 => 'kibi',
132                 20 => 'Mebi',
133                 30 => 'Gibi',
134                 50 => 'Pebi',
135                 60 => 'Exbi',
136                 70 => 'Zebi',
137                 80 => 'Yobi',
138         }->{$n} // '';
139 }
140
141 :></table>