1 // check/numeric_consist.cpp
3 /* This test routine creates some numbers and check the result of several
4 * boolean tests on these numbers like is_integer() etc... */
7 #include <ginac/ginac.h>
9 /* Simple and maybe somewhat pointless consistency tests of assorted tests and
11 static unsigned numeric_consist1(void)
14 numeric test_int1(42);
16 numeric test_rat1 = test_int1; test_rat1 /= test_int2;
17 test_rat1 = -test_rat1; // -42/5
21 if ( !test_int1.is_integer() ) {
23 << " erroneously not recognized as integer" << endl;
26 if ( !test_int1.is_rational() ) {
28 << " erroneously not recognized as rational" << endl;
32 if ( !test_rat1.is_rational() ) {
34 << " erroneously not recognized as rational" << endl;
37 if ( test_rat1.is_integer() ) {
39 << " erroneously recognized as integer" << endl;
43 int i = numeric(1984).to_int();
45 clog << "conversion of " << i
46 << " from numeric to int failed" << endl;
51 if ( !e1.info(info_flags::posint) ) {
52 clog << "expression " << e1
53 << " erroneously not recognized as positive integer" << endl;
58 if ( ex_to_numeric(e2).is_integer() ) {
59 clog << "expression " << e2
60 << " erroneously recognized as integer" << endl;
64 // The next two were two actual bugs in CLN till June, 12, 1999:
65 test_rat1 = numeric(3)/numeric(2);
66 test_rat1 += test_rat1;
67 if ( !test_rat1.is_integer() ) {
68 clog << "3/2 + 3/2 erroneously not integer 3 but instead "
72 test_rat1 = numeric(3)/numeric(2);
73 numeric test_rat2 = test_rat1 + numeric(1); // 5/2
74 test_rat2 -= test_rat1; // 1
75 if ( !test_rat2.is_integer() ) {
76 clog << "5/2 - 3/2 erroneously not integer 1 but instead "
81 // Check some numerator and denominator calculations:
82 for (int i=0; i<10; ++i) {
84 do { re_q = rand(); } while (re_q == 0);
85 do { im_q = rand(); } while (im_q == 0);
86 numeric r(rand()-RAND_MAX/2, re_q);
87 numeric i(rand()-RAND_MAX/2, im_q);
93 clog << z << " erroneously transformed into "
94 << p << "/" << q << " by numer() and denom()" << endl;
101 /* We had some fun with a bug in CLN that caused it to loop forever when
102 * calculating expt(a,b) if b is a rational and a a nonnegative integer.
103 * Implementing a workaround sadly introduced another bug on May 28th 1999
104 * that was fixed on May 31st. The workaround turned out to be stupid and
105 * the bug was finally killed in CLN on September 2nd. */
106 static unsigned numeric_consist2(void)
110 ex zero = numeric(0);
112 ex three = numeric(3);
114 // The hang in this code was the reason for the original workaround
115 if ( pow(two,two/three) == 42 ) {
116 clog << "pow(2,2/3) erroneously returned 42" << endl;
117 ++result; // cannot happen
120 // Actually, this used to raise a FPE after introducing the workaround
121 if ( two*zero != zero ) {
122 clog << "2*0 erroneously returned " << two*zero << endl;
126 // And this returned a cl_F due to the implicit call of numeric::power()
128 if ( !six.info(info_flags::integer) ) {
129 clog << "2*3 erroneously returned the non-integer " << six << endl;
133 // The fix in the workaround left a whole which was fixed hours later...
134 ex another_zero = pow(zero,numeric(1)/numeric(2));
135 if ( another_zero.compare(exZERO()) ) {
136 clog << "pow(0,1/2) erroneously returned" << another_zero << endl;
143 /* Assorted tests to ensure some crucial functions behave exactly as specified
144 * in the documentation. */
145 static unsigned numeric_consist3(void)
148 numeric calc_rem, calc_quo;
151 // check if irem(a, b) and irem(a, b, q) really behave like Maple's
152 // irem(a, b) and irem(a, b, 'q') as advertised in our documentation.
153 // These overloaded routines indeed need to be checked separately since
154 // internally they might be doing something completely different:
155 a = 23; b = 4; calc_rem = irem(a, b);
156 if ( calc_rem != 3 ) {
157 clog << "irem(" << a << "," << b << ") erroneously returned "
161 a = 23; b = -4; calc_rem = irem(a, b);
162 if ( calc_rem != 3 ) {
163 clog << "irem(" << a << "," << b << ") erroneously returned "
167 a = -23; b = 4; calc_rem = irem(a, b);
168 if ( calc_rem != -3 ) {
169 clog << "irem(" << a << "," << b << ") erroneously returned "
173 a = -23; b = -4; calc_rem = irem(a, b);
174 if ( calc_rem != -3 ) {
175 clog << "irem(" << a << "," << b << ") erroneously returned "
179 // and now the overloaded irem(a,b,q):
180 a = 23; b = 4; calc_rem = irem(a, b, calc_quo);
181 if ( calc_rem != 3 || calc_quo != 5 ) {
182 clog << "irem(" << a << "," << b << ",q) erroneously returned "
183 << calc_rem << " with q=" << calc_quo << endl;
186 a = 23; b = -4; calc_rem = irem(a, b, calc_quo);
187 if ( calc_rem != 3 || calc_quo != -5 ) {
188 clog << "irem(" << a << "," << b << ",q) erroneously returned "
189 << calc_rem << " with q=" << calc_quo << endl;
192 a = -23; b = 4; calc_rem = irem(a, b, calc_quo);
193 if ( calc_rem != -3 || calc_quo != -5 ) {
194 clog << "irem(" << a << "," << b << ",q) erroneously returned "
195 << calc_rem << " with q=" << calc_quo << endl;
198 a = -23; b = -4; calc_rem = irem(a, b, calc_quo);
199 if ( calc_rem != -3 || calc_quo != 5 ) {
200 clog << "irem(" << a << "," << b << ",q) erroneously returned "
201 << calc_rem << " with q=" << calc_quo << endl;
204 // check if iquo(a, b) and iquo(a, b, r) really behave like Maple's
205 // iquo(a, b) and iquo(a, b, 'r') as advertised in our documentation.
206 // These overloaded routines indeed need to be checked separately since
207 // internally they might be doing something completely different:
208 a = 23; b = 4; calc_quo = iquo(a, b);
209 if ( calc_quo != 5 ) {
210 clog << "iquo(" << a << "," << b << ") erroneously returned "
214 a = 23; b = -4; calc_quo = iquo(a, b);
215 if ( calc_quo != -5 ) {
216 clog << "iquo(" << a << "," << b << ") erroneously returned "
220 a = -23; b = 4; calc_quo = iquo(a, b);
221 if ( calc_quo != -5 ) {
222 clog << "iquo(" << a << "," << b << ") erroneously returned "
226 a = -23; b = -4; calc_quo = iquo(a, b);
227 if ( calc_quo != 5 ) {
228 clog << "iquo(" << a << "," << b << ") erroneously returned "
232 // and now the overloaded iquo(a,b,r):
233 a = 23; b = 4; calc_quo = iquo(a, b, calc_rem);
234 if ( calc_quo != 5 || calc_rem != 3 ) {
235 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
236 << calc_quo << " with r=" << calc_rem << endl;
239 a = 23; b = -4; calc_quo = iquo(a, b, calc_rem);
240 if ( calc_quo != -5 || calc_rem != 3 ) {
241 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
242 << calc_quo << " with r=" << calc_rem << endl;
245 a = -23; b = 4; calc_quo = iquo(a, b, calc_rem);
246 if ( calc_quo != -5 || calc_rem != -3 ) {
247 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
248 << calc_quo << " with r=" << calc_rem << endl;
251 a = -23; b = -4; calc_quo = iquo(a, b, calc_rem);
252 if ( calc_quo != 5 || calc_rem != -3 ) {
253 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
254 << calc_quo << " with r=" << calc_rem << endl;
261 /* Now we perform some less trivial checks about several functions which should
262 * return exact numbers if possible. */
263 static unsigned numeric_consist4(void)
268 // square roots of squares of integers:
270 for (int i=0; i<42; ++i) {
271 if ( !sqrt(numeric(i*i)).is_integer() ) {
276 clog << "One or more square roots of squares of integers did not return exact integers" << endl;
279 // square roots of squares of rationals:
281 for (int num=0; num<41; ++num) {
282 for (int den=1; den<42; ++den) {
283 if ( !sqrt(numeric(num*num)/numeric(den*den)).is_rational() ) {
289 clog << "One or more square roots of squares of rationals did not return exact integers" << endl;
296 unsigned numeric_consist(void)
300 cout << "checking consistency of numeric types..." << flush;
301 clog << "---------consistency of numeric types:" << endl;
303 result += numeric_consist1();
304 result += numeric_consist2();
305 result += numeric_consist3();
306 result += numeric_consist4();
310 clog << "(no output)" << endl;