]> www.ginac.de Git - ginac.git/blob - check/check_numeric.cpp
Removed remark that symbols always commute.
[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-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  */
23
24 #include "checks.h"
25
26 /* Simple and maybe somewhat pointless consistency tests of assorted tests and
27  * conversions. */
28 static unsigned check_numeric1()
29 {
30         unsigned result = 0;
31         bool errorflag = false;
32         int re_q, im_q;
33         
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);
40                 numeric z = r + I*i;
41                 numeric p = numer(z);
42                 numeric q = denom(z);
43                 numeric res = p/q;
44                 if (res != z) {
45                         clog << z << " erroneously transformed into " 
46                              << p << "/" << q << " by numer() and denom()" << endl;
47                         errorflag = true;
48                 }
49         }
50         if (errorflag)
51                 ++result;
52         
53         return result;
54 }
55
56 static unsigned check_numeric2()
57 {
58         unsigned result = 0;
59         bool errorflag = false;
60         int i_num, i_den;
61         
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...
66                         numeric nm(1,j);
67                         nm += numeric(int(20.0*rand()/(RAND_MAX+1.0))-10);
68                         // ...a numerator...
69                         do {
70                                 i_num = rand();
71                         } while (i_num<=0);
72                         numeric num(i_num);
73                         // ...and a denominator.
74                         do {
75                                 i_den = (rand())/100;
76                         } 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_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...
87                                 } else {
88                                         clog << "(" << num << "/" << den << ")^(" << nm
89                                                  << ") should have been a product, instead it's "
90                                                  << radical << endl;
91                                         errorflag = true;
92                                 }
93                         }
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;
98                                 errorflag = true;
99                         }
100                 }
101         }
102         if (errorflag)
103                 ++result;
104         
105         return result;
106 }
107
108 unsigned check_numeric()
109 {
110         unsigned result = 0;
111         
112         cout << "checking consistency of numeric types" << flush;
113         clog << "---------consistency of numeric types:" << endl;
114         
115         result += check_numeric1();  cout << '.' << flush;
116         result += check_numeric2();  cout << '.' << flush;
117         
118         if (!result) {
119                 cout << " passed " << endl;
120                 clog << "(no output)" << endl;
121         } else {
122                 cout << " failed " << endl;
123         }
124         
125         return result;
126 }