I suspect everyone who plays around with numerical problems on Perl 6 has been as frustrated as I am not to have arbitrary precision integers in Rakudo. As a sort of first step toward getting them, I’ve thrown together the beginnings of a Math::BigInt module using the BIgDigit multiple-precision arithmetic library and Zavolaj.

(Why BigDigit instead of GMP? Two simple reasons. First, building GMP failed on my main development machine (OS X 10.5). GMP’s suggestion for fixing the problem involved getting a new main development environment for the machine, which seemed like a major pain in the neck, and didn’t impress me with GMP’s portability. Second, GMP’s main multiple-precision type is a C struct you store on the stack. That means using it with Zavolaj would have required creating wrappers for every GMP function I wanted to use. Yuck.)

Without further ado, here’s a few examples:

> my @fib := Math::BigInt.new(1), Math::BigInt.new(1), *+* ... *;
> say @fib[200];
453973694165307953197296969697410619233826
> my @powers-of-two := Math::BigInt.new(1), * * 2 ... *;
> say @powers-of-two[80];
1208925819614629174706176

From which you can quickly conclude two things: Math::BigInts easily worked with the sequence operator. And creating a Math::BigInt is ugly and awkward. Once again, blogging has forced me to confront the limitations of my code.

So… if I had an operator to create a BigInt, the code would look a lot nicer. What’s more, you could avoid the term Math::BigInt actually appearing in your code, making it very easy to port to Perl 6s which actually support arbitrary precision Ints. Clearly then, I need to figure out a symbol to use…

### Like this:

Like Loading...

*Related*

This entry was posted on January 29, 2011 at 2:01 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

January 30, 2011 at 7:06 pm |

Re: “Clearly then, I need to figure out a symbol to use…”

How about a capital B at the end if the number literal, kind of like the way L, F, or D is used in some languages to force a long, float, or double literal?

my @powers-of-two := 1B, **2 … *;

January 30, 2011 at 10:26 pm |

I’ve actually taken sorear++’s suggestion (on #perl6) of postfix L, since it already has the connotation of “long int” from other languages — these are just arbitrarily long ints!

January 30, 2011 at 7:08 pm |

…although now that I look at that, it would be difficult when writing the literal in hex. E.g., 0x1B = ?

Maybe a (B) in parenthesis? my $bigint = 1(B) ?

Are you allowed to make up syntax like that?