9c5a88a3edaa396040188d5131e6ca5121598221
[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         
21         ParanoidNumber a("0.3");
22         Debug("start at {%s} = %lf", a.Str().c_str(), a.ToDouble());
23         cout << "0.3 ";
24         float fa = 0.3;
25         double da = 0.3;
26         while (cin.good())
27         {
28                 char op;
29                 cin >> op;
30                 string token("");
31                 for (char c = cin.peek(); cin.good() && !iswspace(c); c = cin.peek())
32                 {
33                         if (c == '+' || c == '-' || c == '*' || c == '/')
34                         {
35                                 break;
36                         }
37                         token += c;
38                         c = cin.get();
39                 }
40                 Debug("String is %s", token.c_str());
41                 float fb = strtof(token.c_str(), NULL);
42                 double db = strtod(token.c_str(), NULL);
43                 ParanoidNumber b(token.c_str());
44                 Debug("b is {%s} %lf", b.Str().c_str(), b.ToDouble());
45                 switch (op)
46                 {
47                         case '+':
48                                 a += b;
49                                 fa += fb;
50                                 da += db;
51                                 break;
52                         case '-':
53                                 a -= b;
54                                 fa -= fb;
55                                 da -= db;
56                                 break;
57                         case '*':
58                                 a *= b;
59                                 fa *= fb;
60                                 da *= db;
61                                 break;
62                         case '/':
63                                 a /= b;
64                                 fa /= fb;
65                                 da /= db;
66                                 break;
67                 }
68                         
69                 Debug("a is: %s", a.Str().c_str());
70                 Debug("a as double: %.40f\n", a.ToDouble());
71                 Debug("floats give: %.40f\n", fa);
72                 Debug("double gives: %.40f\n", da);
73                 
74                 
75         }
76 }

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