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-2020 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
25 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;
71 if (test_crat.info(info_flags::nonnegative)) {
73 << " erroneously recognized as non-negative number" << endl;
77 int i = numeric(1984).to_int();
79 clog << "conversion of " << i
80 << " from numeric to int failed" << endl;
85 if (!e1.info(info_flags::posint)) {
86 clog << "expression " << e1
87 << " erroneously not recognized as positive integer" << endl;
92 if (e2.info(info_flags::integer)) {
93 clog << "expression " << e2
94 << " erroneously recognized as integer" << endl;
98 // The next two were two actual bugs in CLN till June, 12, 1999:
99 test_rat1 = numeric(3)/numeric(2);
100 test_rat1 += test_rat1;
101 if (!test_rat1.is_integer()) {
102 clog << "3/2 + 3/2 erroneously not integer 3 but instead "
103 << test_rat1 << endl;
106 test_rat1 = numeric(3)/numeric(2);
107 numeric test_rat2 = test_rat1 + numeric(1); // 5/2
108 test_rat2 -= test_rat1; // 1
109 if (!test_rat2.is_integer()) {
110 clog << "5/2 - 3/2 erroneously not integer 1 but instead "
111 << test_rat2 << 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 original bug in CLN was finally killed on September 2nd. */
123 static unsigned exam_numeric2()
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.is_zero()) {
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 exam_numeric3()
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);
174 clog << "irem(" << a << "," << b << ") erroneously returned "
178 a = 23; b = -4; calc_rem = irem(a, b);
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);
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);
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 exam_numeric4()
285 // square roots of squares of integers:
287 for (int i=0; i<42; ++i)
288 if (!sqrt(numeric(i*i)).is_integer())
291 clog << "One or more square roots of squares of integers did not return exact integers" << endl;
295 // square roots of squares of rationals:
297 for (int num=0; num<41; ++num)
298 for (int den=1; den<42; ++den)
299 if (!sqrt(numeric(num*num)/numeric(den*den)).is_rational())
302 clog << "One or more square roots of squares of rationals did not return exact integers" << endl;
309 /* This test examines that simplifications of the form 5^(3/2) -> 5*5^(1/2)
310 * are carried out properly. */
311 static unsigned exam_numeric5()
315 // A variation of one of Ramanujan's wonderful identities must be
316 // verifiable with very primitive means:
317 ex e1 = pow(1 + pow(3,numeric(1,5)) - pow(3,numeric(2,5)),3);
318 ex e2 = expand(e1 - 10 + 5*pow(3,numeric(3,5)));
320 clog << "expand((1+3^(1/5)-3^(2/5))^3-10+5*3^(3/5)) returned "
321 << e2 << " instead of 0." << endl;
328 /* This test checks whether the numeric output/parsing routines are
330 static unsigned exam_numeric6()
335 vector<ex> test_numbers;
336 test_numbers.push_back(numeric(0)); // zero
337 test_numbers.push_back(numeric(1)); // one
338 test_numbers.push_back(numeric(-1)); // minus one
339 test_numbers.push_back(numeric(42)); // positive integer
340 test_numbers.push_back(numeric(-42)); // negative integer
341 test_numbers.push_back(numeric(14,3)); // positive rational
342 test_numbers.push_back(numeric(-14,3)); // negative rational
343 test_numbers.push_back(numeric(3.141)); // positive decimal
344 test_numbers.push_back(numeric(-3.141)); // negative decimal
345 test_numbers.push_back(numeric(0.1974)); // positive decimal, leading zero
346 test_numbers.push_back(numeric(-0.1974)); // negative decimal, leading zero
347 test_numbers.push_back(sym); // symbol
349 for (vector<ex>::const_iterator br=test_numbers.begin(); br<test_numbers.end(); ++br) {
350 for (vector<ex>::const_iterator bi=test_numbers.begin(); bi<test_numbers.end(); ++bi) {
352 for (vector<ex>::const_iterator er=test_numbers.begin(); er<test_numbers.end(); ++er) {
353 for (vector<ex>::const_iterator ei=test_numbers.begin(); ei<test_numbers.end(); ++ei) {
355 // Construct expression, don't test invalid ones
356 ex base = (*br) + (*bi)*I, exponent = (*er) + (*ei)*I, x;
358 x = pow(base, exponent);
364 std::ostringstream s;
367 // Read back expression from string
368 string x_as_string = s.str();
369 ex x_again(x_as_string, lst{sym});
371 // They should be equal
372 if (!x_again.is_equal(x)) {
373 clog << x << " was read back as " << x_again << endl;
384 unsigned exam_numeric()
388 cout << "examining consistency of numeric types" << flush;
390 result += exam_numeric1(); cout << '.' << flush;
391 result += exam_numeric2(); cout << '.' << flush;
392 result += exam_numeric3(); cout << '.' << flush;
393 result += exam_numeric4(); cout << '.' << flush;
394 result += exam_numeric5(); cout << '.' << flush;
395 result += exam_numeric6(); cout << '.' << flush;
400 int main(int argc, char** argv)
402 return exam_numeric();