Click here to get back home

Problem around bignum

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Problem around bignum gamo 03-28-2006
Posted by gamo on March 28, 2006, 6:04 am
Please log in for more thread options


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--

Posted by Sisyphus on March 29, 2006, 2:06 am
Please log in for more thread options




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



Posted by gamo on March 29, 2006, 5:53 am
Please log in for more thread options


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

Similar ThreadsPosted
bignum 0.22 July 4, 2007, 9:43 am
bignum incompatible with loos_like_number ??? June 26, 2007, 10:05 pm
bignum incompatible with looks_like_number() ??? June 26, 2007, 10:17 pm
Error in perl module "bignum"... May 14, 2006, 4:31 pm
Problem with Net::FTP July 21, 2004, 12:40 pm
DBI - DBD-DB2 Problem - Please help June 2, 2005, 11:07 pm
Net::FTP->problem with put February 28, 2006, 2:11 am
Re: Problem with DBD::DB2 on AIX. September 20, 2006, 2:10 pm
net::telnet problem July 23, 2004, 6:46 pm
Lwp Post Problem September 1, 2004, 4:09 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap