Thread: Odd method of calculating PI in C - Explainations?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    82

    Odd method of calculating PI in C - Explainations?

    From:
    Obfuscated C Code

    We find:
    http://www.cise.ufl.edu/~manuel/obfuscate/pi.c

    To check the output online:
    C code - 21 lines - codepad

    Yes, the program as it is pasted there gives a poor approximation, however... the accuracy of the calculation increases as the circle diameter is increased. I've stared at this for a little while but can't seem to make sense of what's actually going on, do any of you have insight in to the program flow and what the statements are executing?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The International Obfuscated C Code Contest
    Find the entry, read the notes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by since View Post
    I've stared at this for a little while but can't seem to make sense of what's actually going on, do any of you have insight in to the program flow and what the statements are executing?
    I'm not much for math, but the flow of this is pretty straight forward I think. The pattern of the shape is irrelevent, of course. Look at the #define _
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    Never mind, explanations are starting to finally show up here;

    http://www.reddit.com/r/programming/...beautiful_way/

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by since View Post
    Never mind, explanations are starting to finally show up here;

    http://www.reddit.com/r/programming/...beautiful_way/
    I had all of it but the punchline:

    It's F>0 for the first _ of each line, -F for each _ after that.
    So F gets decremented 202 times, but OO only 16.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    It's a shame however the shape doesn't actually represent a circle. In the end it cheats.

    Still, nicely done. I couldn't see it, until I read how it was done.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-10-2007, 05:54 AM
  2. looking for quick method to calculate pi
    By MindlessXD in forum C++ Programming
    Replies: 16
    Last Post: 07-19-2006, 03:59 PM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  4. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM
  5. Replies: 8
    Last Post: 02-23-2002, 09:50 PM