Permalink
Browse files

Switch gcd to an operator.

  • Loading branch information...
colomon committed May 11, 2011
1 parent 5eb5424 commit b8b20cb9b27d8a587590c22611d425e77e51c12a
Showing with 30 additions and 3 deletions.
  1. +18 −0 lib/Math/BigInt.pm
  2. +12 −3 t/02-arith.t
View
@@ -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) {
View
@@ -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";
}

0 comments on commit b8b20cb

Please sign in to comment.