summaryrefslogtreecommitdiff
path: root/lib/Math/BigInt
diff options
context:
space:
mode:
authorJarkko Hietaniemi <jhi@iki.fi>2002-05-30 20:39:30 +0000
committerJarkko Hietaniemi <jhi@iki.fi>2002-05-30 20:39:30 +0000
commitd614cd8b2519c84f1ee8ae0c9c71fba2ed16cfb3 (patch)
tree9dcab260ac9fae520e0614ff3085803d7e7a832a /lib/Math/BigInt
parentbea1ee5ea99c2aeca55be8c6a67d96f1dd075299 (diff)
downloadperl-d614cd8b2519c84f1ee8ae0c9c71fba2ed16cfb3.tar.gz
Upgrade to Math::BigInt 1.57.
p4raw-id: //depot/perl@16906
Diffstat (limited to 'lib/Math/BigInt')
-rw-r--r--lib/Math/BigInt/Calc.pm29
-rw-r--r--lib/Math/BigInt/t/bare_mbf.t1
-rw-r--r--lib/Math/BigInt/t/bigfltpm.inc2
-rw-r--r--lib/Math/BigInt/t/bigintpm.inc49
-rw-r--r--lib/Math/BigInt/t/calling.t2
-rw-r--r--lib/Math/BigInt/t/mbi_rand.t20
-rw-r--r--lib/Math/BigInt/t/mbimbf.inc2
-rw-r--r--lib/Math/BigInt/t/upgrade.inc2
8 files changed, 85 insertions, 22 deletions
diff --git a/lib/Math/BigInt/Calc.pm b/lib/Math/BigInt/Calc.pm
index f1ade921fe..a2fe8123e4 100644
--- a/lib/Math/BigInt/Calc.pm
+++ b/lib/Math/BigInt/Calc.pm
@@ -8,7 +8,7 @@ require Exporter;
use vars qw/@ISA $VERSION/;
@ISA = qw(Exporter);
-$VERSION = '0.27';
+$VERSION = '0.28';
# Package to store unsigned big integers in decimal and do math with them
@@ -485,9 +485,11 @@ sub _mul_use_mul
}
# since multiplying $x with $x fails, make copy in this case
- $yv = [@$xv] if "$xv" eq "$yv"; # same references?
+ $yv = [@$xv] if $xv == $yv; # same references?
+# $yv = [@$xv] if "$xv" eq "$yv"; # same references?
+
# since multiplying $x with $x would fail here, use the faster squaring
-# return _square($c,$xv) if "$xv" eq "$yv"; # same reference?
+# return _square($c,$xv) if $xv == $yv; # same reference?
if ($LEN_CONVERT != 0)
{
@@ -565,9 +567,10 @@ sub _mul_use_div
# since multiplying $x with $x fails, make copy in this case
- $yv = [@$xv] if "$xv" eq "$yv"; # same references?
+ $yv = [@$xv] if $xv == $yv; # same references?
+# $yv = [@$xv] if "$xv" eq "$yv"; # same references?
# since multiplying $x with $x would fail here, use the faster squaring
-# return _square($c,$xv) if "$xv" eq "$yv"; # same reference?
+# return _square($c,$xv) if $xv == $yv; # same reference?
if ($LEN_CONVERT != 0)
{
@@ -642,7 +645,7 @@ sub _div_use_mul
return $x;
}
- my $y = [ @$yorg ];
+ my $y = [ @$yorg ]; # always make copy to preserve
if ($LEN_CONVERT != 0)
{
$c->_to_small($x); $c->_to_small($y);
@@ -782,7 +785,7 @@ sub _div_use_div
return $x;
}
- my $y = [ @$yorg ];
+ my $y = [ @$yorg ]; # always make copy to preserve
if ($LEN_CONVERT != 0)
{
$c->_to_small($x); $c->_to_small($y);
@@ -1571,6 +1574,16 @@ sub _from_bin
$x;
}
+sub _modinv
+ {
+ # inverse modulus
+ }
+
+sub _modpow
+ {
+ # modulus of power ($x ** $y) % $z
+ }
+
##############################################################################
##############################################################################
@@ -1674,6 +1687,8 @@ slow) fallback routines to emulate these:
_gcd(obj,obj) return Greatest Common Divisor of two objects
_zeros(obj) return number of trailing decimal zeros
+ _modinv return inverse modulus
+ _modpow return modulus of power ($x ** $y) % $z
Input strings come in as unsigned but with prefix (i.e. as '123', '0xabc'
or '0b1101').
diff --git a/lib/Math/BigInt/t/bare_mbf.t b/lib/Math/BigInt/t/bare_mbf.t
index a160c7cf7e..8b682f99eb 100644
--- a/lib/Math/BigInt/t/bare_mbf.t
+++ b/lib/Math/BigInt/t/bare_mbf.t
@@ -8,6 +8,7 @@ BEGIN
$| = 1;
# to locate the testing files
my $location = $0; $location =~ s/bare_mbf.t//i;
+ print "$0\n";
if ($ENV{PERL_CORE})
{
# testing with the core distribution
diff --git a/lib/Math/BigInt/t/bigfltpm.inc b/lib/Math/BigInt/t/bigfltpm.inc
index 734b935168..d455165d99 100644
--- a/lib/Math/BigInt/t/bigfltpm.inc
+++ b/lib/Math/BigInt/t/bigfltpm.inc
@@ -4,7 +4,7 @@ ok ($class->config()->{lib},$CL);
while (<DATA>)
{
- chop;
+ chomp;
$_ =~ s/#.*$//; # remove comments
$_ =~ s/\s+$//; # trailing spaces
next if /^$/; # skip empty lines & comments
diff --git a/lib/Math/BigInt/t/bigintpm.inc b/lib/Math/BigInt/t/bigintpm.inc
index 2bcf3466fb..4d74a412ca 100644
--- a/lib/Math/BigInt/t/bigintpm.inc
+++ b/lib/Math/BigInt/t/bigintpm.inc
@@ -40,7 +40,7 @@ my ($f,$z,$a,$exp,@a,$m,$e,$round_mode,$expected_class);
while (<DATA>)
{
- chop;
+ chomp;
next if /^#/; # skip comments
if (s/^&//)
{
@@ -163,9 +163,18 @@ while (<DATA>)
$try .= "\$x ^ \$y;";
}elsif ($f eq "bpow"){
$try .= "\$x ** \$y;";
+ } elsif( $f eq "bmodinv") {
+ $try .= "\$x->bmodinv(\$y);";
}elsif ($f eq "digit"){
$try .= "\$x->digit(\$y);";
- } else { warn "Unknown op '$f'"; }
+ } else {
+ $try .= "\$z = $class->new(\"$args[2]\");";
+
+ # Functions with three arguments
+ if( $f eq "bmodpow") {
+ $try .= "\$x->bmodpow(\$y,\$z);";
+ } else { warn "Unknown op '$f'"; }
+ }
} # end else all other ops
$ans1 = eval $try;
@@ -1311,6 +1320,42 @@ inf:0:inf
14:3:4
# bug in Calc with '99999' vs $BASE-1
10000000000000000000000000000000000000000000000000000000000000000000000000000000000:10000000375084540248994272022843165711074:999999962491547381984643365663244474111576
+#&bmodinv
+## format: number:modulus:result
+## bmodinv Data errors
+#abc:abc:NaN
+#abc:5:NaN
+#5:abc:NaN
+## bmodinv Expected Results from normal use
+#1:5:1
+#3:5:2
+#-2:5:2
+#324958749843759385732954874325984357439658735983745:2348249874968739:1741662881064902
+## bmodinv Error cases / useless use of function
+#3:-5:NaN
+#inf:5:NaN
+#&bmodpow
+## format: number:exponent:modulus:result
+## bmodpow Data errors
+#abc:abc:abc:NaN
+#5:abc:abc:NaN
+#abc:5:abc:NaN
+#abc:abc:5:NaN
+#5:5:abc:NaN
+#5:abc:5:NaN
+#abc:5:5:NaN
+## bmodpow Expected results
+#0:0:2:1
+#1:0:2:1
+#0:0:1:0
+#8:7:5032:3840
+#8:-1:5033:4404
+#98436739867439843769485798542749827593285729587325:43698764986460981048259837659386739857456983759328457:6943857329857295827698367:3104744730915914415259518
+## bmodpow Error cases
+#8:8:-5:NaN
+#8:-1:16:NaN
+#inf:5:13:NaN
+#5:inf:13:NaN
&bmod
# inf handling, see table in doc
0:inf:0
diff --git a/lib/Math/BigInt/t/calling.t b/lib/Math/BigInt/t/calling.t
index ca78fe98d1..8a0d2bb635 100644
--- a/lib/Math/BigInt/t/calling.t
+++ b/lib/Math/BigInt/t/calling.t
@@ -63,7 +63,7 @@ my $version = '1.46'; # adjust manually to match latest release
my ($func,@args,$ans,$rc,$class,$try);
while (<DATA>)
{
- chop;
+ chomp;
next if /^#/; # skip comments
if (s/^&//)
{
diff --git a/lib/Math/BigInt/t/mbi_rand.t b/lib/Math/BigInt/t/mbi_rand.t
index bbe73c84f3..69be2d4ac4 100644
--- a/lib/Math/BigInt/t/mbi_rand.t
+++ b/lib/Math/BigInt/t/mbi_rand.t
@@ -13,33 +13,35 @@ BEGIN
my $location = $0; $location =~ s/mbi_rand.t//;
unshift @INC, $location; # to locate the testing files
chdir 't' if -d 't';
- $count = 500;
+ $count = 128;
plan tests => $count*2;
}
use Math::BigInt;
my $c = 'Math::BigInt';
-my $length = 200;
+my $length = 128;
# If you get a failure here, please re-run the test with the printed seed
# value as input: perl t/mbi_rand.t seed
my $seed = int(rand(65537)); print "# seed: $seed\n"; srand($seed);
-my ($A,$B,$ADB,$AMB,$la,$lb);
+my ($A,$B,$As,$Bs,$ADB,$AMB,$la,$lb);
+my $two = Math::BigInt->new(2);
for (my $i = 0; $i < $count; $i++)
{
# length of A and B
$la = int(rand($length)+1); $lb = int(rand($length)+1);
- $A = ''; $B = '';
+ $As = ''; $Bs = '';
# we create the numbers from "patterns", e.g. get a random number and a
# random count and string them together. This means things like
# "100000999999999999911122222222" are much more likely. If we just strung
# together digits, we would end up with "1272398823211223" etc.
- while (length($A) < $la) { $A .= int(rand(100)) x int(rand(16)); }
- while (length($B) < $lb) { $B .= int(rand(100)) x int(rand(16)); }
- $A = $c->new($A); $B = $c->new($B);
+ while (length($As) < $la) { $As .= int(rand(100)) x int(rand(16)); }
+ while (length($Bs) < $lb) { $Bs .= int(rand(100)) x int(rand(16)); }
+ $As =~ s/^0+//; $Bs =~ s/^0+//;
+ $A = $c->new($As); $B = $c->new($Bs);
# print "# A $A\n# B $B\n";
if ($A->is_zero() || $B->is_zero())
{
@@ -49,11 +51,11 @@ for (my $i = 0; $i < $count; $i++)
# $X = ($A/$B)*$B + 2 * ($A % $B) - ($A % $B);
($ADB,$AMB) = $A->copy()->bdiv($B);
print "# ". join(' ',Math::BigInt::Calc->_base_len()),"\n"
- unless ok ($ADB*$B+2*$AMB-$AMB,$A);
+ unless ok ($ADB*$B+$two*$AMB-$AMB,$As);
# swap 'em and try this, too
# $X = ($B/$A)*$A + $B % $A;
($ADB,$AMB) = $B->copy()->bdiv($A);
print "# ". join(' ',Math::BigInt::Calc->_base_len()),"\n"
- unless ok ($ADB*$A+2*$AMB-$AMB,$B);
+ unless ok ($ADB*$A+$two*$AMB-$AMB,$Bs);
}
diff --git a/lib/Math/BigInt/t/mbimbf.inc b/lib/Math/BigInt/t/mbimbf.inc
index 968d830a42..1460161e74 100644
--- a/lib/Math/BigInt/t/mbimbf.inc
+++ b/lib/Math/BigInt/t/mbimbf.inc
@@ -596,7 +596,7 @@ my ($ans1,$f,$a,$p,$xp,$yp,$xa,$ya,$try,$ans,@args);
my $CALC = Math::BigInt->config()->{lib};
while (<DATA>)
{
- chop;
+ chomp;
next if /^\s*(#|$)/; # skip comments and empty lines
if (s/^&//)
{
diff --git a/lib/Math/BigInt/t/upgrade.inc b/lib/Math/BigInt/t/upgrade.inc
index bf35261b80..fc70873e0c 100644
--- a/lib/Math/BigInt/t/upgrade.inc
+++ b/lib/Math/BigInt/t/upgrade.inc
@@ -52,7 +52,7 @@ my ($f,$z,$a,$exp,@a,$m,$e,$round_mode,$expected_class);
while (<DATA>)
{
- chop;
+ chomp;
next if /^#/; # skip comments
if (s/^&//)
{