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>
26 using namespace GiNaC;
28 /* Simple and maybe somewhat pointless consistency tests of assorted tests and
30 static unsigned numeric_consist1(void)
33 numeric test_int1(42);
35 numeric test_rat1 = test_int1; test_rat1 /= test_int2;
36 test_rat1 = -test_rat1; // -42/5
40 if ( !test_int1.is_integer() ) {
42 << " erroneously not recognized as integer" << endl;
45 if ( !test_int1.is_rational() ) {
47 << " erroneously not recognized as rational" << endl;
51 if ( !test_rat1.is_rational() ) {
53 << " erroneously not recognized as rational" << endl;
56 if ( test_rat1.is_integer() ) {
58 << " erroneously recognized as integer" << endl;
62 int i = numeric(1984).to_int();
64 clog << "conversion of " << i
65 << " from numeric to int failed" << endl;
70 if ( !e1.info(info_flags::posint) ) {
71 clog << "expression " << e1
72 << " erroneously not recognized as positive integer" << endl;
77 if ( ex_to_numeric(e2).is_integer() ) {
78 clog << "expression " << e2
79 << " erroneously recognized as integer" << endl;
83 // The next two were two actual bugs in CLN till June, 12, 1999:
84 test_rat1 = numeric(3)/numeric(2);
85 test_rat1 += test_rat1;
86 if ( !test_rat1.is_integer() ) {
87 clog << "3/2 + 3/2 erroneously not integer 3 but instead "
91 test_rat1 = numeric(3)/numeric(2);
92 numeric test_rat2 = test_rat1 + numeric(1); // 5/2
93 test_rat2 -= test_rat1; // 1
94 if ( !test_rat2.is_integer() ) {
95 clog << "5/2 - 3/2 erroneously not integer 1 but instead "
100 // Check some numerator and denominator calculations:
101 for (int i=0; i<10; ++i) {
103 do { re_q = rand(); } while (re_q == 0);
104 do { im_q = rand(); } while (im_q == 0);
105 numeric r(rand()-RAND_MAX/2, re_q);
106 numeric i(rand()-RAND_MAX/2, im_q);
108 numeric p = numer(z);
109 numeric q = denom(z);
112 clog << z << " erroneously transformed into "
113 << p << "/" << q << " by numer() and denom()" << endl;
120 /* We had some fun with a bug in CLN that caused it to loop forever when
121 * calculating expt(a,b) if b is a rational and a a nonnegative integer.
122 * Implementing a workaround sadly introduced another bug on May 28th 1999
123 * that was fixed on May 31st. The workaround turned out to be stupid and
124 * the bug was finally killed in CLN on September 2nd. */
125 static unsigned numeric_consist2(void)
129 ex zero = numeric(0);
131 ex three = numeric(3);
133 // The hang in this code was the reason for the original workaround
134 if ( pow(two,two/three) == 42 ) {
135 clog << "pow(2,2/3) erroneously returned 42" << endl;
136 ++result; // cannot happen
139 // Actually, this used to raise a FPE after introducing the workaround
140 if ( two*zero != zero ) {
141 clog << "2*0 erroneously returned " << two*zero << endl;
145 // And this returned a cl_F due to the implicit call of numeric::power()
147 if ( !six.info(info_flags::integer) ) {
148 clog << "2*3 erroneously returned the non-integer " << six << endl;
152 // The fix in the workaround left a whole which was fixed hours later...
153 ex another_zero = pow(zero,numeric(1)/numeric(2));
154 if ( another_zero.compare(exZERO()) ) {
155 clog << "pow(0,1/2) erroneously returned" << another_zero << endl;
162 /* Assorted tests to ensure some crucial functions behave exactly as specified
163 * in the documentation. */
164 static unsigned numeric_consist3(void)
167 numeric calc_rem, calc_quo;
170 // check if irem(a, b) and irem(a, b, q) really behave like Maple's
171 // irem(a, b) and irem(a, b, 'q') as advertised in our documentation.
172 // These overloaded routines indeed need to be checked separately since
173 // internally they might be doing something completely different:
174 a = 23; b = 4; calc_rem = irem(a, b);
175 if ( calc_rem != 3 ) {
176 clog << "irem(" << a << "," << b << ") erroneously returned "
180 a = 23; b = -4; calc_rem = irem(a, b);
181 if ( calc_rem != 3 ) {
182 clog << "irem(" << a << "," << b << ") erroneously returned "
186 a = -23; b = 4; calc_rem = irem(a, b);
187 if ( calc_rem != -3 ) {
188 clog << "irem(" << a << "," << b << ") erroneously returned "
192 a = -23; b = -4; calc_rem = irem(a, b);
193 if ( calc_rem != -3 ) {
194 clog << "irem(" << a << "," << b << ") erroneously returned "
198 // and now the overloaded irem(a,b,q):
199 a = 23; b = 4; calc_rem = irem(a, b, calc_quo);
200 if ( calc_rem != 3 || calc_quo != 5 ) {
201 clog << "irem(" << a << "," << b << ",q) erroneously returned "
202 << calc_rem << " with q=" << calc_quo << endl;
205 a = 23; b = -4; calc_rem = irem(a, b, calc_quo);
206 if ( calc_rem != 3 || calc_quo != -5 ) {
207 clog << "irem(" << a << "," << b << ",q) erroneously returned "
208 << calc_rem << " with q=" << calc_quo << endl;
211 a = -23; b = 4; calc_rem = irem(a, b, calc_quo);
212 if ( calc_rem != -3 || calc_quo != -5 ) {
213 clog << "irem(" << a << "," << b << ",q) erroneously returned "
214 << calc_rem << " with q=" << calc_quo << endl;
217 a = -23; b = -4; calc_rem = irem(a, b, calc_quo);
218 if ( calc_rem != -3 || calc_quo != 5 ) {
219 clog << "irem(" << a << "," << b << ",q) erroneously returned "
220 << calc_rem << " with q=" << calc_quo << endl;
223 // check if iquo(a, b) and iquo(a, b, r) really behave like Maple's
224 // iquo(a, b) and iquo(a, b, 'r') as advertised in our documentation.
225 // These overloaded routines indeed need to be checked separately since
226 // internally they might be doing something completely different:
227 a = 23; b = 4; calc_quo = iquo(a, b);
228 if ( calc_quo != 5 ) {
229 clog << "iquo(" << a << "," << b << ") erroneously returned "
233 a = 23; b = -4; calc_quo = iquo(a, b);
234 if ( calc_quo != -5 ) {
235 clog << "iquo(" << a << "," << b << ") erroneously returned "
239 a = -23; b = 4; calc_quo = iquo(a, b);
240 if ( calc_quo != -5 ) {
241 clog << "iquo(" << a << "," << b << ") erroneously returned "
245 a = -23; b = -4; calc_quo = iquo(a, b);
246 if ( calc_quo != 5 ) {
247 clog << "iquo(" << a << "," << b << ") erroneously returned "
251 // and now the overloaded iquo(a,b,r):
252 a = 23; b = 4; calc_quo = iquo(a, b, calc_rem);
253 if ( calc_quo != 5 || calc_rem != 3 ) {
254 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
255 << calc_quo << " with r=" << calc_rem << endl;
258 a = 23; b = -4; calc_quo = iquo(a, b, calc_rem);
259 if ( calc_quo != -5 || calc_rem != 3 ) {
260 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
261 << calc_quo << " with r=" << calc_rem << endl;
264 a = -23; b = 4; calc_quo = iquo(a, b, calc_rem);
265 if ( calc_quo != -5 || calc_rem != -3 ) {
266 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
267 << calc_quo << " with r=" << calc_rem << endl;
270 a = -23; b = -4; calc_quo = iquo(a, b, calc_rem);
271 if ( calc_quo != 5 || calc_rem != -3 ) {
272 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
273 << calc_quo << " with r=" << calc_rem << endl;
280 /* Now we perform some less trivial checks about several functions which should
281 * return exact numbers if possible. */
282 static unsigned numeric_consist4(void)
287 // square roots of squares of integers:
289 for (int i=0; i<42; ++i) {
290 if ( !sqrt(numeric(i*i)).is_integer() ) {
295 clog << "One or more square roots of squares of integers did not return exact integers" << endl;
298 // square roots of squares of rationals:
300 for (int num=0; num<41; ++num) {
301 for (int den=1; den<42; ++den) {
302 if ( !sqrt(numeric(num*num)/numeric(den*den)).is_rational() ) {
308 clog << "One or more square roots of squares of rationals did not return exact integers" << endl;
315 unsigned numeric_consist(void)
319 cout << "checking consistency of numeric types..." << flush;
320 clog << "---------consistency of numeric types:" << endl;
322 result += numeric_consist1();
323 result += numeric_consist2();
324 result += numeric_consist3();
325 result += numeric_consist4();
329 clog << "(no output)" << endl;