2 * Test mathematical operations on the Real type and consistency with double
7 #include "progressbar.h"
12 #define TEST_CASES 1000
14 static double g_totalerror = 0;
16 bool NotEqual(double a, double b, double threshold=1e-4)
18 double error = fabs(a-b);
19 g_totalerror += error;
20 return (error > threshold);
23 int main(int argc, char ** argv)
28 unsigned failures = 0;
30 double dacumulate = 0;
32 for (unsigned i = 0; i < TEST_CASES; ++i)
34 ProgressBar(i, TEST_CASES, 50);
35 //Debug("Test %u of %u", i, TEST_CASES);
36 double da = (double)(rand() + 1) / (double)(rand() + 1);
37 double db = (double)(rand() + 1) / (double)(rand() + 1);
48 unsigned old_failures = failures;
49 if (NotEqual(Double(a), Double(aa)))
52 Warn("a != Real(a); %f vs %f", Double(a), Double(aa));
54 if (NotEqual(Double(b), Double(bb)))
57 Warn("b != Real(b); %f vs %f", Double(b), Double(bb));
60 if (NotEqual(Double(a), da))
63 Warn("a != da; %f vs %f", Double(a), da);
65 if (NotEqual(Double(b), db))
68 Warn("b != db; %f vs %f", Double(b), db);
70 if (NotEqual(Double(a+b), da+db))
73 Warn("a + b = %f should be %f", Double(a+b), da+db);
75 if (NotEqual(Double(a-b), da-db))
78 Warn("a - b = %f should be %f", Double(a-b), da-db);
80 if (NotEqual(Double(a*b), da*db))
83 Warn("a * b = %f should be %f", Double(a*b), da*db);
85 if (NotEqual(Double(a/b), da/db))
88 Warn("a / b = %f should be %f", Double(a/b), da/db);
91 if (NotEqual(Double(a), da))
94 Warn("a has changed after +-*/ from %f to %f", da, Double(a));
96 if (NotEqual(Double(b), db))
99 Warn("b has changed after +-*/ from %f to %f", db, Double(b));
102 if (NotEqual(Double(a+=b), da+=db))
105 Warn("a += b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
108 if (NotEqual(Double(a-=b), da-=db))
111 Warn("a -= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
114 if (NotEqual(Double(a*=b), da*=db))
117 Warn("a *= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
120 if (NotEqual(Double(a/=b), da/=db))
123 Warn("a /= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
125 if (NotEqual(Double(a*0.0 + 1.0), da*0.0 + 1.0))
128 Warn("a * 0 = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
131 if (NotEqual(Double(a=b), da=db))
134 Warn("a = b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
137 if (NotEqual(Double(-a), -da))
140 Warn("-a = %f should be %f, a before op was %f", Double(-a), -da, Double(abeforeop));
143 if (NotEqual(Double(Sqrt(a)), Sqrt(da)))
146 Warn("Sqrt(a) = %f should be %f, a before op was %f", Double(Sqrt(a)), Sqrt(da), Double(abeforeop));
149 if (NotEqual(Double(a), da))
152 Warn("a = %f, should be %f, a before ops was %f", Double(a), da, Double(abeforeop));
174 if (NotEqual(Double(acumulate), dacumulate))
176 Warn("Accumulated result %.30lf vs %.30lf is wrong", Double(acumulate), dacumulate);
180 if (failures > old_failures)
182 Error("%u failures on case %u da = %f, db = %f, a = %f, b = %f, aa = %f, bb = %f", failures-old_failures, i, da, db, Double(a), Double(b), Double(aa), Double(bb));
183 #if REAL == REAL_RATIONAL || REAL == REAL_RATIONAL_ARBINT
184 Debug("\tStrings are a = %s, b = %s, aa = %s, bb = %s", a.Str().c_str(), b.Str().c_str(), aa.Str().c_str(), bb.Str().c_str());
188 Debug("Completed %u test cases with total of %u operations, %u failures", TEST_CASES, 18*TEST_CASES, failures);
189 Debug("Total accumulated difference between Real and Double operations was %f", g_totalerror);
190 Debug("Real: %.40lf", Double(acumulate));
191 Debug("Doub: %.40lf", dacumulate);
192 Debug("Diff: %.40lf", Double(acumulate) - dacumulate);