bigint
use bigint;
This pragma bypasses the architecture-dependent treatment of
integer operations to work with very large numbers, as well as handling
the special value NaN (for not a
number):
use bigint; say 2 ** 512;
This pragma works by overloading the numeric operators to use
Math::BigInt to compute values. As such, it can be considerably slower
than built-in operations. Still, slow right answers are always better than
fast wrong ones.
You can load different implementing libraries, which may vary in
performance. By default, bigint uses a
pure Perl implementation, but you can load a faster library if you have
it:
use bigint lib => 'GMP';
You can set the accuracy, which determines the number of significant digits in the answer:
use bigint a => 2;
Or, you can set the precision, which specifies the magnitude of the answer. A precision less than 0 is ignored:
use bigint p => –2; # to the hundredths place (ignored) use bigint p => 1; # rounded to 10
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access