]> www.ginac.de Git - ginac.git/blob - check/check_numeric.cpp
- added check for normal((b*a-c*a)/(4-a)) bug
[ginac.git] / check / check_numeric.cpp
1 /** @file check_numeric.cpp
2  *
3  *  These exams creates some numbers and check the result of several boolean
4  *  tests on these numbers like is_integer() etc... */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "checks.h"
25
26 #ifndef NO_NAMESPACE_GINAC
27 using namespace GiNaC;
28 #endif // ndef NO_NAMESPACE_GINAC
29
30 /* Simple and maybe somewhat pointless consistency tests of assorted tests and
31  * conversions. */
32 static unsigned check_numeric1(void)
33 {
34     unsigned result = 0;
35     bool errorflag = false;
36     int re_q, im_q;
37     
38     // Check some numerator and denominator calculations:
39     for (int i=0; i<200; ++i) {
40         do { re_q = rand(); } while (re_q == 0);
41         do { im_q = rand(); } while (im_q == 0);
42         numeric r(rand()-RAND_MAX/2, re_q);
43         numeric i(rand()-RAND_MAX/2, im_q);
44         numeric z = r + I*i;
45         numeric p = numer(z);
46         numeric q = denom(z);
47         numeric res = p/q;
48         if (res != z) {
49             clog << z << " erroneously transformed into " 
50                  << p << "/" << q << " by numer() and denom()" << endl;
51             errorflag = true;
52         }
53     }
54     if (errorflag)
55         ++result;
56     
57     return result;
58 }
59
60 static unsigned check_numeric2(void)
61 {
62     unsigned result = 0;
63     bool errorflag = false;
64     int i_num, i_den;
65     
66     // Check non-nested radicals (n/d)^(m/n) in ex wrapper class:
67     for (int i=0; i<200; ++i) {  // FIXME: run to ~200
68         for (int j=2; j<13; ++j) {
69             // construct an exponent 1/j...
70             numeric nm(1,j);
71             nm += numeric(int(20.0*rand()/(RAND_MAX+1.0))-10);
72             // ...a numerator...
73             do { i_num = rand(); } while (i_num == 0);
74             numeric num(i_num);
75             // ...and a denominator.
76             do { i_den = (rand())/100; } while (i_den == 0);
77             numeric den(i_den);
78             // construct the radicals:
79             ex radical = pow(ex(num)/ex(den),ex(nm));
80             numeric floating = pow(num/den,nm);
81             // test the result:
82             if (is_ex_of_type(radical,numeric)) {
83                 clog << "(" << num << "/" << den << ")^(" << nm
84                      << ") should have been a product, instead it's "
85                      << radical << endl;
86                 errorflag = true;
87             }
88             numeric ratio = ex_to_numeric(evalf(radical))/floating;
89             if (ratio>1.0001 && ratio<0.9999) {
90                 clog << "(" << num << "/" << den << ")^(" << nm
91                      << ") erroneously evaluated to " << radical;
92                 errorflag = true;
93             }
94         }
95     }
96     if (errorflag)
97         ++result;
98     
99     return result;
100 }
101
102 unsigned check_numeric(void)
103 {
104     unsigned result = 0;
105     
106     cout << "checking consistency of numeric types" << flush;
107     clog << "---------consistency of numeric types:" << endl;
108     
109     result += check_numeric1();  cout << '.' << flush;
110     result += check_numeric2();  cout << '.' << flush;
111     
112     if (!result) {
113         cout << " passed " << endl;
114         clog << "(no output)" << endl;
115     } else {
116         cout << " failed " << endl;
117     }
118     
119     return result;
120 }