in reply to Arithmetic on numbers bigger than 1<<31

If you want to work with big integers, try Math::BigInt, a pure perl module, or Math::BigInt::GMP, an XS module:
use Math::BigInt; # or make it faster: install (optional) Math::BigInt::GMP # and always use (it will fall back to pure Perl if the # GMP library is not installed): use Math::BigInt lib => 'GMP'; my $str = '1234567890'; $x = Math::BigInt->new($str); # defaults to 0 $y = $x->copy(); # make a true copy $x->badd($y); # addition (add $y to $x) $x->bsub($y); # subtraction (subtract $y from $x) $x->bstr(); # convert to a normalized string $x->bsstr(); # normalized string in scientific no +tation $x->as_hex(); # as signed hexadecimal string with +prefixed 0x $x->as_bin(); # as signed binary string with prefi +xed 0b # etc.

-Mark

Replies are listed 'Best First'.
Re^2: Arithmetic on numbers bigger than 1<<31 (not so big)
by tye (Sage) on May 13, 2004 at 20:15 UTC

    No need to resort to "big int" modules if you only need 10 digits. Every version of Perl can do arithmatic with exact precision on integers at least to 15 digits. If you divide and wish to get an integer result, then use int which removes the fractional part of the number but doesn't truncate the value to fit in an IV.

    That is, a C data type of "double" (NV) can handle integers (and rather large ones at that) quite well and Perl does this. And Perl's int() doesn't convert to a C data type like "int" or "long" (IV).

    - tye