Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > 'long long integer' in Perl

Reply
Thread Tools

'long long integer' in Perl

 
 
Swapnajit Mitra
Guest
Posts: n/a
 
      02-15-2006
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.

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      02-15-2006
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

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      02-15-2006
"Swapnajit Mitra" <(E-Mail Removed)> 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
 
Reply With Quote
 
Swapnajit Mitra
Guest
Posts: n/a
 
      02-25-2006
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
================================================== =================

 
Reply With Quote
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      02-25-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
Use of Long and Long Long Bart C C Programming 27 01-15-2008 05:27 AM
long long and long Mathieu Dutour C Programming 4 07-24-2007 11:15 AM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
Assigning unsigned long to unsigned long long George Marsaglia C Programming 1 07-08-2003 05:16 PM



Advertisments