1 /** @file 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...
6 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <ginac/ginac.h>
26 /* Simple and maybe somewhat pointless consistency tests of assorted tests and
28 static unsigned numeric_consist1(void)
31 numeric test_int1(42);
33 numeric test_rat1 = test_int1; test_rat1 /= test_int2;
34 test_rat1 = -test_rat1; // -42/5
38 if ( !test_int1.is_integer() ) {
40 << " erroneously not recognized as integer" << endl;
43 if ( !test_int1.is_rational() ) {
45 << " erroneously not recognized as rational" << endl;
49 if ( !test_rat1.is_rational() ) {
51 << " erroneously not recognized as rational" << endl;
54 if ( test_rat1.is_integer() ) {
56 << " erroneously recognized as integer" << endl;
60 int i = numeric(1984).to_int();
62 clog << "conversion of " << i
63 << " from numeric to int failed" << endl;
68 if ( !e1.info(info_flags::posint) ) {
69 clog << "expression " << e1
70 << " erroneously not recognized as positive integer" << endl;
75 if ( ex_to_numeric(e2).is_integer() ) {
76 clog << "expression " << e2
77 << " erroneously recognized as integer" << endl;
81 // The next two were two actual bugs in CLN till June, 12, 1999:
82 test_rat1 = numeric(3)/numeric(2);
83 test_rat1 += test_rat1;
84 if ( !test_rat1.is_integer() ) {
85 clog << "3/2 + 3/2 erroneously not integer 3 but instead "
89 test_rat1 = numeric(3)/numeric(2);
90 numeric test_rat2 = test_rat1 + numeric(1); // 5/2
91 test_rat2 -= test_rat1; // 1
92 if ( !test_rat2.is_integer() ) {
93 clog << "5/2 - 3/2 erroneously not integer 1 but instead "
98 // Check some numerator and denominator calculations:
99 for (int i=0; i<10; ++i) {
101 do { re_q = rand(); } while (re_q == 0);
102 do { im_q = rand(); } while (im_q == 0);
103 numeric r(rand()-RAND_MAX/2, re_q);
104 numeric i(rand()-RAND_MAX/2, im_q);
106 numeric p = numer(z);
107 numeric q = denom(z);
110 clog << z << " erroneously transformed into "
111 << p << "/" << q << " by numer() and denom()" << endl;
118 /* We had some fun with a bug in CLN that caused it to loop forever when
119 * calculating expt(a,b) if b is a rational and a a nonnegative integer.
120 * Implementing a workaround sadly introduced another bug on May 28th 1999
121 * that was fixed on May 31st. The workaround turned out to be stupid and
122 * the bug was finally killed in CLN on September 2nd. */
123 static unsigned numeric_consist2(void)
127 ex zero = numeric(0);
129 ex three = numeric(3);
131 // The hang in this code was the reason for the original workaround
132 if ( pow(two,two/three) == 42 ) {
133 clog << "pow(2,2/3) erroneously returned 42" << endl;
134 ++result; // cannot happen
137 // Actually, this used to raise a FPE after introducing the workaround
138 if ( two*zero != zero ) {
139 clog << "2*0 erroneously returned " << two*zero << endl;
143 // And this returned a cl_F due to the implicit call of numeric::power()
145 if ( !six.info(info_flags::integer) ) {
146 clog << "2*3 erroneously returned the non-integer " << six << endl;
150 // The fix in the workaround left a whole which was fixed hours later...
151 ex another_zero = pow(zero,numeric(1)/numeric(2));
152 if ( another_zero.compare(exZERO()) ) {
153 clog << "pow(0,1/2) erroneously returned" << another_zero << endl;
160 /* Assorted tests to ensure some crucial functions behave exactly as specified
161 * in the documentation. */
162 static unsigned numeric_consist3(void)
165 numeric calc_rem, calc_quo;
168 // check if irem(a, b) and irem(a, b, q) really behave like Maple's
169 // irem(a, b) and irem(a, b, 'q') as advertised in our documentation.
170 // These overloaded routines indeed need to be checked separately since
171 // internally they might be doing something completely different:
172 a = 23; b = 4; calc_rem = irem(a, b);
173 if ( calc_rem != 3 ) {
174 clog << "irem(" << a << "," << b << ") erroneously returned "
178 a = 23; b = -4; calc_rem = irem(a, b);
179 if ( calc_rem != 3 ) {
180 clog << "irem(" << a << "," << b << ") erroneously returned "
184 a = -23; b = 4; calc_rem = irem(a, b);
185 if ( calc_rem != -3 ) {
186 clog << "irem(" << a << "," << b << ") erroneously returned "
190 a = -23; b = -4; calc_rem = irem(a, b);
191 if ( calc_rem != -3 ) {
192 clog << "irem(" << a << "," << b << ") erroneously returned "
196 // and now the overloaded irem(a,b,q):
197 a = 23; b = 4; calc_rem = irem(a, b, calc_quo);
198 if ( calc_rem != 3 || calc_quo != 5 ) {
199 clog << "irem(" << a << "," << b << ",q) erroneously returned "
200 << calc_rem << " with q=" << calc_quo << endl;
203 a = 23; b = -4; calc_rem = irem(a, b, calc_quo);
204 if ( calc_rem != 3 || calc_quo != -5 ) {
205 clog << "irem(" << a << "," << b << ",q) erroneously returned "
206 << calc_rem << " with q=" << calc_quo << endl;
209 a = -23; b = 4; calc_rem = irem(a, b, calc_quo);
210 if ( calc_rem != -3 || calc_quo != -5 ) {
211 clog << "irem(" << a << "," << b << ",q) erroneously returned "
212 << calc_rem << " with q=" << calc_quo << endl;
215 a = -23; b = -4; calc_rem = irem(a, b, calc_quo);
216 if ( calc_rem != -3 || calc_quo != 5 ) {
217 clog << "irem(" << a << "," << b << ",q) erroneously returned "
218 << calc_rem << " with q=" << calc_quo << endl;
221 // check if iquo(a, b) and iquo(a, b, r) really behave like Maple's
222 // iquo(a, b) and iquo(a, b, 'r') as advertised in our documentation.
223 // These overloaded routines indeed need to be checked separately since
224 // internally they might be doing something completely different:
225 a = 23; b = 4; calc_quo = iquo(a, b);
226 if ( calc_quo != 5 ) {
227 clog << "iquo(" << a << "," << b << ") erroneously returned "
231 a = 23; b = -4; calc_quo = iquo(a, b);
232 if ( calc_quo != -5 ) {
233 clog << "iquo(" << a << "," << b << ") erroneously returned "
237 a = -23; b = 4; calc_quo = iquo(a, b);
238 if ( calc_quo != -5 ) {
239 clog << "iquo(" << a << "," << b << ") erroneously returned "
243 a = -23; b = -4; calc_quo = iquo(a, b);
244 if ( calc_quo != 5 ) {
245 clog << "iquo(" << a << "," << b << ") erroneously returned "
249 // and now the overloaded iquo(a,b,r):
250 a = 23; b = 4; calc_quo = iquo(a, b, calc_rem);
251 if ( calc_quo != 5 || calc_rem != 3 ) {
252 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
253 << calc_quo << " with r=" << calc_rem << endl;
256 a = 23; b = -4; calc_quo = iquo(a, b, calc_rem);
257 if ( calc_quo != -5 || calc_rem != 3 ) {
258 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
259 << calc_quo << " with r=" << calc_rem << endl;
262 a = -23; b = 4; calc_quo = iquo(a, b, calc_rem);
263 if ( calc_quo != -5 || calc_rem != -3 ) {
264 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
265 << calc_quo << " with r=" << calc_rem << endl;
268 a = -23; b = -4; calc_quo = iquo(a, b, calc_rem);
269 if ( calc_quo != 5 || calc_rem != -3 ) {
270 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
271 << calc_quo << " with r=" << calc_rem << endl;
278 /* Now we perform some less trivial checks about several functions which should
279 * return exact numbers if possible. */
280 static unsigned numeric_consist4(void)
285 // square roots of squares of integers:
287 for (int i=0; i<42; ++i) {
288 if ( !sqrt(numeric(i*i)).is_integer() ) {
293 clog << "One or more square roots of squares of integers did not return exact integers" << endl;
296 // square roots of squares of rationals:
298 for (int num=0; num<41; ++num) {
299 for (int den=1; den<42; ++den) {
300 if ( !sqrt(numeric(num*num)/numeric(den*den)).is_rational() ) {
306 clog << "One or more square roots of squares of rationals did not return exact integers" << endl;
313 unsigned numeric_consist(void)
317 cout << "checking consistency of numeric types..." << flush;
318 clog << "---------consistency of numeric types:" << endl;
320 result += numeric_consist1();
321 result += numeric_consist2();
322 result += numeric_consist3();
323 result += numeric_consist4();
327 clog << "(no output)" << endl;