It is a well known fact in Mathematics that Pi is 4*arctan(1). arctan(x) is the integral from 0 to x of (1/(1+x*x)) or, if you're not mathematically inclined, it is the area between the x axis and 1/(1+x*x) and between the y-axis and x. So, the for loop is simulating this integration. Of course, the better way to do this is to do a Taylor expansion of arctan.
JDM On Fri, 12 Mar 1999, Bal K. Paudyal wrote: > Hello Friends, > > I encountered following program in one of the Linux Howtos. This calculates > the value of pai. But how does it do this? I am not asking the programming > details, but on what theory the formula is based on. Can anybody help? Is > there any better place to look for help? > > > > --------------------------------------- > #include <stdlib.h>; > #include <stdio.h>; > > main(int argc, char **argv) > { > register double width, sum; > register int intervals, i; > > /* get the number of intervals */ > intervals = atoi(argv[1]); > width = 1.0 / intervals; > > /* do the computation */ > sum = 0; > for (i=0; i<intervals; ++i) { > register double x = (i + 0.5) * width; > sum += 4.0 / (1.0 + x * x); > } > sum *= width; > > printf("Estimation of pi is %f\n", sum); > > return(0); > } > -------------------------------- > > > -- > Unsubscribe? mail -s unsubscribe [EMAIL PROTECTED] < /dev/null > >