[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

C Program confused me



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);
  }
--------------------------------


Reply to: