/* * numerical integration example, as discussed in textbook: * * compute pi by approximating the area under the curve f(x) = 4 / (1 + x*x) * between 0 and 1. * * sequential version. */ package csci3366.sample.numint; import csci3366.sample.utility.Utility; public class NumIntSeq { /* * global variables: * global so in the parallel version all threads will have easy access */ private static long numSteps; private static double step; private static double sum = 0.0; /* main method */ public static void main(String[] args) { /* start timing */ long startTime = System.currentTimeMillis(); /* process command-line arguments */ String usageMessage = "arguments: number_of_steps"; numSteps = Utility.getLongArg(args, 0, 1, "number_of_steps", usageMessage); /* do computation */ for (int i=0; i < numSteps; ++i) { double x = (i+0.5)*step; sum += 4.0/(1.0+x*x); } double pi = sum * step; /* end timing and print result */ long endTime = System.currentTimeMillis(); System.out.printf( "sequential program results with %d steps\n", numSteps); System.out.printf("computed pi = %17.15f\n" , pi); System.out.printf( "difference between estimated pi and Math.PI = %17.15f\n", Math.abs(pi - Math.PI)); System.out.printf("time to compute = %g seconds\n", (double) (endTime - startTime) / 1000); } }