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-2008 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 using namespace GiNaC;
31 /* Simple and maybe somewhat pointless consistency tests of assorted tests and
33 static unsigned exam_numeric1()
36 numeric test_int1(42);
38 numeric test_rat1 = test_int1; test_rat1 /= test_int2;
39 test_rat1 = -test_rat1; // -42/5
40 numeric test_crat = test_rat1+I*test_int2; // 5*I-42/5
44 if (!test_int1.is_integer()) {
46 << " erroneously not recognized as integer" << endl;
49 if (!test_int1.is_rational()) {
51 << " erroneously not recognized as rational" << endl;
55 if (!test_rat1.is_rational()) {
57 << " erroneously not recognized as rational" << endl;
60 if (test_rat1.is_integer()) {
62 << " erroneously recognized as integer" << endl;
66 if (!test_crat.is_crational()) {
68 << " erroneously not recognized as complex rational" << endl;
72 int i = numeric(1984).to_int();
74 clog << "conversion of " << i
75 << " from numeric to int failed" << endl;
80 if (!e1.info(info_flags::posint)) {
81 clog << "expression " << e1
82 << " erroneously not recognized as positive integer" << endl;
87 if (e2.info(info_flags::integer)) {
88 clog << "expression " << e2
89 << " erroneously recognized as integer" << endl;
93 // The next two were two actual bugs in CLN till June, 12, 1999:
94 test_rat1 = numeric(3)/numeric(2);
95 test_rat1 += test_rat1;
96 if (!test_rat1.is_integer()) {
97 clog << "3/2 + 3/2 erroneously not integer 3 but instead "
101 test_rat1 = numeric(3)/numeric(2);
102 numeric test_rat2 = test_rat1 + numeric(1); // 5/2
103 test_rat2 -= test_rat1; // 1
104 if (!test_rat2.is_integer()) {
105 clog << "5/2 - 3/2 erroneously not integer 1 but instead "
106 << test_rat2 << endl;
113 /* We had some fun with a bug in CLN that caused it to loop forever when
114 * calculating expt(a,b) if b is a rational and a a nonnegative integer.
115 * Implementing a workaround sadly introduced another bug on May 28th 1999
116 * that was fixed on May 31st. The workaround turned out to be stupid and
117 * the original bug in CLN was finally killed on September 2nd. */
118 static unsigned exam_numeric2()
122 ex zero = numeric(0);
124 ex three = numeric(3);
126 // The hang in this code was the reason for the original workaround
127 if (pow(two,two/three)==42) {
128 clog << "pow(2,2/3) erroneously returned 42" << endl;
129 ++result; // cannot happen
132 // Actually, this used to raise a FPE after introducing the workaround
133 if (two*zero!=zero) {
134 clog << "2*0 erroneously returned " << two*zero << endl;
138 // And this returned a cl_F due to the implicit call of numeric::power()
140 if (!six.info(info_flags::integer)) {
141 clog << "2*3 erroneously returned the non-integer " << six << endl;
145 // The fix in the workaround left a whole which was fixed hours later...
146 ex another_zero = pow(zero,numeric(1)/numeric(2));
147 if (!another_zero.is_zero()) {
148 clog << "pow(0,1/2) erroneously returned" << another_zero << endl;
155 /* Assorted tests to ensure some crucial functions behave exactly as specified
156 * in the documentation. */
157 static unsigned exam_numeric3()
160 numeric calc_rem, calc_quo;
163 // check if irem(a, b) and irem(a, b, q) really behave like Maple's
164 // irem(a, b) and irem(a, b, 'q') as advertised in our documentation.
165 // These overloaded routines indeed need to be checked separately since
166 // internally they might be doing something completely different:
167 a = 23; b = 4; calc_rem = irem(a, b);
169 clog << "irem(" << a << "," << b << ") erroneously returned "
173 a = 23; b = -4; calc_rem = irem(a, b);
175 clog << "irem(" << a << "," << b << ") erroneously returned "
179 a = -23; b = 4; calc_rem = irem(a, b);
180 if (calc_rem != -3) {
181 clog << "irem(" << a << "," << b << ") erroneously returned "
185 a = -23; b = -4; calc_rem = irem(a, b);
186 if (calc_rem != -3) {
187 clog << "irem(" << a << "," << b << ") erroneously returned "
191 // and now the overloaded irem(a,b,q):
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 a = -23; b = 4; calc_rem = irem(a, b, calc_quo);
205 if (calc_rem != -3 || calc_quo != -5) {
206 clog << "irem(" << a << "," << b << ",q) erroneously returned "
207 << calc_rem << " with q=" << calc_quo << endl;
210 a = -23; b = -4; calc_rem = irem(a, b, calc_quo);
211 if (calc_rem != -3 || calc_quo != 5) {
212 clog << "irem(" << a << "," << b << ",q) erroneously returned "
213 << calc_rem << " with q=" << calc_quo << endl;
216 // check if iquo(a, b) and iquo(a, b, r) really behave like Maple's
217 // iquo(a, b) and iquo(a, b, 'r') as advertised in our documentation.
218 // These overloaded routines indeed need to be checked separately since
219 // internally they might be doing something completely different:
220 a = 23; b = 4; calc_quo = iquo(a, b);
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 a = -23; b = 4; calc_quo = iquo(a, b);
233 if (calc_quo != -5) {
234 clog << "iquo(" << a << "," << b << ") erroneously returned "
238 a = -23; b = -4; calc_quo = iquo(a, b);
240 clog << "iquo(" << a << "," << b << ") erroneously returned "
244 // and now the overloaded iquo(a,b,r):
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;
257 a = -23; b = 4; calc_quo = iquo(a, b, calc_rem);
258 if (calc_quo != -5 || calc_rem != -3) {
259 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
260 << calc_quo << " with r=" << calc_rem << endl;
263 a = -23; b = -4; calc_quo = iquo(a, b, calc_rem);
264 if (calc_quo != 5 || calc_rem != -3) {
265 clog << "iquo(" << a << "," << b << ",r) erroneously returned "
266 << calc_quo << " with r=" << calc_rem << endl;
273 /* Now we perform some less trivial checks about several functions which should
274 * return exact numbers if possible. */
275 static unsigned exam_numeric4()
280 // square roots of squares of integers:
282 for (int i=0; i<42; ++i)
283 if (!sqrt(numeric(i*i)).is_integer())
286 clog << "One or more square roots of squares of integers did not return exact integers" << endl;
290 // square roots of squares of rationals:
292 for (int num=0; num<41; ++num)
293 for (int den=1; den<42; ++den)
294 if (!sqrt(numeric(num*num)/numeric(den*den)).is_rational())
297 clog << "One or more square roots of squares of rationals did not return exact integers" << endl;
304 /* This test examines that simplifications of the form 5^(3/2) -> 5*5^(1/2)
305 * are carried out properly. */
306 static unsigned exam_numeric5()
310 // A variation of one of Ramanujan's wonderful identities must be
311 // verifiable with very primitive means:
312 ex e1 = pow(1 + pow(3,numeric(1,5)) - pow(3,numeric(2,5)),3);
313 ex e2 = expand(e1 - 10 + 5*pow(3,numeric(3,5)));
315 clog << "expand((1+3^(1/5)-3^(2/5))^3-10+5*3^(3/5)) returned "
316 << e2 << " instead of 0." << endl;
323 /* This test checks whether the numeric output/parsing routines are
325 static unsigned exam_numeric6()
330 vector<ex> test_numbers;
331 test_numbers.push_back(numeric(0)); // zero
332 test_numbers.push_back(numeric(1)); // one
333 test_numbers.push_back(numeric(-1)); // minus one
334 test_numbers.push_back(numeric(42)); // positive integer
335 test_numbers.push_back(numeric(-42)); // negative integer
336 test_numbers.push_back(numeric(14,3)); // positive rational
337 test_numbers.push_back(numeric(-14,3)); // negative rational
338 test_numbers.push_back(numeric(3.141)); // positive decimal
339 test_numbers.push_back(numeric(-3.141)); // negative decimal
340 test_numbers.push_back(numeric(0.1974)); // positive decimal, leading zero
341 test_numbers.push_back(numeric(-0.1974)); // negative decimal, leading zero
342 test_numbers.push_back(sym); // symbol
344 for (vector<ex>::const_iterator br=test_numbers.begin(); br<test_numbers.end(); ++br) {
345 for (vector<ex>::const_iterator bi=test_numbers.begin(); bi<test_numbers.end(); ++bi) {
347 for (vector<ex>::const_iterator er=test_numbers.begin(); er<test_numbers.end(); ++er) {
348 for (vector<ex>::const_iterator ei=test_numbers.begin(); ei<test_numbers.end(); ++ei) {
350 // Construct expression, don't test invalid ones
351 ex base = (*br) + (*bi)*I, exponent = (*er) + (*ei)*I, x;
353 x = pow(base, exponent);
359 std::ostringstream s;
362 // Read back expression from string
363 string x_as_string = s.str();
364 ex x_again(x_as_string, lst(sym));
366 // They should be equal
367 if (!x_again.is_equal(x)) {
368 clog << x << " was read back as " << x_again << endl;
379 unsigned exam_numeric()
383 cout << "examining consistency of numeric types" << flush;
385 result += exam_numeric1(); cout << '.' << flush;
386 result += exam_numeric2(); cout << '.' << flush;
387 result += exam_numeric3(); cout << '.' << flush;
388 result += exam_numeric4(); cout << '.' << flush;
389 result += exam_numeric5(); cout << '.' << flush;
390 result += exam_numeric6(); cout << '.' << flush;
395 int main(int argc, char** argv)
397 return exam_numeric();