1 /** @file check_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-2011 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;
27 #include <cstdlib> // for rand()
31 /* Simple and maybe somewhat pointless consistency tests of assorted tests and
33 static unsigned check_numeric1()
36 bool errorflag = false;
39 // Check some numerator and denominator calculations:
40 for (int rep=0; rep<200; ++rep) {
41 do { re_q = rand(); } while (re_q == 0);
42 do { im_q = rand(); } while (im_q == 0);
43 numeric r(rand()-RAND_MAX/2, re_q);
44 numeric i(rand()-RAND_MAX/2, im_q);
50 clog << z << " erroneously transformed into "
51 << p << "/" << q << " by numer() and denom()" << endl;
61 static unsigned check_numeric2()
64 bool errorflag = false;
67 // Check non-nested radicals (n/d)^(m/n) in ex wrapper class:
68 for (int i=0; i<200; ++i) {
69 for (int j=2; j<13; ++j) {
70 // construct an exponent 1/j...
72 nm += numeric(int(20.0*rand()/(RAND_MAX+1.0))-10);
78 // ...and a denominator.
83 // construct the radicals:
84 ex radical = pow(ex(num)/ex(den),ex(nm));
85 numeric floating = pow(num/den,nm);
87 if (is_a<numeric>(radical)) {
88 // This is very improbable with decent random numbers but it
89 // still can happen, so we better check if it is correct:
90 if (pow(radical,inverse(nm))==num/den) {
91 // Aha! We drew some lucky numbers. Nothing to see here...
93 clog << "(" << num << "/" << den << ")^(" << nm
94 << ") should have been a product, instead it's "
99 numeric ratio = abs(ex_to<numeric>(evalf(radical))/floating);
100 if (ratio>1.0001 && ratio<0.9999) {
101 clog << "(" << num << "/" << den << ")^(" << nm
102 << ") erroneously evaluated to " << radical;
113 unsigned check_numeric()
117 cout << "checking consistency of numeric types" << flush;
119 result += check_numeric1(); cout << '.' << flush;
120 result += check_numeric2(); cout << '.' << flush;
125 int main(int argc, char** argv)
127 return check_numeric();