Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix addition to properly support negative numbers.

  • Loading branch information...
commit 6e8a50c7904c959a4b2bd65e27303950dcfc79f9 1 parent 2a55b11
@colomon authored
Showing with 41 additions and 33 deletions.
  1. +23 −10 lib/Math/BigInt.pm
  2. +18 −23 t/02-arith.t
View
33 lib/Math/BigInt.pm
@@ -90,25 +90,38 @@ class Math::BigInt does Real {
}
my sub Add(Math::BigInt $a, Math::BigInt $b) {
-
+ my $result;
+ if $a.negative == $b.negative {
+ $result = Math::BigInt.new($a);
+ bdAdd($result.bd, $a.bd, $b.bd);
+ } else {
+ given bdCompare($a.bd, $b.bd) {
+ when 0 {
+ $result = Math::BigInt.new(0);
+ }
+ when -1 {
+ $result = Math::BigInt.new($b);
+ bdSubtract($result.bd, $b.bd, $a.bd);
+ }
+ when 1 {
+ $result = Math::BigInt.new($a);
+ bdSubtract($result.bd, $a.bd, $b.bd);
+ }
+ }
+ }
+ $result;
}
multi sub infix:<+>(Math::BigInt $a, Math::BigInt $b) is export(:DEFAULT) {
- my $result = Math::BigInt.new("1");
- bdAdd($result.bd, $a.bd, $b.bd);
- $result;
+ Add($a, $b);
}
multi sub infix:<+>(Math::BigInt $a, Int $b) is export(:DEFAULT) {
- my $result = Math::BigInt.new("1");
- bdAdd($result.bd, $a.bd, Math::BigInt.new($b).bd);
- $result;
+ Add($a, Math::BigInt.new($b));
}
multi sub infix:<+>(Int $a, Math::BigInt $b) is export(:DEFAULT) {
- my $result = Math::BigInt.new("1");
- bdAdd($result.bd, Math::BigInt.new($a).bd, $b.bd);
- $result;
+ Add(Math::BigInt.new($a), $b);
}
multi sub infix:<->(Math::BigInt $a, Math::BigInt $b) is export(:DEFAULT) {
View
41 t/02-arith.t
@@ -14,33 +14,28 @@ plan *;
}
{
- my $a = Math::BigInt.new("100000000000000000");
- my $b = Math::BigInt.new("-1");
-
- my $c = $a + $b;
- isa_ok $c, Math::BigInt, "100000000000000000 + -1 is a BigInt";
- todo "infix:<+> doesn't properly handle negative numbers yet", 1;
- is $c, "99999999999999999", "and it's 99999999999999999";
-}
+ my @numbers = (-20, -11, -1, 0, 1, 3, 13, 27);
+
+ for @numbers X @numbers -> $a-int, $b-int {
+ my $a = Math::BigInt.new($a-int);
+ my $b = Math::BigInt.new($b-int);
-{
- my $a = Math::BigInt.new("-100000000000000000");
- my $b = Math::BigInt.new("1");
+ my $c = $a + $b;
+ isa_ok $c, Math::BigInt, "long sum of $a-int and $b-int is a BigInt";
+ is $c, $a-int + $b-int, "and it's { $a-int + $b-int }";
- my $c = $a + $b;
- isa_ok $c, Math::BigInt, "-100000000000000000 + 1 is a BigInt";
- todo "infix:<+> doesn't properly handle negative numbers yet", 1;
- is $c, "-99999999999999999", "and it's -99999999999999999";
-}
+ $c = $b + $a;
+ isa_ok $c, Math::BigInt, "long sum of $b-int and $a-int is a BigInt";
+ is $c, $a-int + $b-int, "and it's { $a-int + $b-int }";
-{
- my $a = Math::BigInt.new("-100000000000000000");
- my $b = Math::BigInt.new("-1");
+ $c = $a + $b-int;
+ isa_ok $c, Math::BigInt, "long sum of $a-int and $b-int is a BigInt";
+ is $c, $a-int + $b-int, "and it's { $a-int + $b-int }";
- my $c = $a + $b;
- isa_ok $c, Math::BigInt, "-100000000000000000 + -1 is a BigInt";
- todo "infix:<+> doesn't properly handle negative numbers yet", 1;
- is $c, "-100000000000000001", "and it's -100000000000000001";
+ $c = $a-int + $b;
+ isa_ok $c, Math::BigInt, "long sum of $a-int and $b-int is a BigInt";
+ is $c, $a-int + $b-int, "and it's { $a-int + $b-int }";
+ }
}
# adding normal ints and BigInts
Please sign in to comment.
Something went wrong with that request. Please try again.