Break maths some more
[ipdf/code.git] / src / tests / paranoidcalculator.cpp
1 #include "main.h"
2 #include "real.h"
3 #include <cmath>
4 #include <cassert>
5 #include <list>
6 #include <bitset>
7 #include <iostream>
8 #include <cfloat>
9 #include <fenv.h>
10 #include "paranoidnumber.h"
11
12 using namespace std;
13 using namespace IPDF;
14
15 int main(int argc, char ** argv)
16 {
17         Debug("FLT_MAX = %.40f", FLT_MAX);
18         Debug("FLT_MIN = %.40f", FLT_MIN);
19         Debug("FLT_EPSILON = %.40f", FLT_EPSILON);
20         Debug("Sizeof ParanoidNumber::digit_t is %u", sizeof(ParanoidNumber::digit_t));
21         Debug("Sizeof ParanoidNumber is %u", sizeof(ParanoidNumber));
22         Debug("Sizeof double is %u", sizeof(double));
23         Debug("Sizeof ParanoidNumber* %u", sizeof(ParanoidNumber*));
24
25         string token("");
26         cin >> token;   
27         ParanoidNumber a(token.c_str());
28         double da = a.ToDouble();
29         float fa = da;
30         while (cin.good())
31         {
32                 char op;
33                 cin >> op;
34                 token = "";
35                 for (char c = cin.peek(); cin.good() && !iswspace(c); c = cin.peek())
36                 {
37                         if (c == '+' || c == '-' || c == '*' || c == '/')
38                         {
39                                 break;
40                         }
41                         token += c;
42                         c = cin.get();
43                 }
44                 Debug("String is %s", token.c_str());
45                 float fb = strtof(token.c_str(), NULL);
46                 double db = strtod(token.c_str(), NULL);
47                 ParanoidNumber b(token.c_str());
48                 Debug("b is {%s} %lf", b.Str().c_str(), b.ToDouble());
49                 switch (op)
50                 {
51                         case '+':
52                                 a += b;
53                                 fa += fb;
54                                 da += db;
55                                 break;
56                         case '-':
57                                 a -= b;
58                                 fa -= fb;
59                                 da -= db;
60                                 break;
61                         case '*':
62                                 a *= b;
63                                 fa *= fb;
64                                 da *= db;
65                                 break;
66                         case '/':
67                                 a /= b;
68                                 fa /= fb;
69                                 da /= db;
70                                 break;
71                 }
72                         
73                 Debug("a is: %s", a.Str().c_str());
74                 Debug("a as double: %.40f\n", a.ToDouble());
75                 Debug("a as float: %.40f\n", a.ToFloat());
76                 Debug("a as int64_t: %ld\n", a.Convert<int64_t>());
77                 Debug("floats give: %.40f\n", fa);
78                 Debug("double gives: %.40f\n", da);
79                 
80                 
81         }
82 }

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