how to generate pi in c
Dean S. Messing
deanm at sharplabs.com
Tue Nov 9 00:09:12 UTC 2010
The following program will compute pi to three (3) places
(approximately :-)
#define _ -F<00 || --F-OO--;
int F=00,OO=00;
main(){F_OO();printf("%1.3f\n", 4.*-F/OO/OO);}F_OO()
{
_-_-_-_
_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_
_-_-_-_
}
Be sure to compile like this:
gcc -traditional-cpp pi_calc.c -o pi_calc.x
Then run like this:
./pi_calc.x
W/o the "-traditional-cpp" switch you will, instead, get an excellent
approximation to 1/4.
Excercise: Discover how the program works?
Once you do, you will know how to make it more accurate, so
that you can compute pi to _any_ finite precision you like.
:-)
This code was the winner of one of the wonderful "Obfuscated C" contests
in the early 1990's or late 1980's.
Dean
More information about the users
mailing list