10 #include "paranoidnumber.h"
11 #include "progressbar.h"
16 string RandomNumberAsString(int max_digits = 12)
19 int digits = 1+(rand() % max_digits);
20 int dp = (rand() % digits)+1;
21 for (int i = 0; i < digits; ++i)
28 result += ('0'+rand() % 10);
33 bool CloseEnough(double d, ParanoidNumber & p)
35 double pd = p.ToDouble();
38 return fabs(pd) <= 1e-6;
39 return fabs((fabs(pd - d) / d)) <= 1e-6;
42 #define TEST_CASES 1000
44 int main(int argc, char ** argv)
47 string number(RandomNumberAsString());
48 ParanoidNumber a(number);
49 float fa = strtof(number.c_str(), NULL);
50 double da = strtod(number.c_str(), NULL);
52 long double lda = strtold(number.c_str(), NULL);
54 if (fabs(a.ToDouble() - da) > 1e-6)
56 Error("double %lf, pn %lf {%s}", da, a.ToDouble(), a.Str().c_str());
57 Fatal("Didn't construct correctly off %s", number.c_str());
60 char opch[] = {'+','-','*','/'};
61 int opcount[] = {0,0,0,0};
62 for (int i = 0; i < TEST_CASES; ++i)
64 ProgressBar(i, TEST_CASES); fprintf(stderr, "%.30lf (%d)+ (%d)- (%d)* (%d)/ [Paranoia %ld]",diff, opcount[0],opcount[1],opcount[2],opcount[3], ParanoidNumber::Paranoia());
65 number = RandomNumberAsString();
66 ParanoidNumber b(number);
67 float fb = strtof(number.c_str(), NULL);
68 double db = strtod(number.c_str(), NULL);
74 long double ldb = strtold(number.c_str(), NULL);
75 int op = rand() % 4;//= 2*(rand() % 2);
78 //if (rand() % 2 == 0)
79 //op = 2+(rand() % 2);
82 ParanoidNumber olda(a);
111 diff = 100.0*(fabs(a.ToDouble() - da) / da);
112 if (!CloseEnough(da, a))
114 Error("Op %i: ParanoidNumber probably doesn't work", i);
115 Error("Operation: %lf %c %lf", oldda, opch[op], db);
116 Error("As PN: %lf %c %lf", olda.ToDouble(), opch[op], b.ToDouble());
117 Error("PN String: %s", a.Str().c_str());
118 Error("Diff is %.40lf", diff);
119 Fatal("%.40lf, expected aboout %.40lf", a.ToDouble(), da);
125 printf("ParanoidNumber: {%s} = %.40lf\n", a.Str().c_str(), a.ToDouble());
126 printf("float: %.40f\n", fa);
127 printf("double: %.40lf\n", da);
128 printf("long double: %.40Lf\n", lda);
129 printf("diff %.40lf\n", diff);