Merge branch 'master' of git.ucc.asn.au:/ipdf/code
[ipdf/code.git] / src / tests / handbook1-1.cpp
1 /**
2  * From Handbook of Floating-Point Arithmetic
3  * Program 1.1 "A sequence that seems to converge to a wrong limit"
4  * Modified to work with a template type and then print the results for our favourite types
5  * 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)
6  */
7
8 #include <cstdio>
9 #include "real.h"
10
11 using namespace std;
12 using namespace IPDF;
13
14 template <class T> T s(T u, T v, int max)
15 {
16         T w = 0.;
17         for (int i = 3; i <= max; i++)
18         {
19                 w = T(111.) - T(1130.)/v + T(3000.)/(v*u);
20                 u = v;
21                 v = w;
22         }
23         return w;
24 }
25
26 int main(void)
27 {
28         double u0 = 2;
29         double u1 = -4;
30         printf("#n\tfloat\tdouble\tlong\tReal\n");
31         for (int i = 3; i <= 30; ++i)
32         {
33                 printf("%d\t%.15f\t%.15lf\t%.15llf\t%.15lf\n", i,
34                         s<float>(u0,u1,i), s<double>(u0,u1,i), s<long double>(u0,u1,i), Float(s<Real>(u0,u1,i)));
35         }
36
37         return 0;
38 }

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