For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > February 2006 > 'long long integer' in Perl









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author 'long long integer' in Perl
Swapnajit Mitra

2006-02-15, 6:58 pm

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.

Paul Lalli

2006-02-15, 6:58 pm

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

Jim Gibson

2006-02-15, 6:58 pm

In article <1140031818.983987.269120@z14g2000cwz.googlegroups.com>,
Swapnajit Mitra <mittra@juno.com> 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.


Use the Math::BigInt module, which should be part of your basic Perl
installation. If not, it is available at www.cpan.org.

For documentation, type

perldoc Math::BigInt

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
xhoster@gmail.com

2006-02-15, 6:58 pm

"Swapnajit Mitra" <mittra@juno.com> 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
Swapnajit Mitra

2006-02-25, 6:57 pm

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

jl_post@hotmail.com

2006-02-25, 6:57 pm

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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com