]> www.ginac.de Git - ginac.git/blob - check/check_numeric.cpp
fe98933a69b1593f5d5db6df188a04165666e94c
[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-2015 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 "ginac.h"
25 using namespace GiNaC;
26
27 #include <cstdlib> // for rand()
28 #include <iostream>
29 using namespace std;
30
31 /* Simple and maybe somewhat pointless consistency tests of assorted tests and
32  * conversions. */
33 static unsigned check_numeric1()
34 {
35         unsigned result = 0;
36         bool errorflag = false;
37         int re_q, im_q;
38         
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);
45                 numeric z = r + I*i;
46                 numeric p = numer(z);
47                 numeric q = denom(z);
48                 numeric res = p/q;
49                 if (res != z) {
50                         clog << z << " erroneously transformed into " 
51                              << p << "/" << q << " by numer() and denom()" << endl;
52                         errorflag = true;
53                 }
54         }
55         if (errorflag)
56                 ++result;
57         
58         return result;
59 }
60
61 static unsigned check_numeric2()
62 {
63         unsigned result = 0;
64         bool errorflag = false;
65         int i_num, i_den;
66         
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...
71                         numeric nm(1,j);
72                         nm += numeric(int(20.0*rand()/(RAND_MAX+1.0))-10);
73                         // ...a numerator...
74                         do {
75                                 i_num = rand();
76                         } while (i_num<=0);
77                         numeric num(i_num);
78                         // ...and a denominator.
79                         do {
80                                 i_den = (rand())/100;
81                         } while (i_den<=0);
82                         numeric den(i_den);
83                         // construct the radicals:
84                         ex radical = pow(ex(num)/ex(den),ex(nm));
85                         numeric floating = pow(num/den,nm);
86                         // test the result:
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...
92                                 } else {
93                                         clog << "(" << num << "/" << den << ")^(" << nm
94                                                  << ") should have been a product, instead it's "
95                                                  << radical << endl;
96                                         errorflag = true;
97                                 }
98                         }
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;
103                                 errorflag = true;
104                         }
105                 }
106         }
107         if (errorflag)
108                 ++result;
109         
110         return result;
111 }
112
113 unsigned check_numeric()
114 {
115         unsigned result = 0;
116         
117         cout << "checking consistency of numeric types" << flush;
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 }