Fun with Math::BigFloat and #perl

n1vux on 2006-01-29T04:51:52

I'm not usually an irc-nik, but I had fun tonight thanks to trying #perl on freenode. Pete_I posted a query for help with a "borked" program to compute Pi, and was ignored by the folks busy debating VI vs Emacs. (Do they do that daily or only on weekends?)

Hoping to be helpful, and out of curiosity -- I have always loved math, or at least since 2nd grade -- I clicked on his paste link. It referenced a website by someone I vaguely knew and certainly knew of 20 years ago when our research areas overlapped and have seen in the math literature since then, David H. Bailey. I remember noting his breakthrough on direct computation of digits of pi (in HEX), and his website to search for your name (in ascii) in Pi, but I had not yet played with his new algorithm. I had played with searching for strings of DIGITS in a pre-computed Pi of a mere 10_000 decimal digits with Perl last year. Dave's 5-bit ascii search in Pi searches 4_000_000_000 bits, 5 orders longer.

So I asked Pete_I what was wrong, but the only reply was a punch-line just the facts ma'am from a sniper. So after a short break, I asked him by direct message. It seemed his coding of the algorithm was losing precision after 10-15 decimal places, even though it was using Math::BigFloat.

After several false starts -- and reading the academic paper three links into DBH's pages -- I found that, unless we used the modular arithmetic hacks DBH used to avoid using arbitrary precision packages, we needed to increase the default precision and use BigFloat for all numeric variables, not just $pi. In the process, I refactored the code to compute the sequence of the series first and then evaluate the polynomial by the classical add-and-divide technique.

My final code differs from Pete_I's code primarily in reading in, printing, and subtracting from the comparison value. (And commenting out Acme::Comment ;-)