Skip to content

Commit

Permalink
base-repeating puts parens around repeating group
Browse files Browse the repository at this point in the history
  • Loading branch information
TimToady committed Feb 13, 2015
1 parent 929945a commit 94c51c7
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/core/Rational.pm
Expand Up @@ -109,6 +109,43 @@ my role Rational[::NuT, ::DeT] does Real {
$s;
}

method base-repeating($base) {
my $s = $!numerator < 0 ?? '-' !! '';
my $r = self.abs;
my $i = $r.floor;
$r -= $i;
$s ~= $i.base($base);
if $r {
my @f;
my %seen;
my $rp = $r.perl;
while $r {
%seen{$rp} = +@f;
$r *= $base;
$i = $r.floor;
$r -= $i;
$rp = $r.perl;
push @f, $i;
last if %seen{$rp}.defined;
last if +@f >= 100000; # sanity
}
state @digits = '0'..'9', 'A'..'Z';
my $frac = @digits[@f].join;
if $r {
my $seen = %seen{$r.perl};
note "RESULT $frac $seen %seen.perl()";
if $seen.defined {
$frac = substr($frac,0,$seen) ~ '(' ~ substr($frac,$seen) ~ ')';
}
else {
$frac ~= '...';
}
}
$s ~= '.' ~ $frac;
}
$s;
}

method succ {
self.new($!numerator + $!denominator, $!denominator);
}
Expand Down

0 comments on commit 94c51c7

Please sign in to comment.