Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot


by root (Monk)
on Dec 23, 1999 at 01:30 UTC ( [id://1285]=perlfunc: print w/replies, xml ) Need Help??


See the current Perl documentation for Math::BigInt.

Here is our local, out-dated (pre-5.6) version:

Math::BigInt - Arbitrary size integer math package

  use Math::BigInt;
  $i = Math::BigInt->new($string);

  $i->bneg return BINT               negation
  $i->babs return BINT               absolute value
  $i->bcmp(BINT) return CODE         compare numbers (undef,<

All basic math operations are overloaded if you declare your big integers as

  $i = new Math::BigInt '123 456 789 123 456 789';
Canonical notation

Big integer value are strings of the form /^[+-]\d+$/ with leading zeros suppressed.


Input values to these routines may be strings of the form /^\s*[+-]?[\d\s]+$/.


Output values always always in canonical form

Actual math is done in an internal format consisting of an array whose first element is the sign (/^[+-]$/) and whose remaining elements are base 100000 digits with the least significant digit first. The string 'NaN' is used to represent the result when input arguments are not numbers, as well as the result of dividing by zero.


   '+0'                            canonical zero value
   '   -123 123 123'               canonical value '-123123123'
   '1 23 456 7890'                 canonical value '+1234567890'

Autocreating constants

After use Math::BigInt ':constant' all the integer decimal constants in the given scope are converted to Math::BigInt. This conversion happens at compile time.

In particular

  perl -MMath::BigInt=:constant -e 'print 2**100'

print the integer value of 2**100. Note that without convertion of constants the expression 2**100 will be calculatted as floating point number.


The current version of this module is a preliminary version of the real thing that is currently (as of perl5.002) under development.


Mark Biggar, overloaded interface by Ilya Zakharevich.

Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-06-13 00:20 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.