base: experiment listing fractions in various number bases
authorMischa POSLAWSKY <perl@shiar.org>
Sun, 22 Apr 2012 22:22:03 +0000 (00:22 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Fri, 19 Apr 2013 15:10:13 +0000 (17:10 +0200)
base.plp [new file with mode: 0644]

diff --git a/base.plp b/base.plp
new file mode 100644 (file)
index 0000000..ad6961b
--- /dev/null
+++ b/base.plp
@@ -0,0 +1,59 @@
+<(common.inc.plp)><:
+
+Html({
+       title => 'fractions',
+       version => 'v1.0',
+       description => [
+               "Cheat sheets summarising various software programs and standards.",
+       ],
+       keywords => [qw'
+               sheet cheat reference software overview summary help keyboard map unicode
+       '],
+       stylesheet => [qw'light dark circus mono red'],
+});
+
+:>
+<h1>Fractions</h1>
+
+<table>
+<:
+use Math::BigFloat;
+
+my @cols = (2, 6, 8, 9, 10, 12, 16, 18, 60);
+print '<tr><th>';
+print '<th>', $_ for @cols;
+
+my @char = (0..9, 'A'..'Z', 'a'..'z');
+my $places = 48;
+
+for my $n (2 .. 25) {
+       print '<tr>';
+       print '<th>', $n;
+       for my $radix (@cols) {
+               my $accuracy = int($places * log($radix) / log(10));
+               Math::BigFloat->accuracy($accuracy);
+               my $frac = Math::BigFloat->new(1)->bdiv($n, $accuracy+1);
+               my $out = '';
+               my $class = '';
+ADD_DIGITS:
+               for my $place (1 .. $places) {
+                       $out .= $char[ $frac->blsft(1, $radix) ];
+                       $frac->bmod(1) or do {
+                               $class = $n == $radix ? 'l5' : $place == 1 ? 'l4' : 'l3';
+                               last;
+                       };
+                       for my $check (log($n)/log($radix) .. length($out)>>1) {
+                               if ($out =~ /[^0]/ and substr($out, -$check) eq substr($out, -$check*2, $check)) {
+                                       $class = $check == 1 ? 'l2' : 'l1';
+                                       substr($out, -$check) = '';
+                                       substr($out, -$check, 0) = '(';
+                                       last ADD_DIGITS;
+                               }
+                       }
+               }
+               printf '<td%s>%s', $class && qq( class="$class"), $out;
+       }
+#      \x{305}
+}
+:></table>
+