Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a copy constructor.

  • Loading branch information...
commit 84939136faa29017920768c18417ab85487d0f22 1 parent b8b20cb
@colomon authored
Showing with 31 additions and 0 deletions.
  1. +11 −0 lib/Math/BigInt.pm
  2. +20 −0 t/01-basic.t
View
11 lib/Math/BigInt.pm
@@ -2,6 +2,7 @@ use NativeCall;
sub bdNew() returns OpaquePointer is native("libbd") { ... }
# sub bdFreeSol(OpaquePointer $bd) is native("libbd") { ... }
+sub bdSetEqual(OpaquePointer $bd1, OpaquePointer $bd2) returns Int is native("libbd") { ... }
sub bdConvFromDecimal(OpaquePointer $bd, Str $digits) returns Int is native("libbd") { ... }
sub bdIncrement(OpaquePointer $w) returns Int is native("libbd") { ... }
sub bdDecrement(OpaquePointer $w) returns Int is native("libbd") { ... }
@@ -24,6 +25,12 @@ class Math::BigInt does Real {
has $.bd;
has $.negative;
+ multi method new(Math::BigInt $other) {
+ my $bd = bdNew();
+ bdSetEqual($bd, $other.bd);
+ self.bless(*, :$bd, :negative($other.negative));
+ }
+
multi method new(Str $digits) {
if $digits ~~ /(\-?)(\d+)/ {
my $bd = bdNew();
@@ -82,6 +89,10 @@ class Math::BigInt does Real {
Math::BigInt.new(~$a);
}
+ my sub Add(Math::BigInt $a, Math::BigInt $b) {
+
+ }
+
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);
View
20 t/01-basic.t
@@ -9,6 +9,10 @@ plan *;
ok $a ~~ Numeric & Real, "It's Numeric and Real, too";
is ~$a, "1", "Stringifies properly";
is +$a, 1, "Numifies properly";
+
+ my $b = Math::BigInt.new($a);
+ isa_ok $b, Math::BigInt, "We successfully made a BigInt";
+ is +$b, 1, "Numifies properly";
}
{
@@ -17,6 +21,10 @@ plan *;
ok $a ~~ Numeric & Real, "It's Numeric and Real, too";
is ~$a, "-1", "Stringifies properly";
is +$a, -1, "Numifies properly";
+
+ my $b = Math::BigInt.new($a);
+ isa_ok $b, Math::BigInt, "We successfully made a BigInt";
+ is +$b, -1, "Numifies properly";
}
{
@@ -25,6 +33,10 @@ plan *;
ok $a ~~ Numeric & Real, "It's Numeric and Real, too";
is ~$a, "0", "Stringifies properly";
is +$a, 0, "Numifies properly";
+
+ my $b = Math::BigInt.new($a);
+ isa_ok $b, Math::BigInt, "We successfully made a BigInt";
+ is +$b, 0, "Numifies properly";
}
{
@@ -37,6 +49,10 @@ plan *;
my $b = $a.perl.eval;
is ~$b, ~$b, ".perl.eval works as a round trip";
+
+ my $c = Math::BigInt.new($a);
+ isa_ok $c, Math::BigInt, "We successfully made a BigInt";
+ is ~$c, "1234567890098765432100123456789", "Stringifies properly";
}
{
@@ -49,6 +65,10 @@ plan *;
my $b = $a.perl.eval;
is ~$b, ~$b, ".perl.eval works as a round trip";
+
+ my $c = Math::BigInt.new($a);
+ isa_ok $c, Math::BigInt, "We successfully made a BigInt";
+ is ~$c, "-1234567890098765432100123456789", "Stringifies properly";
}
{
Please sign in to comment.
Something went wrong with that request. Please try again.