luckyrefa.blogg.se

Calculate pi
Calculate pi













Recursion to any great depth, is usually impractical for a commercial program, but tail recursion allows an algorithm to be expressed recursively, while implemented as a loop. An easy start point would be x = r, y = 0, and then to count c the number of steps until x <= 0 to plot a quater of a circle. h also wants to be the radius r of your circle. H is usually a power of 2 so that the divide can be done easily with a shift (or subtracting from the exponent on a double). In computer graphics you can plot/draw a circle with its centre at 0,0 from a initial point x,y, the next point x',y' can be found using a simple formula: What is PI? The circumference of a circle divided by its diameter.

calculate pi

It was taught me by a physics professor when I was an undergrad. None will ever be perfect, and you have to sort them out by intended use.ģ55/113 is an old Chinese estimate, and I believe it pre-dates 22/7 by many years. The lesson you get from estimating PI is that there are lots of ways of doing it, Which is probably good out as far as it goes. to /usr/include/math.h on my box, M_PI is #define'd as:

calculate pi calculate pi

Note that 22/7 gives you: 3.14285714, which is wrong at the thousandths.ģ55/113 gives you 3.14159292 which isn't wrong until the ten-millionths.Īcc. In some cases, this is even good enough for integer arithmetic: multiply by 355 then divide by 113.ģ55/113 is also easy to commit to memory (for some people anyway): count one, one, three, three, five, five and remember that you're naming the digits in the denominator and numerator (if you forget which triplet goes on top, a microsecond's thought is usually going to straighten it out). Is 355/113, which is good to several decimal places (at least three or four, I think). There are a couple of really, really old tricks I'm surprised to not see here.Ītan(1) = PI/4, so an old chestnut when a trustworthy arc-tangent function isĪ very cute, fixed-ratio estimate that makes the old Western 22/7 look like dirt















Calculate pi