Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Rat.base by TimToady++
  • Loading branch information
moritz committed May 12, 2012
1 parent 6b8c360 commit f685cb6
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/core/Rat.pm
Expand Up @@ -83,6 +83,36 @@ my role Rational does Real {
}
$s;
}

method base($base) {
my $s = $!numerator < 0 ?? '-' !! '';
my $r = self.abs;
my $i = $r.floor;
$r -= $i;
$s ~= $i.base($base);
if $r {
my $want = $!denominator < $base**6 ?? 6 !! $!denominator.log($base).ceiling + 1;
my @f;
while $r and @f < $want {
$r *= $base;
$i = $r.floor;
push @f, $i;
$r -= $i;
}
if 2 * $r >= 1 {
for @f-1 ... 0 -> $x {
last if ++@f[$x] < $base;
@f[$x] = 0;
$s ~= ($i+1).base($base) if $x == 0; # never happens?
}
}
$s ~= '.';
$s ~= (0..9,'A'..'Z')[@f].join;
}
$s;
}


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

0 comments on commit f685cb6

Please sign in to comment.