bigint vs. bignum

A

Alex

Hello, I'm new to Perl

I have a small program, which uses only (big) integers, but works
differently with "use bigint" and "use bignum". Can you please explain
why?

sub Next($)
{
my $var = shift;
my $tst = $var % 2;
return ($tst == 0) ? $var / 2 : $var * 3 + 1;
}

my $n = 2 ** 1000 - 1;
my $counter = 0;

while (1)
{
$n = Next($n);
if ($n == 1)
{
print("counter = ", $counter, "\n");
last;
}
$counter++;
}

Thank you in advance
Alex
 
X

xhoster

Alex said:
Hello, I'm new to Perl

I have a small program, which uses only (big) integers, but works
differently with "use bigint" and "use bignum". Can you please explain
why?

sub Next($)
{
my $var = shift;
my $tst = $var % 2;
return ($tst == 0) ? $var / 2 : $var * 3 + 1;

use bignum turns on upgrading.

When the division by 2 happens, the BigInt upgrades to BigFloat. It
doesn't look to see if the division results in an integer before
upgrading--it just does it.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top