Permalink
Browse files

Add infix:<L*> and infix:<L+> in a (failed?) attempt to get operators…

… that will work with Rakudo's meta-operators.
  • Loading branch information...
1 parent e4158c1 commit 3b298b961bdbab103273bab90555aeffae0e5d76 @colomon committed Jan 31, 2011
Showing with 78 additions and 0 deletions.
  1. +49 −0 lib/Math/BigInt.pm
  2. +29 −0 t/05-metaops.t
View
@@ -198,4 +198,53 @@ class Math::BigInt does Real {
multi sub infix:«>=»(Math::BigInt $a, Math::BigInt $b) is export(:DEFAULT) {
($a <=> $b) != -1;
}
+
+ # Bonus operators (to work around a Rakudo bug affecting meta-operators)
+ our multi sub infix:<L+>(Math::BigInt $a, Math::BigInt $b) is export(:DEFAULT) {
+ my $result = Math::BigInt.new("1");
+ bdAdd($result.bd, $a.bd, $b.bd);
+ $result;
+ }
+
+ our multi sub infix:<L+>(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;
+ }
+
+ our multi sub infix:<L+>(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;
+ }
+
+ our multi sub infix:<L+>(Int $a, Int $b) is export(:DEFAULT) {
+ my $result = Math::BigInt.new("1");
+ bdAdd($result.bd, Math::BigInt.new($a).bd, Math::BigInt.new($b).bd);
+ $result;
+ }
+
+ our multi sub infix:<L*>(Math::BigInt $a, Math::BigInt $b) is export(:DEFAULT) {
+ my $result = Math::BigInt.new("1");
+ bdMultiply($result.bd, $a.bd, $b.bd);
+ $result;
+ }
+
+ our multi sub infix:<L*>(Math::BigInt $a, Int $b) is export(:DEFAULT) {
+ my $result = Math::BigInt.new("1");
+ bdMultiply($result.bd, $a.bd, Math::BigInt.new($b).bd);
+ $result;
+ }
+
+ our multi sub infix:<L*>(Int $a, Math::BigInt $b) is export(:DEFAULT) {
+ my $result = Math::BigInt.new("1");
+ bdMultiply($result.bd, Math::BigInt.new($a).bd, $b.bd);
+ $result;
+ }
+
+ our multi sub infix:<L*>(Int $a, Int $b) is export(:DEFAULT) {
+ my $result = Math::BigInt.new("1");
+ bdMultiply($result.bd, Math::BigInt.new($a).bd, Math::BigInt.new($b).bd);
+ $result;
+ }
}
View
@@ -0,0 +1,29 @@
+use Math::BigInt;
+use Test;
+
+plan *;
+
+{
+ my @fib := 1L, 1L, *+* ... *;
+ my @fib2 := 1, 1, * L+ * ... *;
+
+ is ~@fib[200], ~@fib2[200], "1L, 1L, *+* ... * agrees with 1, 1, * L+ * ... *";
+}
+
+{
+ my $a = Math::BigInt.new(1);
+ for 1..50 {
+ $a = $a * $_;
+ }
+ my $b = 1;
+ for 1..50 {
+ $b = $b L* $_;
+ }
+
+ is ~$b, ~$a, 'L* and * both generate is exactly 50!';
+ # skip "Rakudo does not properly find infix:<L*> for [L*]";
+ # is ~([L*] 1..50), ~$a, 'and so does [L*] 1..50';
+}
+
+
+done;

0 comments on commit 3b298b9

Please sign in to comment.