2 * Test mathematical operations on the Real type and consistency with double
11 #define TEST_CASES 1000
13 static double g_totalerror = 0;
15 bool NotEqual(double a, double b, double threshold=1e-1)
17 double error = fabs(a-b);
18 g_totalerror += error;
19 return (error > threshold);
22 int main(int argc, char ** argv)
26 unsigned failures = 0;
27 for (unsigned i = 0; i < TEST_CASES; ++i)
29 //Debug("Test %u of %u", i, TEST_CASES);
30 double da = (double)(rand() + 1) / (double)(rand() + 1);
31 double db = (double)(rand() + 1) / (double)(rand() + 1);
42 unsigned old_failures = failures;
43 if (NotEqual(Double(a), Double(aa)))
46 Warn("a != Real(a); %f vs %f", Double(a), Double(aa));
48 if (NotEqual(Double(b), Double(bb)))
51 Warn("b != Real(b); %f vs %f", Double(b), Double(bb));
54 if (NotEqual(Double(a), da))
57 Warn("a != da; %f vs %f", Double(a), da);
59 if (NotEqual(Double(b), db))
62 Warn("b != db; %f vs %f", Double(b), db);
64 if (NotEqual(Double(a+b), da+db))
67 Warn("a + b = %f should be %f", Double(a+b), da+db);
69 if (NotEqual(Double(a-b), da-db))
72 Warn("a - b = %f should be %f", Double(a-b), da-db);
74 if (NotEqual(Double(a*b), da*db))
77 Warn("a * b = %f should be %f", Double(a*b), da*db);
79 if (NotEqual(Double(a/b), da/db))
82 Warn("a / b = %f should be %f", Double(a/b), da/db);
85 if (NotEqual(Double(a), da))
88 Warn("a has changed after +-*/ from %f to %f", da, Double(a));
90 if (NotEqual(Double(b), db))
93 Warn("b has changed after +-*/ from %f to %f", db, Double(b));
96 if (NotEqual(Double(a+=b), da+=db))
99 Warn("a += b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
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 (failures > old_failures)
122 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));
123 #if REAL == REAL_RATIONAL || REAL == REAL_RATIONAL_ARBINT
124 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());
128 Debug("Completed %u test cases with total of %u operations, %u failures", TEST_CASES, 12*TEST_CASES, failures);
129 Debug("Total accumulated difference between Real and Double operations was %f", g_totalerror);