]> www.ginac.de Git - ginac.git/blob - check/check_numeric.cpp
using () operator for setting matrix elements
[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-2001 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 /* Simple and maybe somewhat pointless consistency tests of assorted tests and
27  * conversions. */
28 static unsigned check_numeric1(void)
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(void)
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) {  // FIXME: run to ~200
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 { i_num = rand(); } while (i_num == 0);
70                         numeric num(i_num);
71                         // ...and a denominator.
72                         do { i_den = (rand())/100; } while (i_den == 0);
73                         numeric den(i_den);
74                         // construct the radicals:
75                         ex radical = pow(ex(num)/ex(den),ex(nm));
76                         numeric floating = pow(num/den,nm);
77                         // test the result:
78                         if (is_ex_of_type(radical,numeric)) {
79                                 clog << "(" << num << "/" << den << ")^(" << nm
80                                      << ") should have been a product, instead it's "
81                                      << radical << endl;
82                                 errorflag = true;
83                         }
84                         numeric ratio = ex_to_numeric(evalf(radical))/floating;
85                         if (ratio>1.0001 && ratio<0.9999) {
86                                 clog << "(" << num << "/" << den << ")^(" << nm
87                                      << ") erroneously evaluated to " << radical;
88                                 errorflag = true;
89                         }
90                 }
91         }
92         if (errorflag)
93                 ++result;
94         
95         return result;
96 }
97
98 unsigned check_numeric(void)
99 {
100         unsigned result = 0;
101         
102         cout << "checking consistency of numeric types" << flush;
103         clog << "---------consistency of numeric types:" << endl;
104         
105         result += check_numeric1();  cout << '.' << flush;
106         result += check_numeric2();  cout << '.' << flush;
107         
108         if (!result) {
109                 cout << " passed " << endl;
110                 clog << "(no output)" << endl;
111         } else {
112                 cout << " failed " << endl;
113         }
114         
115         return result;
116 }