X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Ftests%2Fcalculatepi.cpp;h=ea9ea8f8f654b7478791de5ca6410c7520b73608;hp=46348ffb8ace1195f54865afe2ce8a3613e78ab5;hb=0ce8df17fae3ec986bd9570e5552aed92c080088;hpb=fdc6f69d40cf7e872218ec48493534cc3b85c852 diff --git a/src/tests/calculatepi.cpp b/src/tests/calculatepi.cpp index 46348ff..ea9ea8f 100644 --- a/src/tests/calculatepi.cpp +++ b/src/tests/calculatepi.cpp @@ -4,7 +4,7 @@ * Compares results obtained with float, double, and Real. */ #include "main.h" -#include // for PI +#include #include // for performance measurements /** Function to integrate @@ -19,7 +19,7 @@ template T f(const T & x) /** * Integrate f using the simpson rule */ -template T Integrate(T(*f)(const T&), const T & xmin, const T & xmax, uint64_t intervals) +template T Integrate(T(*f)(const T & ), const T & xmin, const T & xmax, uint64_t intervals) { T sum = 0.0; T dx = (xmax - xmin) / T(intervals); @@ -31,33 +31,47 @@ template T Integrate(T(*f)(const T&), const T & xmin, const T & xmax, odd += f(xmin + dx*T(i)); for (i = 2; i < intervals-1; i+=2) even += f(xmin + dx*T(i)); - sum = (f(xmin) + T(4.0)*odd + T(2.0)*even + f(xmin+T(intervals)*dx))*dx / T(3.0); return sum; } -#define MAX_INTERVALS 1e9 - +#define MAX_INTERVALS 1e10 /** * main */ int main(int argc, char ** argv) { - printf("# intervals\terror_float\tclock_float\terror_double\tclock_double\terror_real\tclock_real\n"); - for (uint64_t intervals = 1; intervals < (uint64_t)(MAX_INTERVALS); intervals*=10) + setbuf(stdout, NULL); + setbuf(stderr, NULL); + long double PI = acosl(-1.0L); + printf("# intervals\terror_float\tclock_float\terror_double\tclock_double\terror_long\tclock_long"); + #if REAL > REAL_LONG_DOUBLE + printf("\terror_real\tclock_real\n"); + #else + printf("\n"); + #endif //REAL + for (uint64_t intervals = 1; intervals < (uint64_t)(MAX_INTERVALS); intervals*=5) { clock_t start = clock(); - float error_float = Integrate(f, 0.0, 1.0, intervals) - (float)(M_PI); + float error_float = Integrate(f, 0.0f, 1.0f, intervals) - float(PI); clock_t clock_float = clock() - start; start = clock(); - double error_double = Integrate(f, 0.0, 1.0, intervals) - (double)(M_PI); + double error_double = Integrate(f, 0.0, 1.0, intervals) - double(PI); clock_t clock_double = clock() - start; start = clock(); - Real error_real = Integrate(f, 0.0, 1.0, intervals) - Real(M_PI); - clock_t clock_real = clock() - start; - - printf("%lu\t%.30f\t%li\t%.30f\t%li\t%.30f\t%li\n", intervals, error_float, clock_float, error_double, clock_double, Float(error_real), clock_real); + long double error_long = Integrate(f,0.0L,1.0L, intervals) - PI; + clock_t clock_long = clock() - start; + + printf("%lu\t%.30f\t%li\t%.30lf\t%li\t%.30llf\t%li", intervals, error_float, clock_float, error_double, clock_double, error_long, clock_long); + + #if REAL > REAL_LONG_DOUBLE + Real error_real = Integrate(f,Real(0.0L), Real(1.0L), intervals) - Real(PI); + clock_t clock_real = clock() - start; + printf("\t%.30lf\t%li\n", Float(error_real), clock_real); + #else + printf("\n"); + #endif } }