style: nested scss selectors
[sheet.git] / base.plp
1 <(common.inc.plp)><:
2
3 Html({
4         title => 'number bases',
5         version => '1.2',
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 });
13
14 my @cols = (2, 6, 8, 9, 10, 12, 16, 18, 20);
15 my @morecols = (2 .. 6, 8, 9, 10, 12, 16, 18, 20, 24, 32, 36, 64);
16 my @char = (0..9, 'A'..'Z', 'a'..'z');
17 my %RADIXNAME = (
18          2 => 'binary',
19          3 => 'ternary',
20         #4 => 'quaternary',
21         #5 => 'quinary',
22          6 => 'senary',
23          8 => 'octal',
24         #9 => 'nonary',
25         10 => 'decimal',
26         12 => 'dozenal', # duodecimal
27         16 => 'hexadecimal',
28         20 => 'vigesimal',
29         #36 => 'double-senary',
30         60 => 'sexagesimal',
31         #64 => 'double-octal',
32 );
33 :>
34 <h1>Number bases</h1>
35
36 <h2>Radix economy</h2>
37 <table class=mapped>
38 <:
39 sub showcolhead {
40         print '<col>';
41         my @spans;
42         $spans[ $_ > 10 ]++ for @_;
43         print "<colgroup span=$_>" for @spans;
44         print '<thead><tr><th>';
45         for (@_) {
46                 print '<th>', $_ < 36 ? $char[$_] : $char[35].'+'.$char[$_ - 35];
47                 print " <small>($_)</small>" for join(', ',
48                         $RADIXNAME{$_} // (),
49                         $_ >= 10 ? "base$_" : (),
50                 ) || ();
51         }
52         say '</thead>';
53 }
54
55 sub radix_economy {
56         my ($val, $radix) = @_;
57         return $radix * int(log($val) / log($radix) + 1);
58 }
59
60 use List::Util 'sum';
61
62 showcolhead(@morecols);
63
64 for my $max (100, 255, 1024) {
65         print '<tr><th>⍳', $max;
66         for my $radix (@morecols) {
67                 printf '<td style="text-align:right">%.1f',
68                         sum(map { radix_economy($_, $radix) } 1 .. $max) / $max;
69         }
70 }
71 :></table>
72
73 <h2>Reciprocal fractions (n⁻¹)</h2>
74 <table class=mapped>
75 <:
76 use Math::BigFloat;
77
78 my $count = 40;
79 my $places = $count<<1;
80
81 sub showfrac {
82         my ($num, $radix) = @_;
83
84         my $out = '';
85         my $class = '';
86         my $zeros = 0;
87
88 ADD_DIGITS:
89         for my $place (1 .. $places) {
90                 # add a digit in requested base (left shift)
91                 $out .= $char[ $num->blsft(1, $radix) ];
92                 $num->bmod(1) or do {
93                         # no remaining fractional part
94                         $class = $out eq '1' ? 'l5' : $place == 1 ? 'l4' : 'l3';
95                         last;
96                 };
97                 $zeros++ if $out =~ /^0+$/;
98
99                 for my $check ($zeros .. length($out)>>1) {
100                         if (substr($out, -$check) eq substr($out, -$check*2, $check)) {
101                                 $class = $check == 1 ? 'l2' : 'l1';
102                                 substr($out, -$check) = '';
103                                 substr($out, -$check, 0) = '<span style="text-decoration:overline">';
104                                 $check .= '</span>';
105                                 last ADD_DIGITS;
106                         }
107                 }
108         }
109         printf '<td%s style="text-align:left">%s', $class && qq( class="$class"), $out;
110 }
111
112 showcolhead(@cols);
113
114 for my $n (2 .. $count) {
115         print '<tbody>' if $n % 8 == 1;
116         print '<tr>';
117         print '<th>', $n;
118         for my $radix (@cols) {
119                 my $accuracy = int($places * log($radix) / log(10));
120                 Math::BigFloat->accuracy($accuracy);
121                 showfrac(scalar Math::BigFloat->new(1)->bdiv($n, $accuracy+1), $radix);
122         }
123         say '';
124 }
125
126 :></table>
127
128 <hr>
129
130 <h2>Duplication (2ⁿ)</h2>
131 <table class=mapped>
132 <:
133 sub showint {
134         my ($int, $radix) = @_;
135         my @digits;
136         while ($int >= 1) {
137                 push @digits, $char[$int % $radix];
138                 $int /= $radix;
139         }
140         splice @digits, 3 * $_, 0, '&nbsp;' for reverse 1 .. @digits/3;
141         return join '', reverse @digits;
142 }
143
144 @cols = grep { not $_ ~~ [2,8,16] } @cols, 36;
145 showcolhead(@cols);
146
147 for my $n (0, 3 .. 16, 0, 18, 20, 24, 30, 32, 36, 40, 48, 50, 60, 64) {
148         if (!$n) {
149                 print '<tbody>';
150                 next;
151         }
152         print '<tr>';
153         print '<th>', $n;
154         for my $radix (@cols) {
155                 print '<td style="text-align:right">', showint(2 ** $n, $radix);
156         }
157         say '<th>', {
158                  4 => 'nibble',
159                  8 => 'octet',
160                 16 => '2o',
161                 24 => '3o',
162                 32 => '4o',
163                 40 => '5o, Tebi',
164                 48 => '6o',
165                 64 => 'o²',
166                 10 => 'kibi',
167                 20 => 'Mebi',
168                 30 => 'Gibi',
169                 50 => 'Pebi',
170                 60 => 'Exbi',
171                 70 => 'Zebi',
172                 80 => 'Yobi',
173         }->{$n} // '';
174 }
175
176 :></table>