/* L-31 MCS 507 Wed 7 Nov 2012 : integral4pi_native.c */

/* This script applies the composite trapezoidal rule 
   to the integral of sqrt(1-x^2) for x from 0 to 1,
   to obtain an approximation for pi.
   This native C code allows to compare against the Cython code.
   Compile with full optimization as
   gcc -O3 integral4pi_native.c -o /tmp/integral4pi_native */

#include <stdio.h>
#include <math.h>
#include <time.h>

double circle ( double x )
{
   return sqrt(1-x*x);
}

double integral4pi ( int n )
{
   int i;
   double h = 1.0/n;
   double r = (circle(0)+circle(1))/2;

   for(i=0; i<n; i++)
      r += circle(i*h);
   return 4*r*h;
}

int main ( void )
{
   int n = 100000000;
   clock_t start_time,stop_time;

   start_time = clock();
   double a = integral4pi(n);
   stop_time = clock();

   printf("pi = %.15f\n",a);
   printf("elapsed time = %.3f seconds\n",
          (double) (stop_time-start_time)/CLOCKS_PER_SEC);

   return 0;
}
