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-2004 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
26 /* Simple and maybe somewhat pointless consistency tests of assorted tests and
28 static unsigned check_numeric1()
31 bool errorflag = false;
34 // Check some numerator and denominator calculations:
35 for (int rep=0; rep<200; ++rep) {
36 do { re_q = rand(); } while (re_q == 0);
37 do { im_q = rand(); } while (im_q == 0);
38 numeric r(rand()-RAND_MAX/2, re_q);
39 numeric i(rand()-RAND_MAX/2, im_q);
45 clog << z << " erroneously transformed into "
46 << p << "/" << q << " by numer() and denom()" << endl;
56 static unsigned check_numeric2()
59 bool errorflag = false;
62 // Check non-nested radicals (n/d)^(m/n) in ex wrapper class:
63 for (int i=0; i<200; ++i) {
64 for (int j=2; j<13; ++j) {
65 // construct an exponent 1/j...
67 nm += numeric(int(20.0*rand()/(RAND_MAX+1.0))-10);
73 // ...and a denominator.
78 // construct the radicals:
79 ex radical = pow(ex(num)/ex(den),ex(nm));
80 numeric floating = pow(num/den,nm);
82 if (is_a<numeric>(radical)) {
83 // This is very improbable with decent random numbers but it
84 // still can happen, so we better check if it is correct:
85 if (pow(radical,inverse(nm))==num/den) {
86 // Aha! We drew some lucky numbers. Nothing to see here...
88 clog << "(" << num << "/" << den << ")^(" << nm
89 << ") should have been a product, instead it's "
94 numeric ratio = abs(ex_to<numeric>(evalf(radical))/floating);
95 if (ratio>1.0001 && ratio<0.9999) {
96 clog << "(" << num << "/" << den << ")^(" << nm
97 << ") erroneously evaluated to " << radical;
108 unsigned check_numeric()
112 cout << "checking consistency of numeric types" << flush;
113 clog << "---------consistency of numeric types:" << endl;
115 result += check_numeric1(); cout << '.' << flush;
116 result += check_numeric2(); cout << '.' << flush;
119 cout << " passed " << endl;
120 clog << "(no output)" << endl;
122 cout << " failed " << endl;