How can I use arbitrary integers in Perl?

Is there any standard way to use integers of arbitrary length in Perl? I am working on code that generates an x64 assembly for tests, and I'm tired of manipulating 32 bits at a time.

I am using Perl 5.10.0, for what it's worth it.

+7
integer perl arbitrary-precision
source share
2 answers

If you want to use large integers, you can use bigint , which you can use for the file:

  use bigint; 

or only a limited amount:

  { use bigint; ...; } 

If you need large floating point numbers as well as large integers, you can use the bignum pragma in the same way. In any case, this will slow down your program a bit (or significantly if you do a lot of math), so you should only use them for the parts where you really need them. However, faster is not better than right. :)

If you want very precise control over numbers using big* math, you can use the base classes that implement them and create objects yourself, rather than applying big* semantics to everything. See what Math::Big* modules are.

I talk more about this in the Benchmarking Mastering Perl chapter, because computers that are too fast are currently using factorial as a slow function, and we have also added a large numbers section to the upcoming Effective Perl Programming, 2nd Edition .

+10
source share
+15
source share

All Articles