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-4)
17 double error = fabs(a-b);
18 g_totalerror += error;
19 return (error > threshold);
22 int main(int argc, char ** argv)
27 unsigned failures = 0;
28 for (unsigned i = 0; i < TEST_CASES; ++i)
30 //Debug("Test %u of %u", i, TEST_CASES);
31 double da = (double)(rand() + 1) / (double)(rand() + 1);
32 double db = (double)(rand() + 1) / (double)(rand() + 1);
43 unsigned old_failures = failures;
44 if (NotEqual(Double(a), Double(aa)))
47 Warn("a != Real(a); %f vs %f", Double(a), Double(aa));
49 if (NotEqual(Double(b), Double(bb)))
52 Warn("b != Real(b); %f vs %f", Double(b), Double(bb));
55 if (NotEqual(Double(a), da))
58 Warn("a != da; %f vs %f", Double(a), da);
60 if (NotEqual(Double(b), db))
63 Warn("b != db; %f vs %f", Double(b), db);
65 if (NotEqual(Double(a+b), da+db))
68 Warn("a + b = %f should be %f", Double(a+b), da+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);
86 if (NotEqual(Double(a), da))
89 Warn("a has changed after +-*/ from %f to %f", da, Double(a));
91 if (NotEqual(Double(b), db))
94 Warn("b has changed after +-*/ from %f to %f", db, Double(b));
97 if (NotEqual(Double(a+=b), da+=db))
100 Warn("a += b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
103 if (NotEqual(Double(a-=b), da-=db))
106 Warn("a -= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
109 if (NotEqual(Double(a*=b), da*=db))
112 Warn("a *= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
115 if (NotEqual(Double(a/=b), da/=db))
118 Warn("a /= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
120 if (NotEqual(Double(a*0.0 + 1.0), da*0.0 + 1.0))
123 Warn("a * 0 = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
126 if (NotEqual(Double(a=b), da=db))
129 Warn("a = b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
132 if (failures > old_failures)
134 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));
135 #if REAL == REAL_RATIONAL || REAL == REAL_RATIONAL_ARBINT
136 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());
140 Debug("Completed %u test cases with total of %u operations, %u failures", TEST_CASES, 12*TEST_CASES, failures);
141 Debug("Total accumulated difference between Real and Double operations was %f", g_totalerror);