X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Ftests%2Fhandbook1-1.cpp;fp=src%2Ftests%2Fhandbook1-1.cpp;h=a93e7891405e29a472cff6f37c49cf3a4d455e82;hp=0000000000000000000000000000000000000000;hb=e24724d4c5a0c57be728b418a2d75d94fbe442a3;hpb=eff96d65bd1101083e04cb3ff2468b3feea3ff9e 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; +}