Skip to content

Commit

Permalink
Switch gcd to an operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
colomon committed May 11, 2011
1 parent 5eb5424 commit b8b20cb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
18 changes: 18 additions & 0 deletions lib/Math/BigInt.pm
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,24 @@ class Math::BigInt does Real {
$a ** Math::BigInt.new($b);
}

multi sub infix:<gcd>(Math::BigInt $a, Math::BigInt $b) is export(:DEFAULT) {
my $result = Math::BigInt.new("1");
bdGcd($result.bd, $a.bd, $b.bd);
$result;
}

multi sub infix:<gcd>(Math::BigInt $a, Int $b) is export(:DEFAULT) {
my $result = Math::BigInt.new("1");
bdGcd($result.bd, $a.bd, Math::BigInt.new($b).bd);
$result;
}

multi sub infix:<gcd>(Int $a, Math::BigInt $b) is export(:DEFAULT) {
my $result = Math::BigInt.new("1");
bdGcd($result.bd, Math::BigInt.new($a).bd, $b.bd);
$result;
}

# Comparison operators

multi sub infix:<cmp>(Math::BigInt $a, Math::BigInt $b) is export(:DEFAULT) {
Expand Down
15 changes: 12 additions & 3 deletions t/02-arith.t
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,18 @@ plan *;

# gcd!
{
is gcd(25L, 24L), 1, "gcd of 25 and 24 is 1";
is gcd(25L, 5L), 5, "gcd of 25 and 5 is 5";
is gcd(25L, 30L), 5, "gcd of 25 and 30 is 5";
isa_ok 25L gcd 24L, Math::BigInt, "25L gcd 24L is a Math::BigInt";
is 25L gcd 24L, 1, "gcd of 25 and 24 is 1";
is 25L gcd 5L, 5, "gcd of 25 and 5 is 5";
is 25L gcd 30L, 5, "gcd of 25 and 30 is 5";
isa_ok 25L gcd 24, Math::BigInt, "25L gcd 24 is a Math::BigInt";
is 25L gcd 24, 1, "gcd of 25 and 24 is 1";
is 25L gcd 5, 5, "gcd of 25 and 5 is 5";
is 25L gcd 30, 5, "gcd of 25 and 30 is 5";
isa_ok 25 gcd 24L, Math::BigInt, "25 gcd 24L is a Math::BigInt";
is 25 gcd 24L, 1, "gcd of 25 and 24 is 1";
is 25 gcd 5L, 5, "gcd of 25 and 5 is 5";
is 25 gcd 30L, 5, "gcd of 25 and 30 is 5";
}


Expand Down

0 comments on commit b8b20cb

Please sign in to comment.