From: Sam Moore Date: Tue, 22 Apr 2014 10:34:43 +0000 (+0800) Subject: Tester based on Handbook of Floating-Point Arithmetic 1.1 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=e24724d4c5a0c57be728b418a2d75d94fbe442a3;p=ipdf%2Fcode.git Tester based on Handbook of Floating-Point Arithmetic 1.1 See also ipdf/documents --- diff --git a/src/real.h b/src/real.h index 4a81227..f052696 100644 --- a/src/real.h +++ b/src/real.h @@ -22,13 +22,10 @@ namespace IPDF #if REAL == REAL_SINGLE typedef float Real; - inline float Float(Real r) {return r;} #elif REAL == REAL_DOUBLE typedef double Real; - inline double Float(Real r) {return r;} #elif REAL == REAL_LONG_DOUBLE typedef long double Real; - inline long double Float(Real r) {return r;} #elif REAL == REAL_SINGLE_FAST2SUM typedef RealF2S Real; inline float Float(Real r) {return r.m_value;} @@ -37,6 +34,12 @@ namespace IPDF #error "Type of Real unspecified." #endif //REAL + // Allow us to call Float on the primative types + // Useful so I can template some things that could be either (a more complicated) Real or a primitive type + // Mostly in the testers. + inline float Float(float f) {return f;} + inline double Float(double f) {return f;} + inline long double Float(long double f) {return f;} } #endif //_REAL_H diff --git a/src/tests/handbook1-1.cpp b/src/tests/handbook1-1.cpp new file mode 100644 index 0000000..a93e789 --- /dev/null +++ b/src/tests/handbook1-1.cpp @@ -0,0 +1,38 @@ +/** + * From Handbook of Floating-Point Arithmetic + * Program 1.1 "A sequence that seems to converge to a wrong limit" + * Modified to work with a template type and then print the results for our favourite types + * I know it is O(N^2) when it should be O(N), but in terms of amount of typing it is O(much nicer this way for small values of N) + */ + +#include +#include "real.h" + +using namespace std; +using namespace IPDF; + +template T s(T u, T v, int max) +{ + T w = 0.; + for (int i = 3; i <= max; i++) + { + w = T(111.) - T(1130.)/v + T(3000.)/(v*u); + u = v; + v = w; + } + return w; +} + +int main(void) +{ + double u0 = 2; + double u1 = -4; + printf("#n\tfloat\tdouble\tlong\tReal\n"); + for (int i = 3; i <= 30; ++i) + { + printf("%d\t%.15f\t%.15lf\t%.15llf\t%.15lf\n", i, + s(u0,u1,i), s(u0,u1,i), s(u0,u1,i), Float(s(u0,u1,i))); + } + + return 0; +}