Problem around bignum

Problem around bignum

am 28.03.2006 13:04:33 von gamo

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

--8323328-892855016-1143543873=:9612
Content-Type: TEXT/PLAIN; charset=ISO-8859-1
Content-Transfer-Encoding: QUOTED-PRINTABLE


I have a question: Why bignum fails?

#!/usr/local/bin/perl -w

use bignum;

$DIV=3D 7**3;

$I =3D combi(2006,292)%$DIV;
print "$I\n";
$I =3D combi(2006,292) % 7;
print "$I\n";
$I =3D combi(2006,418)%$DIV;
print "$I\n";

sub combi {
my ($n,$m) =3D @_;
$nn=3D$mm=3D1;
for (2..$m){
=09$mm *=3D $_;
}
for (($n-$m+1)..$n){
=09$nn *=3D $_;
}
my $r =3D $nn/$mm;
return $r;
}
__END__
WRONG!


#!/usr/local/bin/perl -w

use GMP::Mpz qw(mpz);

$DIV=3D mpz(7**3);

$I =3D mpz(0);

$I =3D combi(2006,292)%$DIV;
print "$I\n";
$I =3D combi(2006,292) % 7;
print "$I\n";
$I =3D combi(2006,418)%$DIV;
print "$I\n";


sub combi {
my ($n,$m) =3D @_;
my $nn=3D mpz(1);
my $mm =3D mpz(1);
my $r =3D mpz(0);
for (2..$m){
=09$mm *=3D $_;
}
for (($n-$m+1)..$n){
=09$nn *=3D $_;
}
$r =3D $nn/$mm;
return $r;
}
__END__
RIGHT!

Note that use of $I->bmod($DIV) doesn't change the results.
TIA.

--=20
http://www.telecable.es/personales/gamo/
S=F3lo hay 10 tipos de personas, las que saben binario y las que no
perl -e 'print 111_111_111**2,"\n";'
--8323328-892855016-1143543873=:9612--

Re: Problem around bignum

am 29.03.2006 09:06:58 von Sisyphus

"gamo" wrote in message
news:Pine.LNX.4.64.0603281256140.9612@jvz.es...

I have a question: Why bignum fails?

#!/usr/local/bin/perl -w

use bignum;

$DIV= 7**3;

$I = combi(2006,292)%$DIV;
print "$I\n";
$I = combi(2006,292) % 7;
print "$I\n";
$I = combi(2006,418)%$DIV;
print "$I\n";

sub combi {
my ($n,$m) = @_;
$nn=$mm=1;
for (2..$m){
$mm *= $_;
}
for (($n-$m+1)..$n){
$nn *= $_;
}
my $r = $nn/$mm;
return $r;
}
__END__
WRONG!


#!/usr/local/bin/perl -w

use GMP::Mpz qw(mpz);

$DIV= mpz(7**3);

$I = mpz(0);

$I = combi(2006,292)%$DIV;
print "$I\n";
$I = combi(2006,292) % 7;
print "$I\n";
$I = combi(2006,418)%$DIV;
print "$I\n";


sub combi {
my ($n,$m) = @_;
my $nn= mpz(1);
my $mm = mpz(1);
my $r = mpz(0);
for (2..$m){
$mm *= $_;
}
for (($n-$m+1)..$n){
$nn *= $_;
}
$r = $nn/$mm;
return $r;
}
__END__
RIGHT!

Note that use of $I->bmod($DIV) doesn't change the results.
TIA.

------------------------------------------------------------ ----------------
-----
------------------------------------------------------------ ----------------
-----

Although both of your combi() subroutines calculate the same values for both
$nn and $mm (which you can verify with appropriate print statements), the
"bignum" version of combi() miscalculates $r - while the GMP::Mpz version of
combi() calculates $r correctly. (Again you can verify that the returns are
different by simply 'print $r;'.)

I don't know why bignum gets it wrong - but you could let the author know
about the bug. (Try to create a simpler demo for him, however - or at least
tell him that the problem is with the calculation of $r, rather than making
him work that out for himself.) Personally, if I wanted to use pure perl
biginteger routines I would just 'use Math::BigInt;' rather than 'use
bignum;'. If you did switch to 'use Math::BigInt;' I think you would find
that the bug goes away - yielding a correct answer.

But .... if I had GMP::Mpz (which I do) or any other XS interface to the GMP
library (which I also do) then I wouldn't even want to use "pure perl
biginteger routines" at all :-)

Cheers,
Rob

Re: Problem around bignum

am 29.03.2006 12:53:28 von gamo

On Wed, 29 Mar 2006, Sisyphus wrote:

> ------------------------------------------------------------ ----------------
> -----
>
> Although both of your combi() subroutines calculate the same values for both
> $nn and $mm (which you can verify with appropriate print statements), the
> "bignum" version of combi() miscalculates $r - while the GMP::Mpz version of
> combi() calculates $r correctly. (Again you can verify that the returns are
> different by simply 'print $r;'.)
>
> I don't know why bignum gets it wrong - but you could let the author know
> about the bug. (Try to create a simpler demo for him, however - or at least
> tell him that the problem is with the calculation of $r, rather than making
> him work that out for himself.) Personally, if I wanted to use pure perl
> biginteger routines I would just 'use Math::BigInt;' rather than 'use
> bignum;'. If you did switch to 'use Math::BigInt;' I think you would find
> that the bug goes away - yielding a correct answer.
>
> But .... if I had GMP::Mpz (which I do) or any other XS interface to the GMP
> library (which I also do) then I wouldn't even want to use "pure perl
> biginteger routines" at all :-)
>
> Cheers,
> Rob
>

Thank you very much for your help, in many times.
Cheers