Tester based on Handbook of Floating-Point Arithmetic 1.1
authorSam Moore <[email protected]>
Tue, 22 Apr 2014 10:34:43 +0000 (18:34 +0800)
committerSam Moore <[email protected]>
Tue, 22 Apr 2014 10:34:43 +0000 (18:34 +0800)
See also ipdf/documents

src/real.h
src/tests/handbook1-1.cpp [new file with mode: 0644]

index 4a81227..f052696 100644 (file)
@@ -22,13 +22,10 @@ namespace IPDF
 
 #if REAL == REAL_SINGLE
        typedef float Real;
 
 #if REAL == REAL_SINGLE
        typedef float Real;
-       inline float Float(Real r) {return r;}
 #elif REAL == REAL_DOUBLE
        typedef double Real;
 #elif REAL == REAL_DOUBLE
        typedef double Real;
-       inline double Float(Real r) {return r;}
 #elif REAL == REAL_LONG_DOUBLE
        typedef long double Real;
 #elif REAL == REAL_LONG_DOUBLE
        typedef long double Real;
-       inline long double Float(Real r) {return r;}
 #elif REAL == REAL_SINGLE_FAST2SUM
        typedef RealF2S<float> Real;
        inline float Float(Real r) {return r.m_value;}
 #elif REAL == REAL_SINGLE_FAST2SUM
        typedef RealF2S<float> Real;
        inline float Float(Real r) {return r.m_value;}
@@ -37,6 +34,12 @@ namespace IPDF
        #error "Type of Real unspecified."
 #endif //REAL
 
        #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
 }
 
 #endif //_REAL_H
diff --git a/src/tests/handbook1-1.cpp b/src/tests/handbook1-1.cpp
new file mode 100644 (file)
index 0000000..a93e789
--- /dev/null
@@ -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 <cstdio>
+#include "real.h"
+
+using namespace std;
+using namespace IPDF;
+
+template <class T> 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<float>(u0,u1,i), s<double>(u0,u1,i), s<long double>(u0,u1,i), Float(s<Real>(u0,u1,i)));
+       }
+
+       return 0;
+}

UCC git Repository :: git.ucc.asn.au