|
Prev: using LWP to POST data via "javascript:document.form.submit()" link
Next: How to read a perl script from withink itself
From: gsarup on 5 Jan 2006 16:53 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
From: Brian Wakem on 5 Jan 2006 17:11 gsarup(a)gmail.com wrote: > 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; -- Brian Wakem Email: http://homepage.ntlworld.com/b.wakem/myemail.png
From: gsarup on 5 Jan 2006 17:15 Thanks for the reply Brian. In the line: $chksum=$chksum+$chk; where do I put in the BigInt command? Thanks Gaurav
From: Anno Siegel on 5 Jan 2006 17:21 <gsarup(a)gmail.com> wrote in comp.lang.perl.misc: > 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 -- If you want to post a followup via groups.google.com, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers.
From: Brian Wakem on 5 Jan 2006 17:23
gsarup(a)gmail.com wrote: > Thanks for the reply Brian. > > In the line: > > $chksum=$chksum+$chk; > > where do I put in the BigInt command? I've never used the module. http://search.cpan.org/~tels/Math-BigInt-1.77/lib/Math/BigInt.pm -- Brian Wakem Email: http://homepage.ntlworld.com/b.wakem/myemail.png |