Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add infix:<div>.

  • Loading branch information...
commit de7a5ceb1e95aa7aa362d56d1be9169eae341231 1 parent 17ff128
@colomon authored
Showing with 29 additions and 1 deletion.
  1. +15 −0 lib/Math/BigInt.pm
  2. +14 −1 t/02-arith.t
View
15 lib/Math/BigInt.pm
@@ -8,6 +8,7 @@ sub bdDecrement(OpaquePointer $w) returns Int is native("libbd") { ... }
sub bdAdd(OpaquePointer $w, OpaquePointer $u, OpaquePointer $v) returns Int is native("libbd") { ... }
sub bdSubtract(OpaquePointer $w, OpaquePointer $u, OpaquePointer $v) returns Int is native("libbd") { ... }
sub bdMultiply(OpaquePointer $w, OpaquePointer $u, OpaquePointer $v) returns Int is native("libbd") { ... }
+sub bdDivide(OpaquePointer $q, OpaquePointer $r, OpaquePointer $u, OpaquePointer $v) returns Int is native("libbd") { ... }
sub bdModulo(OpaquePointer $w, OpaquePointer $u, OpaquePointer $v) returns Int is native("libbd") { ... }
sub bdConvToDecimal(OpaquePointer $bd, OpaquePointer $s, Int $smax) returns Int is native("libbd") { ... }
@@ -104,6 +105,20 @@ class Math::BigInt does Real {
$result;
}
+ multi sub infix:<div>(Math::BigInt $a, Math::BigInt $b) is export(:DEFAULT) {
+ my $result = Math::BigInt.new("1");
+ my $r = Math::BigInt.new("1");
+ bdDivide($result.bd, $r.bd, $a.bd, $b.bd);
+ $result;
+ }
+
+ multi sub infix:<div>(Math::BigInt $a, Int $b) is export(:DEFAULT) {
+ my $result = Math::BigInt.new("1");
+ my $r = Math::BigInt.new("1");
+ bdDivide($result.bd, $r.bd, $a.bd, Math::BigInt.new($b).bd);
+ $result;
+ }
+
multi sub infix:<%>(Math::BigInt $a, Math::BigInt $b) is export(:DEFAULT) {
my $result = Math::BigInt.new("1");
bdModulo($result.bd, $a.bd, $b.bd);
View
15 t/02-arith.t
@@ -93,6 +93,20 @@ plan *;
is $a, "800000000000000000", "and it's 800000000000000000";
}
+# div BigInts and BigInts
+{
+ my $a = Math::BigInt.new("43452454234524532423");
+ my $b = Math::BigInt.new("2");
+
+ my $c = ($a * $b) div $b;
+ isa_ok $c, Math::BigInt, "43452454234524532423 * 2 div 2 is a BigInt";
+ is $c, "43452454234524532423", "and it's 43452454234524532423";
+
+ $c = ($a * $b) div 2;
+ isa_ok $c, Math::BigInt, "43452454234524532423 * 2 div 2 is a BigInt";
+ is $c, "43452454234524532423", "and it's 43452454234524532423";
+}
+
# modulo BigInts and BigInts
{
my $a = Math::BigInt.new("43452454234524532423");
@@ -111,5 +125,4 @@ plan *;
-
done;
Please sign in to comment.
Something went wrong with that request. Please try again.