'long long integer' in Perl

Discussion in 'Perl Misc' started by Swapnajit Mitra, Feb 15, 2006.

  1. This must be a simple question for the gurus. I would like to assign a
    bigger than 32-bit integer to a variable. Essentially this variable has
    to be a 'long long' (64 bit on my machine).

    $variable = 0xFFFFFFFFA; # 36 bits

    How do I do this? Using no special syntax produces this message:

    Integer Overflow in hexadecimal number.

    Thanks.
    Swapnajit Mitra, Feb 15, 2006
    #1
    1. Advertising

  2. Swapnajit Mitra

    Paul Lalli Guest

    Swapnajit Mitra wrote:
    > This must be a simple question for the gurus. I would like to assign a
    > bigger than 32-bit integer to a variable. Essentially this variable has
    > to be a 'long long' (64 bit on my machine).
    >
    > $variable = 0xFFFFFFFFA; # 36 bits
    >
    > How do I do this? Using no special syntax produces this message:
    >
    > Integer Overflow in hexadecimal number


    perldoc bigint
    perldoc Math::BigInt

    Paul Lalli
    Paul Lalli, Feb 15, 2006
    #2
    1. Advertising

  3. Swapnajit Mitra

    Guest

    "Swapnajit Mitra" <> wrote:
    > This must be a simple question for the gurus. I would like to assign a
    > bigger than 32-bit integer to a variable. Essentially this variable has
    > to be a 'long long' (64 bit on my machine).
    >
    > $variable = 0xFFFFFFFFA; # 36 bits
    >
    > How do I do this? Using no special syntax produces this message:
    >
    > Integer Overflow in hexadecimal number.


    It works as is on perls (or, at least on one perl) built for 64 bit
    machines.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Feb 15, 2006
    #3
  4. Thanks all those who replied. I have used the bigint module and it has
    solved the problem.

    However, it has introduced a new one - if I have a construct such as

    if ($an_integer == 0) {
    ...

    where $an_integer is not necessarily a long long, when I try to run
    perl in debug mode (-d), it seems the comparison is done in a separate
    subroutine. I can always press 'n' (next) rather than 's' (step), but
    it is not clear to me beforehand what are the native operators (such as
    '==' in this case) are replaced by underlying subroutines. Can anybody
    provide a list of such operators?

    - Swapnajit.
    ===================================================================
    Verilog, SystemVerilog and all good stuffs
    Project VeriPage: http://www.project-veripage.com
    ===================================================================
    Swapnajit Mitra, Feb 25, 2006
    #4
  5. Swapnajit Mitra

    Guest

    Swapnajit Mitra wrote:
    >
    > However, it has introduced a new one - if I have a construct such as
    >
    > if ($an_integer == 0) {
    > ...
    >
    > where $an_integer is not necessarily a long long, when I try to run
    > perl in debug mode (-d), it seems the comparison is done in a separate
    > subroutine. I can always press 'n' (next) rather than 's' (step), but
    > it is not clear to me beforehand what are the native operators (such as
    > '==' in this case) are replaced by underlying subroutines.



    Dear Swapnajit,

    You can actually look at the code of the Math::BigInt module with
    the command:

    perldoc -m Math::BigInt

    Near the top of that file, you will see many operators being
    overloaded, like:

    use overload
    '=' => sub { $_[0]->copy(); },
    '-' => sub { my $c = $_[0]->copy; $_[2] ?
    $c->bneg()->badd($_[1]) :
    $c->bsub( $_[1]) },
    '+' => sub { $_[0]->copy()->badd($_[1]); }
    '+=' => sub { $_[0]->badd($_[1]); },
    '-=' => sub { $_[0]->bsub($_[1]); },
    '*=' => sub { $_[0]->bmul($_[1]); },
    '/=' => sub { scalar $_[0]->bdiv($_[1]); },
    '%=' => sub { $_[0]->bmod($_[1]); },
    ....

    Which operators are overloaded (and how) depends on the version of
    Math::BigInt you use, so you might see a slightly different version of
    what I see.

    To see what happens when you use the '==' operator on a
    Math::BigInt, try running a little program at the command prompt in
    debug in debug mode, like this:

    perl -MMath::BigInt -wde "Math::BigInt->new(1e15) == 0"

    This program will put you in debug mode, ready to create a large number
    (1*10^15) and then compare it to zero.

    Pressing "s" right away will put you in the new() method (the method
    that creates the BigInt). This step has to happen before the '=='
    operator is executed. At this point, press "r" to return from this
    subroutine and start executing the '==' operator.

    Once you press "r", you might see something like this:

    69: '<=>' => sub { $_[2] ?
    70: ref($_[0])->bcmp($_[1],$_[0]) :
    71: $_[0]->bcmp($_[1])},

    (Again, what you see will depend on what version of Math::BigInt you
    are using.)

    If you see the same output I see, it means that the Math::BigInt
    module will use the overloaded '<=>' operator to do the work that you
    expect the '==' operator to do, and that is to call $_[0]->bcmp($_[1])
    (that is, the Math::BigInt::bcmp() routine).

    Of course, you can step into the Math::BigInt::bcmp() routine to see
    what it does with the debugger's "s" command.


    > Can anybody provide a list of such operators?


    Well, theoretically, the operators that the Math::BigInt module is
    compatible with are the same operators that work on a standard integer.
    But if you really want to see a list of these operators, just open up
    the Math::BigInt module (with the "perldoc -m Math::BigInt" command)
    and look for what is being overloaded (near the top of the file).

    You'll see a list of standard mathematical operations, but there
    might be a few missing (like '=='). In such cases, those operators are
    handled by other operators (like '<=>') that are used to derive the
    needed information.

    I hope this helps, Swapnajit.

    -- Jean-Luc Romano
    , Feb 25, 2006
    #5
    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. Dan Pop
    Replies:
    0
    Views:
    1,168
    Dan Pop
    Jun 24, 2003
  2. cyberdude
    Replies:
    2
    Views:
    5,104
    Keith Thompson
    Jun 25, 2003
  3. music4
    Replies:
    3
    Views:
    896
    CBFalconer
    Jan 5, 2004
  4. Replies:
    7
    Views:
    928
    Maxim Yegorushkin
    Jun 15, 2006
  5. Suresh V
    Replies:
    5
    Views:
    3,677
    SaticCaster
    Jul 5, 2010
Loading...

Share This Page