]> www.ginac.de Git - ginac.git/blob - check/check_numeric.cpp
Merge git://www.ginac.de/ginac
[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-2008 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 <iostream>
25 #include <cstdlib> // rand()
26 #include "ginac.h"
27 using namespace std;
28 using namespace GiNaC;
29
30 /* Simple and maybe somewhat pointless consistency tests of assorted tests and
31  * conversions. */
32 static unsigned check_numeric1()
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 rep=0; rep<200; ++rep) {
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()
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) {
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 {
74                                 i_num = rand();
75                         } while (i_num<=0);
76                         numeric num(i_num);
77                         // ...and a denominator.
78                         do {
79                                 i_den = (rand())/100;
80                         } while (i_den<=0);
81                         numeric den(i_den);
82                         // construct the radicals:
83                         ex radical = pow(ex(num)/ex(den),ex(nm));
84                         numeric floating = pow(num/den,nm);
85                         // test the result:
86                         if (is_a<numeric>(radical)) {
87                                 // This is very improbable with decent random numbers but it
88                                 // still can happen, so we better check if it is correct:
89                                 if (pow(radical,inverse(nm))==num/den) {
90                                         // Aha! We drew some lucky numbers. Nothing to see here...
91                                 } else {
92                                         clog << "(" << num << "/" << den << ")^(" << nm
93                                                  << ") should have been a product, instead it's "
94                                                  << radical << endl;
95                                         errorflag = true;
96                                 }
97                         }
98                         numeric ratio = abs(ex_to<numeric>(evalf(radical))/floating);
99                         if (ratio>1.0001 && ratio<0.9999) {
100                                 clog << "(" << num << "/" << den << ")^(" << nm
101                                      << ") erroneously evaluated to " << radical;
102                                 errorflag = true;
103                         }
104                 }
105         }
106         if (errorflag)
107                 ++result;
108         
109         return result;
110 }
111
112 unsigned check_numeric()
113 {
114         unsigned result = 0;
115         
116         cout << "checking consistency of numeric types" << flush;
117         clog << "---------consistency of numeric types:" << endl;
118         
119         result += check_numeric1();  cout << '.' << flush;
120         result += check_numeric2();  cout << '.' << flush;
121         
122         return result;
123 }
124
125 int main(int argc, char** argv)
126 {
127         return check_numeric();
128 }