Skip to content Skip to sidebar Skip to footer

C, Perl, And Python Similar Loops Different Results

I wrote scripts to calculate pi in python, perl and c. They all use the same algorithm (trapezoidal reimann sum of a circle with n subintervals) and the python and perl programs a

Solution 1:

Well, comparing your methods, it became obvious your final operation for calculating pi was incorrect.

Replace pi = (val + x) * (four/(long double)n); with these two lines:

val = val * (longdouble)2.0;
pi = (val + x) * ((longdouble)2.0/(longdouble)n);

Compiling and running gives:

3.14159265241

Which I believe is the output you're looking for.

Post a Comment for "C, Perl, And Python Similar Loops Different Results"