Perl Integer Length

Discussion in 'Perl Misc' started by gsarup@gmail.com, Jan 5, 2006.

  1. Guest

    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
     
    , Jan 5, 2006
    #1
    1. Advertising

  2. Brian Wakem Guest

    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
     
    Brian Wakem, Jan 5, 2006
    #2
    1. Advertising

  3. Guest

    Thanks for the reply Brian.

    In the line:

    $chksum=$chksum+$chk;

    where do I put in the BigInt command?

    Thanks
    Gaurav
     
    , Jan 5, 2006
    #3
  4. Anno Siegel Guest

    <> 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.
     
    Anno Siegel, Jan 5, 2006
    #4
  5. Brian Wakem Guest

    Brian Wakem, Jan 5, 2006
    #5
  6. Guest

    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
     
    , Jan 5, 2006
    #6
  7. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > 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
    --
    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.
     
    Anno Siegel, Jan 5, 2006
    #7
  8. At 2006-01-05 05:15PM, <> wrote:
    > $chksum=$chksum+$chk;


    Did you know you can write that as:
    $chksum += $chk;

    --
    Glenn Jackman
    Ulterior Designer
     
    Glenn Jackman, Jan 6, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mitchua
    Replies:
    5
    Views:
    2,752
    Eric J. Roode
    Jul 17, 2003
  2. =?Utf-8?B?SG96aQ==?=
    Replies:
    1
    Views:
    6,972
    Ken Cox [Microsoft MVP]
    Jun 2, 2004
  3. Sam
    Replies:
    3
    Views:
    14,123
    Karl Seguin
    Feb 17, 2005
  4. Replies:
    2
    Views:
    5,939
  5. Dan Manes
    Replies:
    1
    Views:
    736
    David Browne
    Apr 23, 2006
Loading...

Share This Page