From: Richard Kreckel Date: Thu, 17 Dec 2015 21:30:13 +0000 (+0100) Subject: [bugfix] Make sure add::eval() collects all numeric terms *correctly*. X-Git-Tag: ginac_1-6-6~3 X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=commitdiff_plain;h=83b067d5b1a232b56039f227a9445cea3dd3225a;hp=2639812c0ba4e1f9620660bbba1f12bf5b865e29 [bugfix] Make sure add::eval() collects all numeric terms *correctly*. This fixes a bug introduced with in 2010 with 89d5356b. --- diff --git a/check/exam_paranoia.cpp b/check/exam_paranoia.cpp index 711c64a6..81dda853 100644 --- a/check/exam_paranoia.cpp +++ b/check/exam_paranoia.cpp @@ -468,16 +468,26 @@ static unsigned exam_paranoia17() } // Bug in add::eval() could result in numeric terms not being collected into -// the overall coefficient. Fixed on Sep 22, 2010 +// the overall coefficient. Fixed first on Sep 22, 2010 and again on Dec 17 2015 static unsigned exam_paranoia18() { + unsigned result = 0; + ex sqrt2 = sqrt(ex(2)); - ex e = 1+2*(sqrt2+1)*(sqrt2-1); - if ( e.real_part() != 3 ) { + ex e = 1 + 2*(sqrt2+1)*(sqrt2-1); + if (e.real_part() != 3) { clog << "real_part(1+2*(sqrt(2)+1)*(sqrt(2)-1)) failed to evaluate to 3\n"; - return 1; + ++result; } - return 0; + + ex sqrt3 = sqrt(ex(3)); + ex f = 2 + 2*(sqrt2+1)*(sqrt2-1) - 2*(sqrt3+1)*(sqrt3-1); + if (f.real_part() != 0) { + clog << "real_part(2+2*(sqrt(2)+1)*(sqrt(2)-1)-3*(sqrt(3)+1)*(sqrt(3)-1)) failed to evaluate to 0\n"; + ++result; + } + + return result; } // Bug in mul::conjugate when factors are evaluated at branch cuts, reported as diff --git a/ginac/add.cpp b/ginac/add.cpp index ccd49240..1a19d86d 100644 --- a/ginac/add.cpp +++ b/ginac/add.cpp @@ -388,11 +388,11 @@ ex add::eval(int level) const if (terms_to_collect) { std::auto_ptr s(new epvector); s->reserve(seq_size - terms_to_collect); - numeric oc = *_num1_p; + numeric oc = *_num0_p; j = seq.begin(); while (j != last) { if (unlikely(is_a(j->rest))) - oc = oc.mul(ex_to(j->rest)).mul(ex_to(j->coeff)); + oc = oc.add(ex_to(j->rest).mul(ex_to(j->coeff))); else s->push_back(*j); ++j;