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... */
7 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <ginac/ginac.h>
27 #ifndef NO_GINAC_NAMESPACE
28 using namespace GiNaC;
29 #endif // ndef NO_GINAC_NAMESPACE
31 /* Simple and maybe somewhat pointless consistency tests of assorted tests and
33 static unsigned numeric_consist1(void)
36 numeric test_int1(42);
38 numeric test_rat1 = test_int1; test_rat1 /= test_int2;
39 test_rat1 = -test_rat1; // -42/5
43 if ( !test_int1.is_integer() ) {
45 << " erroneously not recognized as integer" << endl;
48 if ( !test_int1.is_rational() ) {
50 << " erroneously not recognized as rational" << endl;
54 if ( !test_rat1.is_rational() ) {
56 << " erroneously not recognized as rational" << endl;
59 if ( test_rat1.is_integer() ) {
61 << " erroneously recognized as integer" << endl;
65 int i = numeric(1984).to_int();
67 clog << "conversion of " << i
68 << " from numeric to int failed" << endl;
73 if ( !e1.info(info_flags::posint) ) {
74 clog << "expression " << e1
75 << " erroneously not recognized as positive integer" << endl;
80 if ( ex_to_numeric(e2).is_integer() ) {
81 clog << "expression " << e2
82 << " erroneously recognized as integer" << endl;
86 // The next two were two actual bugs in CLN till June, 12, 1999:
87 test_rat1 = numeric(3)/numeric(2);
88 test_rat1 += test_rat1;
89 if ( !test_rat1.is_integer() ) {
90 clog << "3/2 + 3/2 erroneously not integer 3 but instead "
94 test_rat1 = numeric(3)/numeric(2);
95 numeric test_rat2 = test_rat1 + numeric(1); // 5/2
96 test_rat2 -= test_rat1; // 1
97 if ( !test_rat2.is_integer() ) {
98 clog << "5/2 - 3/2 erroneously not integer 1 but instead "
103 // Check some numerator and denominator calculations:
104 for (int i=0; i<10; ++i) {
106 do { re_q = rand(); } while (re_q == 0);
107 do { im_q = rand(); } while (im_q == 0);
108 numeric r(rand()-RAND_MAX/2, re_q);
109 numeric i(rand()-RAND_MAX/2, im_q);
111 numeric p = numer(z);
112 numeric q = denom(z);
115 clog << z << " erroneously transformed into "
116 << p << "/" << q << " by numer() and denom()" << endl;
123 /* We had some fun with a bug in CLN that caused it to loop forever when
124 * calculating expt(a,b) if b is a rational and a a nonnegative integer.
125 * Implementing a workaround sadly introduced another bug on May 28th 1999
126 * that was fixed on May 31st. The workaround turned out to be stupid and
127 * the bug was finally killed in CLN on September 2nd. */
128 static unsigned numeric_consist2(void)
132 ex zero = numeric(0);
134 ex three = numeric(3);
136 // The hang in this code was the reason for the original workaround
137 if ( pow(two,two/three) == 42 ) {
138 clog << "pow(2,2/3) erroneously returned 42" << endl;
139 ++result; // cannot happen
142 // Actually, this used to raise a FPE after introducing the workaround
143 if ( two*zero != zero ) {
144 clog << "2*0 erroneously returned " << two*zero << endl;
148 // And this returned a cl_F due to the implicit call of numeric::power()
150 if ( !six.info(info_flags::integer) ) {
151 clog << "2*3 erroneously returned the non-integer " << six << endl;
155 // The fix in the workaround left a whole which was fixed hours later...
156 ex another_zero = pow(zero,numeric(1)/numeric(2));
157 if ( another_zero.compare(exZERO()) ) {
158 clog << "pow(0,1/2) erroneously returned" << another_zero << endl;
165 /* Assorted tests to ensure some crucial functions behave exactly as specified
166 * in the documentation. */
167 static unsigned numeric_consist3(void)
170 numeric calc_rem, calc_quo;
173 // check if irem(a, b) and irem(a, b, q) really behave like Maple's
174 // irem(a, b) and irem(a, b, 'q') as advertised in our documentation.
175 // These overloaded routines indeed need to be checked separately since
176 // internally they might be doing something completely different:
177 a = 23; b = 4; calc_rem = irem(a, b);
178 if ( calc_rem != 3 ) {
179 clog << "irem(" << a << "," << b << ") erroneously returned "
183 a = 23; b = -4; calc_rem = irem(a, b);
184 if ( calc_rem != 3 ) {
185 clog << "irem(" << a << "," << b << ") erroneously returned "
189 a = -23; b = 4; calc_rem = irem(a, b);
190 if ( calc_rem != -3 ) {
191 clog << "irem(" << a << "," << b << ") erroneously returned "
195 a = -23; b = -4; calc_rem = irem(a, b);
196 if ( calc_rem != -3 ) {
197 clog << "irem(" << a << "," << b << ") erroneously returned "
201 // and now the overloaded irem(a,b,q):
202 a = 23; b = 4; calc_rem = irem(a, b, calc_quo);
203 if ( calc_rem != 3 || calc_quo != 5 ) {
204 clog << "irem(" << a << "," << b << ",q) erroneously returned "
205 << calc_rem << " with q=" << calc_quo << endl;
208 a = 23; b = -4; calc_rem = irem(a, b, calc_quo);
209 if ( calc_rem != 3 || calc_quo != -5 ) {
210 clog << "irem(" << a << "," << b << ",q) erroneously returned "
211 << calc_rem << " with q=" << calc_quo << endl;
214 a = -23; b = 4; calc_rem = irem(a, b, calc_quo);
215 if ( calc_rem != -3 || calc_quo != -5 ) {
216 clog << "irem(" << a << "," << b << ",q) erroneously returned "
217 << calc_rem << " with q=" << calc_quo << endl;
220 a = -23; b = -4; calc_rem = irem(a, b, calc_quo);
221 if ( calc_rem != -3 || calc_quo != 5 ) {
222 clog << "irem(" << a << "," << b << ",q) erroneously returned "
223 << calc_rem << " with q=" << calc_quo << endl;
226 // check if iquo(a, b) and iquo(a, b, r) really behave like Maple's
227 // iquo(a, b) and iquo(a, b, 'r') as advertised in our documentation.
228 // These overloaded routines indeed need to be checked separately since
229 // internally they might be doing something completely different:
230 a = 23; b = 4; calc_quo = iquo(a, b);
231 if ( calc_quo != 5 ) {
232 clog << "iquo(" << a << "," << b << ") erroneously returned "
236 a = 23; b = -4; calc_quo = iquo(a, b);
237 if ( calc_quo != -5 ) {
238 clog << "iquo(" << a << "," << b << ") erroneously returned "
242 a = -23; b = 4; calc_quo = iquo(a, b);
243 if ( calc_quo != -5 ) {
244 clog << "iquo(" << a << "," << b << ") erroneously returned "
248 a = -23; b = -4; calc_quo = iquo(a, b);
249 if ( calc_quo != 5 ) {
250 clog << "iquo(" << a << "," << b << ") erroneously returned "
254 // and now the overloaded iquo(a,b,r):
255 a = 23; b = 4; calc_quo = iquo(a, b, calc_rem);
256 if ( calc_quo != 5 || calc_rem != 3 ) {
257 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
258 << calc_quo << " with r=" << calc_rem << endl;
261 a = 23; b = -4; calc_quo = iquo(a, b, calc_rem);
262 if ( calc_quo != -5 || calc_rem != 3 ) {
263 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
264 << calc_quo << " with r=" << calc_rem << endl;
267 a = -23; b = 4; calc_quo = iquo(a, b, calc_rem);
268 if ( calc_quo != -5 || calc_rem != -3 ) {
269 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
270 << calc_quo << " with r=" << calc_rem << endl;
273 a = -23; b = -4; calc_quo = iquo(a, b, calc_rem);
274 if ( calc_quo != 5 || calc_rem != -3 ) {
275 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
276 << calc_quo << " with r=" << calc_rem << endl;
283 /* Now we perform some less trivial checks about several functions which should
284 * return exact numbers if possible. */
285 static unsigned numeric_consist4(void)
290 // square roots of squares of integers:
292 for (int i=0; i<42; ++i) {
293 if ( !sqrt(numeric(i*i)).is_integer() ) {
298 clog << "One or more square roots of squares of integers did not return exact integers" << endl;
301 // square roots of squares of rationals:
303 for (int num=0; num<41; ++num) {
304 for (int den=1; den<42; ++den) {
305 if ( !sqrt(numeric(num*num)/numeric(den*den)).is_rational() ) {
311 clog << "One or more square roots of squares of rationals did not return exact integers" << endl;
318 unsigned numeric_consist(void)
322 cout << "checking consistency of numeric types..." << flush;
323 clog << "---------consistency of numeric types:" << endl;
325 result += numeric_consist1();
326 result += numeric_consist2();
327 result += numeric_consist3();
328 result += numeric_consist4();
332 clog << "(no output)" << endl;