Skip to content

Commit

Permalink
implement Int.base
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed May 7, 2011
1 parent 6ae3c92 commit 705435e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/core/Int.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ augment class Int does Real {
method gcd(Int $x: Int $y) {
pir::gcd__iii($x, $y);
}

method base(Cool $base as Int) {
fail("base must be between 2 and 36, got $base") unless 2 <= $base <= 36;
my @conversion = 0..9, 'A' .. 'Z';
my @res;
my $n = self.abs;
repeat {
push @res, @conversion[$n % $base];
$n div= $base;
} while $n > 0;
push @res, '-' if self < 0;
join '', @res.reverse;

}
}

multi sub infix:<cmp>(Int $a, Int $b) {
Expand Down
1 change: 1 addition & 0 deletions t/spectest.data
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ S32-list/roll.t
S32-list/sort.t
S32-list/uniq.t
S32-num/abs.t
S32-num/base.t
S32-num/complex.t
S32-num/cool-num.t
S32-num/exp.t
Expand Down

0 comments on commit 705435e

Please sign in to comment.