Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implementation of polymod method to play with
  • Loading branch information
TimToady committed Feb 14, 2015
1 parent 949b809 commit 874caf6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/core/Int.pm
Expand Up @@ -58,6 +58,22 @@ my class Int does Real { # declared in BOOTSTRAP
nqp::p6box_s(nqp::base_I(self, $b)) ~ ($digits ?? '.' ~ '0' x $digits !! '');
}

# If self is Int, we assume mods are Ints also. (div fails otherwise.)
# If do-not-want, user should cast invocant to proper domain.
method polymod(Int:D: *@mods) {
my $more = self;
fail X::OutOfRange.new(what => 'invocant to polymod', got => $more, range => "0..*") if $more < 0;
gather {
for @mods -> $mod {
last unless $more;
fail X::Numeric::DivideByZero.new(using => 'polymod') unless $mod;
take $more mod $mod;
$more div= $mod;
}
take $more;
}
}

method expmod(Int:D: Int:D \base, Int:D \mod) {
nqp::expmod_I(self, nqp::decont(base), nqp::decont(mod), Int);
}
Expand Down
15 changes: 15 additions & 0 deletions src/core/Real.pm
Expand Up @@ -64,6 +64,21 @@ my role Real does Numeric {
}
method isNaN { Bool::False }

method polymod(Real:D: *@mods) {
my $more = self;
fail X::OutOfRange.new(what => 'invocant to polymod', got => $more, range => "0..*") if $more < 0;
gather {
for @mods -> $mod {
last unless $more;
fail X::Numeric::DivideByZero.new(using => 'polymod') unless $mod;
take my $rem = $more % $mod;
$more -= $rem;
$more /= $mod;
}
take $more;
}
}

method base(Int:D $base, $digits = 1e8.log($base.Num).Int) {
my Int $int_part = self.Int;
my $frac = abs(self - $int_part);
Expand Down

0 comments on commit 874caf6

Please sign in to comment.