'long long integer' in Perl

S

Swapnajit Mitra

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.
 
P

Paul Lalli

Swapnajit said:
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
 
X

xhoster

Swapnajit Mitra said:
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
 
S

Swapnajit Mitra

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
===================================================================
 
J

jl_post

Swapnajit said:
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
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top