NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

## All the Perl that's Practical to Extract and Report

### Journal of brian_d_foy (44)

Friday April 21, 2006
11:05 AM

### Don't Memorize Pi

[ #29402 ]

There's no need to memorize the digits of Pi. Just remember -1.

#!/usr/bin/perl

use Math::Complex;

use constant PI => -1 * log(-1) * sqrt(-1);

printf "Pi is %.12f\n", PI;

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.

Full
Abbreviated
Hidden
• #### Same thing only different(Score:1)

I can't remember where I saw it first but I've always used:
use constant PI => 4 * atan(1);
• #### pi = 2*(pi/2) = 2*atan( 1/0 ) = 2*atan(+Inf)(Score:1)

use constant PI => 2 * atan2(1, 0);
• #### A more boring alternative...(Score:1)

#!/usr/bin/perl

use Math::Trig;

printf "Pi is %.12f\n", pi;
• #### Re:A more boring alternative...(Score:2)

No need for Math::Triig there, since it actually gets pi() from Math::Complex, which implements it as
sub pi () { 4 * CORE::atan2(1, 1) }
• #### If you want a few more digits...(Score:1)

use Math::NumberCruncher qw(\$PI);

print PI, "\n";
• #### Re:If you want a few more digits...(Score:2)

Math::NumberCruncher just hard-codes it. I can't post it here because there is some filter in Slashcode to block posts with "awful long string of letters". Feh
\$PI = new Math::BigFloat "3.141592653589793...";
• #### Re:If you want a few more digits...(Score:1)

Math::NumberCruncher just hard-codes it.

Yeah, I knew that. So it was in a way an answer even more boring than the post I replied to :-) Nothing exciting like using `log(-1) = -πi`. But, having worked on the module, I just had to mention it :-)

• #### Re:If you want a few more digits...(Score:2)

Now I'm kinda wondering if I can coerce Math::BigNum into my solution somehow so it gets a couple hundred decimal places....
• #### My personal favorite...(Score:2)

I understand that Brian was sharing a mathematical elegance with us, rather than actually trying to use PI, but since we're now headed off in that direction...

When I need to approximate PI, I just remember the sequence [1 1 3 3 5 5], rearranging its two halves to get:

my \$PI = 355/113;

which is correct to the first seven decimal places.

That's sufficiently accurate to compute the circumference of the Earth from its radius with an error of about 3 metres (out of 40 million metres). Close enough for most e

• #### Re:My personal favorite...(Score:2)

It's probably exactly the circumference of the earth if you pick the right spot. Short by 3 meters? Just move a bit off the mountain. :)
• #### Pi and e deeply interwoven(Score:1)

Thanks. I've always liked the equation
e ** pi * i  + 1 = 0
since it relates the 5 most important constants. I hadn't realized if one solved this equation for pi that all the coefficients were -1, that's so cool.
--
Bill
# I had a sig when sigs were cool
use Sig;