1 /** @file exam_numeric.cpp
3 * These exams creates some numbers and check the result of several boolean
4 * tests on these numbers like is_integer() etc... */
7 * GiNaC Copyright (C) 1999-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 /* Simple and maybe somewhat pointless consistency tests of assorted tests and
30 static unsigned exam_numeric1()
33 numeric test_int1(42);
35 numeric test_rat1 = test_int1; test_rat1 /= test_int2;
36 test_rat1 = -test_rat1; // -42/5
37 numeric test_crat = test_rat1+I*test_int2; // 5*I-42/5
41 if (!test_int1.is_integer()) {
43 << " erroneously not recognized as integer" << endl;
46 if (!test_int1.is_rational()) {
48 << " erroneously not recognized as rational" << endl;
52 if (!test_rat1.is_rational()) {
54 << " erroneously not recognized as rational" << endl;
57 if (test_rat1.is_integer()) {
59 << " erroneously recognized as integer" << endl;
63 if (!test_crat.is_crational()) {
65 << " erroneously not recognized as complex rational" << endl;
69 int i = numeric(1984).to_int();
71 clog << "conversion of " << i
72 << " from numeric to int failed" << endl;
77 if (!e1.info(info_flags::posint)) {
78 clog << "expression " << e1
79 << " erroneously not recognized as positive integer" << endl;
84 if (e2.info(info_flags::integer)) {
85 clog << "expression " << e2
86 << " erroneously recognized as integer" << endl;
90 // The next two were two actual bugs in CLN till June, 12, 1999:
91 test_rat1 = numeric(3)/numeric(2);
92 test_rat1 += test_rat1;
93 if (!test_rat1.is_integer()) {
94 clog << "3/2 + 3/2 erroneously not integer 3 but instead "
98 test_rat1 = numeric(3)/numeric(2);
99 numeric test_rat2 = test_rat1 + numeric(1); // 5/2
100 test_rat2 -= test_rat1; // 1
101 if (!test_rat2.is_integer()) {
102 clog << "5/2 - 3/2 erroneously not integer 1 but instead "
103 << test_rat2 << endl;
110 /* We had some fun with a bug in CLN that caused it to loop forever when
111 * calculating expt(a,b) if b is a rational and a a nonnegative integer.
112 * Implementing a workaround sadly introduced another bug on May 28th 1999
113 * that was fixed on May 31st. The workaround turned out to be stupid and
114 * the original bug in CLN was finally killed on September 2nd. */
115 static unsigned exam_numeric2()
119 ex zero = numeric(0);
121 ex three = numeric(3);
123 // The hang in this code was the reason for the original workaround
124 if (pow(two,two/three)==42) {
125 clog << "pow(2,2/3) erroneously returned 42" << endl;
126 ++result; // cannot happen
129 // Actually, this used to raise a FPE after introducing the workaround
130 if (two*zero!=zero) {
131 clog << "2*0 erroneously returned " << two*zero << endl;
135 // And this returned a cl_F due to the implicit call of numeric::power()
137 if (!six.info(info_flags::integer)) {
138 clog << "2*3 erroneously returned the non-integer " << six << endl;
142 // The fix in the workaround left a whole which was fixed hours later...
143 ex another_zero = pow(zero,numeric(1)/numeric(2));
144 if (!another_zero.is_zero()) {
145 clog << "pow(0,1/2) erroneously returned" << another_zero << endl;
152 /* Assorted tests to ensure some crucial functions behave exactly as specified
153 * in the documentation. */
154 static unsigned exam_numeric3()
157 numeric calc_rem, calc_quo;
160 // check if irem(a, b) and irem(a, b, q) really behave like Maple's
161 // irem(a, b) and irem(a, b, 'q') as advertised in our documentation.
162 // These overloaded routines indeed need to be checked separately since
163 // internally they might be doing something completely different:
164 a = 23; b = 4; calc_rem = irem(a, b);
166 clog << "irem(" << a << "," << b << ") erroneously returned "
170 a = 23; b = -4; calc_rem = irem(a, b);
172 clog << "irem(" << a << "," << b << ") erroneously returned "
176 a = -23; b = 4; calc_rem = irem(a, b);
177 if (calc_rem != -3) {
178 clog << "irem(" << a << "," << b << ") erroneously returned "
182 a = -23; b = -4; calc_rem = irem(a, b);
183 if (calc_rem != -3) {
184 clog << "irem(" << a << "," << b << ") erroneously returned "
188 // and now the overloaded irem(a,b,q):
189 a = 23; b = 4; calc_rem = irem(a, b, calc_quo);
190 if (calc_rem != 3 || calc_quo != 5) {
191 clog << "irem(" << a << "," << b << ",q) erroneously returned "
192 << calc_rem << " with q=" << calc_quo << endl;
195 a = 23; b = -4; calc_rem = irem(a, b, calc_quo);
196 if (calc_rem != 3 || calc_quo != -5) {
197 clog << "irem(" << a << "," << b << ",q) erroneously returned "
198 << calc_rem << " with q=" << calc_quo << endl;
201 a = -23; b = 4; calc_rem = irem(a, b, calc_quo);
202 if (calc_rem != -3 || calc_quo != -5) {
203 clog << "irem(" << a << "," << b << ",q) erroneously returned "
204 << calc_rem << " with q=" << calc_quo << endl;
207 a = -23; b = -4; calc_rem = irem(a, b, calc_quo);
208 if (calc_rem != -3 || calc_quo != 5) {
209 clog << "irem(" << a << "," << b << ",q) erroneously returned "
210 << calc_rem << " with q=" << calc_quo << endl;
213 // check if iquo(a, b) and iquo(a, b, r) really behave like Maple's
214 // iquo(a, b) and iquo(a, b, 'r') as advertised in our documentation.
215 // These overloaded routines indeed need to be checked separately since
216 // internally they might be doing something completely different:
217 a = 23; b = 4; calc_quo = iquo(a, b);
219 clog << "iquo(" << a << "," << b << ") erroneously returned "
223 a = 23; b = -4; calc_quo = iquo(a, b);
224 if (calc_quo != -5) {
225 clog << "iquo(" << a << "," << b << ") erroneously returned "
229 a = -23; b = 4; calc_quo = iquo(a, b);
230 if (calc_quo != -5) {
231 clog << "iquo(" << a << "," << b << ") erroneously returned "
235 a = -23; b = -4; calc_quo = iquo(a, b);
237 clog << "iquo(" << a << "," << b << ") erroneously returned "
241 // and now the overloaded iquo(a,b,r):
242 a = 23; b = 4; calc_quo = iquo(a, b, calc_rem);
243 if (calc_quo != 5 || calc_rem != 3) {
244 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
245 << calc_quo << " with r=" << calc_rem << endl;
248 a = 23; b = -4; calc_quo = iquo(a, b, calc_rem);
249 if (calc_quo != -5 || calc_rem != 3) {
250 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
251 << calc_quo << " with r=" << calc_rem << endl;
254 a = -23; b = 4; calc_quo = iquo(a, b, calc_rem);
255 if (calc_quo != -5 || calc_rem != -3) {
256 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
257 << calc_quo << " with r=" << calc_rem << endl;
260 a = -23; b = -4; calc_quo = iquo(a, b, calc_rem);
261 if (calc_quo != 5 || calc_rem != -3) {
262 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
263 << calc_quo << " with r=" << calc_rem << endl;
270 /* Now we perform some less trivial checks about several functions which should
271 * return exact numbers if possible. */
272 static unsigned exam_numeric4()
277 // square roots of squares of integers:
279 for (int i=0; i<42; ++i)
280 if (!sqrt(numeric(i*i)).is_integer())
283 clog << "One or more square roots of squares of integers did not return exact integers" << endl;
287 // square roots of squares of rationals:
289 for (int num=0; num<41; ++num)
290 for (int den=1; den<42; ++den)
291 if (!sqrt(numeric(num*num)/numeric(den*den)).is_rational())
294 clog << "One or more square roots of squares of rationals did not return exact integers" << endl;
301 /* This test examines that simplifications of the form 5^(3/2) -> 5*5^(1/2)
302 * are carried out properly. */
303 static unsigned exam_numeric5()
307 // A variation of one of Ramanujan's wonderful identities must be
308 // verifiable with very primitive means:
309 ex e1 = pow(1 + pow(3,numeric(1,5)) - pow(3,numeric(2,5)),3);
310 ex e2 = expand(e1 - 10 + 5*pow(3,numeric(3,5)));
312 clog << "expand((1+3^(1/5)-3^(2/5))^3-10+5*3^(3/5)) returned "
313 << e2 << " instead of 0." << endl;
320 /* This test checks whether the numeric output/parsing routines are
322 static unsigned exam_numeric6()
327 vector<ex> test_numbers;
328 test_numbers.push_back(numeric(0)); // zero
329 test_numbers.push_back(numeric(1)); // one
330 test_numbers.push_back(numeric(-1)); // minus one
331 test_numbers.push_back(numeric(42)); // positive integer
332 test_numbers.push_back(numeric(-42)); // negative integer
333 test_numbers.push_back(numeric(14,3)); // positive rational
334 test_numbers.push_back(numeric(-14,3)); // negative rational
335 test_numbers.push_back(numeric(3.141)); // positive decimal
336 test_numbers.push_back(numeric(-3.141)); // negative decimal
337 test_numbers.push_back(numeric(0.1974)); // positive decimal, leading zero
338 test_numbers.push_back(numeric(-0.1974)); // negative decimal, leading zero
339 test_numbers.push_back(sym); // symbol
341 for (vector<ex>::const_iterator br=test_numbers.begin(); br<test_numbers.end(); ++br) {
342 for (vector<ex>::const_iterator bi=test_numbers.begin(); bi<test_numbers.end(); ++bi) {
344 for (vector<ex>::const_iterator er=test_numbers.begin(); er<test_numbers.end(); ++er) {
345 for (vector<ex>::const_iterator ei=test_numbers.begin(); ei<test_numbers.end(); ++ei) {
347 // Construct expression, don't test invalid ones
348 ex base = (*br) + (*bi)*I, exponent = (*er) + (*ei)*I, x;
350 x = pow(base, exponent);
356 std::ostringstream s;
359 // Read back expression from string
360 string x_as_string = s.str();
361 ex x_again(x_as_string, lst(sym));
363 // They should be equal
364 if (!x_again.is_equal(x)) {
365 clog << x << " was read back as " << x_again << endl;
376 unsigned exam_numeric()
380 cout << "examining consistency of numeric types" << flush;
381 clog << "----------consistency of numeric types:" << endl;
383 result += exam_numeric1(); cout << '.' << flush;
384 result += exam_numeric2(); cout << '.' << flush;
385 result += exam_numeric3(); cout << '.' << flush;
386 result += exam_numeric4(); cout << '.' << flush;
387 result += exam_numeric5(); cout << '.' << flush;
388 result += exam_numeric6(); cout << '.' << flush;
391 cout << " passed " << endl;
392 clog << "(no output)" << endl;
394 cout << " failed " << endl;