Don't Memorize Pi

brian_d_foy on 2006-04-21T16:05:18

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;


Same thing only different

Mr. Muskrat on 2006-04-21T16:30:44

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)

ferreira on 2006-04-21T23:59:58

use constant PI => 2 * atan2(1, 0);

A more boring alternative...

itub on 2006-04-21T18:34:45

#!/usr/bin/perl

use Math::Trig;

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

Re:A more boring alternative...

brian_d_foy on 2006-04-21T19:22:32

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...

runrig on 2006-04-21T19:25:36

use Math::NumberCruncher qw($PI);

print PI, "\n";

Re:If you want a few more digits...

brian_d_foy on 2006-04-21T20:13:34

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...

runrig on 2006-04-21T22:53:02

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...

brian_d_foy on 2006-04-21T22:58:07

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...

Damian on 2006-04-21T22:15:53

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 everyday calculations. ;-)

Re:My personal favorite...

brian_d_foy on 2006-04-21T22:39:55

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

n1vux on 2006-04-24T16:18:47

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.