So, last spring I did a series of posts on making a Pi spigot. Unfortunately, the project foundered a bit, as it turned out that only Rakudo had the subsignatures needed to make the code pretty, but only Niecza had the big integers needed to make the code work.
Fast forward to today. Now both Rakudo and Niecza can handle the best version of the code with no problem!
Let me emphasize that again. A year ago, neither implementation had those two features. Eight months ago, each had one. Today both have both. What’s more, Rakudo seems to have made some pretty impressive speed improvements.
I think the awkward situation with Rakudo Star has helped obscure the fantastic good news in Perl 6. Right now we have two distinct Perl 6 implementations, each of which is markedly better than anything available a year ago. While there are still some rough edges, both implementations are making visible progress every single day.
Okay, enough cheerleading. Time to finish the Pi story. It turned out there was one last complication. I had never actually tried to use the code to generate an infinite stream of Pi. I always stopped at 40 digits. Confident that the algorithm must work, I tweaked the output to just continue generating digits until you stopped the program with control-C. And I ran it using Niecza, and it printed
And then it just sat there, calculating. After a few minutes I stopped it and tried it in Rakudo, and got the exact same result.
Well, after adding a few debugging
says here and there, I found the problem. The
extr sub was returning
NaN. It turns out that both compilers have issues with dividing huge numbers. Here’s the division operation that was causing the problem:
70777782797813415527343750 / 7640045443
Turns out the bottom number is bigger than can be presented by a
Num, so the division operation ends up becoming
N / Inf, which is
NaN. This is a bit obscured in Rakudo because the division operation actually returns a
Rat (which should be illegal according to the spec!), but then
.floor is called on the result, which tries to convert to
Num before doing the floor operation.
This opens several questions, like: Should Perl 6′s
Int / Int operator be modified to try to cope with this sort of case?
But for the Pi problem, the good news is this: every time the
extr sub is called, its result is fed to
.floor. That means we simply needed to replace
div and get rid of the
And with that, the code easily produces 2,000+ digits of Pi using either Rakudo or Niecza!