Perl Integer Length

G

gsarup

Hi,

I am encountering an unusual problem. I have a perl script that adds
the sum of all 6 digit integers in a file. The problem is that ever so
often the sum exceeds the maximum allowable limit for integer size.
When that happens the program appends a '-'ve sign to the sum and
starts to subtract the numbers rather than add them.

The problem is compounded becauce I am also using 'sprintf' to ensure
that the number contains 15 characters in the output. When I remove the
sprintf command, the programs works correctly.

My question is this:

1) Does perl have an equivalent of 'long' in Java for integers?
2) Why does sprintf affect the sum of the result? It does not happen
all the time.

I would appreciate any help on this.

Thanks
Gaurav
 
B

Brian Wakem

Hi,

I am encountering an unusual problem. I have a perl script that adds
the sum of all 6 digit integers in a file. The problem is that ever so
often the sum exceeds the maximum allowable limit for integer size.
When that happens the program appends a '-'ve sign to the sum and
starts to subtract the numbers rather than add them.

The problem is compounded becauce I am also using 'sprintf' to ensure
that the number contains 15 characters in the output. When I remove the
sprintf command, the programs works correctly.

My question is this:

1) Does perl have an equivalent of 'long' in Java for integers?
2) Why does sprintf affect the sum of the result? It does not happen
all the time.


use Math::BigInt;
 
G

gsarup

Thanks for the reply Brian.

In the line:

$chksum=$chksum+$chk;

where do I put in the BigInt command?

Thanks
Gaurav
 
A

Anno Siegel

Hi,

I am encountering an unusual problem.

Not unusual at all.
I have a perl script that adds
the sum of all 6 digit integers in a file. The problem is that ever so
often the sum exceeds the maximum allowable limit for integer size.
When that happens the program appends a '-'ve sign to the sum and
starts to subtract the numbers rather than add them.

You are misinterpreting your results. Perl is building the sum
all right, unless you do things you didn't mention.
The problem is compounded becauce I am also using 'sprintf' to ensure
that the number contains 15 characters in the output. When I remove the
sprintf command, the programs works correctly.

My question is this:

1) Does perl have an equivalent of 'long' in Java for integers?

Perl has only one type of number. When the native integer range is
exceeded, the internal representation switches to the largest available
float. You can add a lot of six-digit numbers before you get out of
range.
2) Why does sprintf affect the sum of the result? It does not happen
all the time.

Your problem is printf. Unfortunately you didn't show your code, but
I'm willing to bet you are using a format like "%15d". That is wrong
when large numbers are involved. Use "%15.0f" and you'll be fine.

Anno
 
G

gsarup

Thanks Anno,

The relevant code is given below:

while($tail=<DISTFILE>)
{
$chk=substr($tail,17,6);
print "chk = $chk\n";
$chksum=$chksum+$chk;
}

$chksum=sprintf("%015d", $chksum);

Thanks
Gaurav
 
A

Anno Siegel

Thanks Anno,

The relevant code is given below:

while($tail=<DISTFILE>)
{
$chk=substr($tail,17,6);
print "chk = $chk\n";
$chksum=$chksum+$chk;
}

$chksum=sprintf("%015d", $chksum);

There you go. If $chksum is >= 2**31 (about 5000 six-digit numbers)
you'll see wrong results. "%015.0f" will show the correct sums.

Anno
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top