1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
/** @file exam_numeric.cpp
*
* These exams creates some numbers and check the result of several boolean
* tests on these numbers like is_integer() etc... */
/*
* GiNaC Copyright (C) 1999-2002 Johannes Gutenberg University Mainz, Germany
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "exams.h"
#include <sstream>
/* Simple and maybe somewhat pointless consistency tests of assorted tests and
* conversions. */
static unsigned exam_numeric1(void)
{
unsigned result = 0;
numeric test_int1(42);
numeric test_int2(5);
numeric test_rat1 = test_int1; test_rat1 /= test_int2;
test_rat1 = -test_rat1; // -42/5
numeric test_crat = test_rat1+I*test_int2; // 5*I-42/5
symbol a("a");
ex e1, e2;
if (!test_int1.is_integer()) {
clog << test_int1
<< " erroneously not recognized as integer" << endl;
++result;
}
if (!test_int1.is_rational()) {
clog << test_int1
<< " erroneously not recognized as rational" << endl;
++result;
}
if (!test_rat1.is_rational()) {
clog << test_rat1
<< " erroneously not recognized as rational" << endl;
++result;
}
if (test_rat1.is_integer()) {
clog << test_rat1
<< " erroneously recognized as integer" << endl;
++result;
}
if (!test_crat.is_crational()) {
clog << test_crat
<< " erroneously not recognized as complex rational" << endl;
++result;
}
int i = numeric(1984).to_int();
if (i-1984) {
clog << "conversion of " << i
<< " from numeric to int failed" << endl;
++result;
}
e1 = test_int1;
if (!e1.info(info_flags::posint)) {
clog << "expression " << e1
<< " erroneously not recognized as positive integer" << endl;
++result;
}
e2 = test_int1 + a;
if (e2.info(info_flags::integer)) {
clog << "expression " << e2
<< " erroneously recognized as integer" << endl;
++result;
}
// The next two were two actual bugs in CLN till June, 12, 1999:
test_rat1 = numeric(3)/numeric(2);
test_rat1 += test_rat1;
if (!test_rat1.is_integer()) {
clog << "3/2 + 3/2 erroneously not integer 3 but instead "
<< test_rat1 << endl;
++result;
}
test_rat1 = numeric(3)/numeric(2);
numeric test_rat2 = test_rat1 + numeric(1); // 5/2
test_rat2 -= test_rat1; // 1
if (!test_rat2.is_integer()) {
clog << "5/2 - 3/2 erroneously not integer 1 but instead "
<< test_rat2 << endl;
++result;
}
return result;
}
/* We had some fun with a bug in CLN that caused it to loop forever when
* calculating expt(a,b) if b is a rational and a a nonnegative integer.
* Implementing a workaround sadly introduced another bug on May 28th 1999
* that was fixed on May 31st. The workaround turned out to be stupid and
* the original bug in CLN was finally killed on September 2nd. */
static unsigned exam_numeric2(void)
{
unsigned result = 0;
ex zero = numeric(0);
ex two = numeric(2);
ex three = numeric(3);
// The hang in this code was the reason for the original workaround
if (pow(two,two/three)==42) {
clog << "pow(2,2/3) erroneously returned 42" << endl;
++result; // cannot happen
}
// Actually, this used to raise a FPE after introducing the workaround
if (two*zero!=zero) {
clog << "2*0 erroneously returned " << two*zero << endl;
++result;
}
// And this returned a cl_F due to the implicit call of numeric::power()
ex six = two*three;
if (!six.info(info_flags::integer)) {
clog << "2*3 erroneously returned the non-integer " << six << endl;
++result;
}
// The fix in the workaround left a whole which was fixed hours later...
ex another_zero = pow(zero,numeric(1)/numeric(2));
if (!another_zero.is_zero()) {
clog << "pow(0,1/2) erroneously returned" << another_zero << endl;
++result;
}
return result;
}
/* Assorted tests to ensure some crucial functions behave exactly as specified
* in the documentation. */
static unsigned exam_numeric3(void)
{
unsigned result = 0;
numeric calc_rem, calc_quo;
numeric a, b;
// check if irem(a, b) and irem(a, b, q) really behave like Maple's
// irem(a, b) and irem(a, b, 'q') as advertised in our documentation.
// These overloaded routines indeed need to be checked separately since
// internally they might be doing something completely different:
a = 23; b = 4; calc_rem = irem(a, b);
if (calc_rem != 3) {
clog << "irem(" << a << "," << b << ") erroneously returned "
<< calc_rem << endl;
++result;
}
a = 23; b = -4; calc_rem = irem(a, b);
if (calc_rem != 3) {
clog << "irem(" << a << "," << b << ") erroneously returned "
<< calc_rem << endl;
++result;
}
a = -23; b = 4; calc_rem = irem(a, b);
if (calc_rem != -3) {
clog << "irem(" << a << "," << b << ") erroneously returned "
<< calc_rem << endl;
++result;
}
a = -23; b = -4; calc_rem = irem(a, b);
if (calc_rem != -3) {
clog << "irem(" << a << "," << b << ") erroneously returned "
<< calc_rem << endl;
++result;
}
// and now the overloaded irem(a,b,q):
a = 23; b = 4; calc_rem = irem(a, b, calc_quo);
if (calc_rem != 3 || calc_quo != 5) {
clog << "irem(" << a << "," << b << ",q) erroneously returned "
<< calc_rem << " with q=" << calc_quo << endl;
++result;
}
a = 23; b = -4; calc_rem = irem(a, b, calc_quo);
if (calc_rem != 3 || calc_quo != -5) {
clog << "irem(" << a << "," << b << ",q) erroneously returned "
<< calc_rem << " with q=" << calc_quo << endl;
++result;
}
a = -23; b = 4; calc_rem = irem(a, b, calc_quo);
if (calc_rem != -3 || calc_quo != -5) {
clog << "irem(" << a << "," << b << ",q) erroneously returned "
<< calc_rem << " with q=" << calc_quo << endl;
++result;
}
a = -23; b = -4; calc_rem = irem(a, b, calc_quo);
if (calc_rem != -3 || calc_quo != 5) {
clog << "irem(" << a << "," << b << ",q) erroneously returned "
<< calc_rem << " with q=" << calc_quo << endl;
++result;
}
// check if iquo(a, b) and iquo(a, b, r) really behave like Maple's
// iquo(a, b) and iquo(a, b, 'r') as advertised in our documentation.
// These overloaded routines indeed need to be checked separately since
// internally they might be doing something completely different:
a = 23; b = 4; calc_quo = iquo(a, b);
if (calc_quo != 5) {
clog << "iquo(" << a << "," << b << ") erroneously returned "
<< calc_quo << endl;
++result;
}
a = 23; b = -4; calc_quo = iquo(a, b);
if (calc_quo != -5) {
clog << "iquo(" << a << "," << b << ") erroneously returned "
<< calc_quo << endl;
++result;
}
a = -23; b = 4; calc_quo = iquo(a, b);
if (calc_quo != -5) {
clog << "iquo(" << a << "," << b << ") erroneously returned "
<< calc_quo << endl;
++result;
}
a = -23; b = -4; calc_quo = iquo(a, b);
if (calc_quo != 5) {
clog << "iquo(" << a << "," << b << ") erroneously returned "
<< calc_quo << endl;
++result;
}
// and now the overloaded iquo(a,b,r):
a = 23; b = 4; calc_quo = iquo(a, b, calc_rem);
if (calc_quo != 5 || calc_rem != 3) {
clog << "iquo(" << a << "," << b << ",r) erroneously returned "
<< calc_quo << " with r=" << calc_rem << endl;
++result;
}
a = 23; b = -4; calc_quo = iquo(a, b, calc_rem);
if (calc_quo != -5 || calc_rem != 3) {
clog << "iquo(" << a << "," << b << ",r) erroneously returned "
<< calc_quo << " with r=" << calc_rem << endl;
++result;
}
a = -23; b = 4; calc_quo = iquo(a, b, calc_rem);
if (calc_quo != -5 || calc_rem != -3) {
clog << "iquo(" << a << "," << b << ",r) erroneously returned "
<< calc_quo << " with r=" << calc_rem << endl;
++result;
}
a = -23; b = -4; calc_quo = iquo(a, b, calc_rem);
if (calc_quo != 5 || calc_rem != -3) {
clog << "iquo(" << a << "," << b << ",r) erroneously returned "
<< calc_quo << " with r=" << calc_rem << endl;
++result;
}
return result;
}
/* Now we perform some less trivial checks about several functions which should
* return exact numbers if possible. */
static unsigned exam_numeric4(void)
{
unsigned result = 0;
bool passed;
// square roots of squares of integers:
passed = true;
for (int i=0; i<42; ++i)
if (!sqrt(numeric(i*i)).is_integer())
passed = false;
if (!passed) {
clog << "One or more square roots of squares of integers did not return exact integers" << endl;
++result;
}
// square roots of squares of rationals:
passed = true;
for (int num=0; num<41; ++num)
for (int den=1; den<42; ++den)
if (!sqrt(numeric(num*num)/numeric(den*den)).is_rational())
passed = false;
if (!passed) {
clog << "One or more square roots of squares of rationals did not return exact integers" << endl;
++result;
}
return result;
}
/* This test examines that simplifications of the form 5^(3/2) -> 5*5^(1/2)
* are carried out properly. */
static unsigned exam_numeric5(void)
{
unsigned result = 0;
// A variation of one of Ramanujan's wonderful identities must be
// verifiable with very primitive means:
ex e1 = pow(1 + pow(3,numeric(1,5)) - pow(3,numeric(2,5)),3);
ex e2 = expand(e1 - 10 + 5*pow(3,numeric(3,5)));
if (!e2.is_zero()) {
clog << "expand((1+3^(1/5)-3^(2/5))^3-10+5*3^(3/5)) returned "
<< e2 << " instead of 0." << endl;
++result;
}
return result;
}
/* This test checks whether the numeric output/parsing routines are
consistent. */
static unsigned exam_numeric6(void)
{
unsigned result = 0;
symbol sym("sym");
vector<ex> test_numbers;
test_numbers.push_back(numeric(0)); // zero
test_numbers.push_back(numeric(1)); // one
test_numbers.push_back(numeric(-1)); // minus one
test_numbers.push_back(numeric(42)); // positive integer
test_numbers.push_back(numeric(-42)); // negative integer
test_numbers.push_back(numeric(14,3)); // positive rational
test_numbers.push_back(numeric(-14,3)); // negative rational
test_numbers.push_back(numeric(3.141)); // positive decimal
test_numbers.push_back(numeric(-3.141)); // negative decimal
test_numbers.push_back(numeric(0.1974)); // positive decimal, leading zero
test_numbers.push_back(numeric(-0.1974)); // negative decimal, leading zero
test_numbers.push_back(sym); // symbol
for (vector<ex>::const_iterator br=test_numbers.begin(); br<test_numbers.end(); ++br) {
for (vector<ex>::const_iterator bi=test_numbers.begin(); bi<test_numbers.end(); ++bi) {
for (vector<ex>::const_iterator er=test_numbers.begin(); er<test_numbers.end(); ++er) {
for (vector<ex>::const_iterator ei=test_numbers.begin(); ei<test_numbers.end(); ++ei) {
// Construct expression, don't test invalid ones
ex base = (*br) + (*bi)*I, exponent = (*er) + (*ei)*I, x;
try {
x = pow(base, exponent);
} catch (...) {
continue;
}
// Print to string
std::ostringstream s;
s << x;
// Read back expression from string
string x_as_string = s.str();
ex x_again(x_as_string, lst(sym));
// They should be equal
if (!x_again.is_equal(x)) {
clog << x << " was read back as " << x_again << endl;
++result;
}
}
}
}
}
return result;
}
unsigned exam_numeric(void)
{
unsigned result = 0;
cout << "examining consistency of numeric types" << flush;
clog << "----------consistency of numeric types:" << endl;
result += exam_numeric1(); cout << '.' << flush;
result += exam_numeric2(); cout << '.' << flush;
result += exam_numeric3(); cout << '.' << flush;
result += exam_numeric4(); cout << '.' << flush;
result += exam_numeric5(); cout << '.' << flush;
result += exam_numeric6(); cout << '.' << flush;
if (!result) {
cout << " passed " << endl;
clog << "(no output)" << endl;
} else {
cout << " failed " << endl;
}
return result;
}
|