10 #include "paranoidnumber.h"
15 string RandomNumberAsString(int digits = 6)
18 int dp = 1+(rand() % 3);
19 for (int i = 0; i < digits; ++i)
26 result += ('0'+rand() % 10);
31 #define TEST_CASES 10000
33 int main(int argc, char ** argv)
36 string number(RandomNumberAsString());
37 ParanoidNumber a(number);
38 float fa = strtof(number.c_str(), NULL);
39 double da = strtod(number.c_str(), NULL);
40 long double lda = strtold(number.c_str(), NULL);
42 if (fabs(a.ToDouble() - da) > 1e-6)
44 Error("double %lf, pn %lf {%s}", da, a.ToDouble(), a.Str().c_str());
45 Fatal("Didn't construct correctly off %s", number.c_str());
48 char opch[] = {'+','-','*','/'};
50 for (int i = 0; i < TEST_CASES; ++i)
52 number = RandomNumberAsString();
53 ParanoidNumber b(number);
54 float fb = strtof(number.c_str(), NULL);
55 double db = strtod(number.c_str(), NULL);
56 long double ldb = strtold(number.c_str(), NULL);
57 int op = (rand() % 4);
58 ParanoidNumber olda(a);
87 if (fabs(a.ToDouble() - da) > 1.0 )
89 Error("Op %i: ParanoidNumber probably doesn't work", i);
90 Error("Operation: %lf %c %lf", oldda, opch[op], db);
91 Error("As PN: %lf %c %lf", olda.ToDouble(), opch[op], b.ToDouble());
92 Fatal("%lf, expected aboout %lf", a.ToDouble(), da);
95 printf("ParanoidNumber: {%s} = %.40lf\n", a.Str().c_str(), a.ToDouble());
96 printf("float: %.40f\n", fa);
97 printf("double: %.40lf\n", da);
98 printf("long double: %.40Lf\n", lda);