spacer
{Arrest This Man, He Talks In Maths } spacer

Blog : Archives : Homepage

With your feet in the air, and your head on the ground . . .

spacer
spacer

{Wednesday, October 05, 2005}

 
I don't remember how it came up, but last night I was reminded of this program I heard of when I was an undergrad, that calculates pi by computing its own area. My memory was hazy, but I managed to navigoogle my way to a wikipedia page on it:

#define _ -F<00||--F-OO--;
int F=00,OO=00;main(){F_OO();printf("%1.3f\n",4.*-F/OO/OO);}F_OO()
{
_-_-_-_
_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_
_-_-_-_
}
(Okay, I can't get the spacing to come out right at all; properly formatted, it actually does look like a circle. Look at the wikipedia page.)

My new roommates (both programmers) were agog, and we spent some time trying to decipher it, last night. It's pretty neat; this program won an award for "Best Abuse of the Preprocessor", and deservedly so. Here's what we could make of it: The #define line at the top defines the "_" symbol to mean the statement/fragment "-F<00||--F-OO--;" . . . so on compilation, that statement is substituted in everywhere the "_" symbol occurs in the main program (or, actually, in the F_OO() function.) Because of the "--F" and "OO--" clauses, every time this fragment executes, it changes the values of the (globally defined) variables "F" and "OO". Somehow, it does so very cleverly, such that F comes out to the area, and OO comes out to 1/2 the radius. I think. Actually, in no way do I actually see how it does that. But apparently it does.

Pretty cool.

posted by Miles 9:35 AM

Comments: Post a Comment
spacer