From 28df37813caaefcbc71c9237119326e8bb36fb5d Mon Sep 17 00:00:00 2001 From: Ian Abbott Date: Fri, 15 Sep 2017 12:07:41 +0100 Subject: [PATCH] [poincare] Fix recursion bug in adaptiveQuadrature() integration The recursive adaptiveQuadrature() function is initially called with the maximum number of recursion levels to perform, so adaptiveQuadrature() should decrement numberOfRecursions at each recursion level. When adaptiveQuadrature() needs to recurse, it splits the interval in two, calculates the integral for each half by calling itself recursively, and adds the result from each half. However, it then ignores this result and returns NAN because it is checking a stale quadKG.absoluteError value. Rearrange the code to avoid that. --- poincare/src/integral.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/poincare/src/integral.cpp b/poincare/src/integral.cpp index d81949521..580b56f54 100644 --- a/poincare/src/integral.cpp +++ b/poincare/src/integral.cpp @@ -171,14 +171,14 @@ template T Integral::adaptiveQuadrature(T a, T b, T eps, int numberOfIterations, VariableContext xContext, AngleUnit angleUnit) const { DetailedResult quadKG = kronrodGaussQuadrature(a, b, xContext, angleUnit); T result = quadKG.integral; - if (numberOfIterations < k_maxNumberOfIterations && quadKG.absoluteError > eps) { + if (quadKG.absoluteError <= eps) { + return result; + } else if (--numberOfIterations > 0) { T m = (a+b)/2; - result = adaptiveQuadrature(a, m, eps/2, numberOfIterations+1, xContext, angleUnit) + adaptiveQuadrature(m, b, eps/2, numberOfIterations+1, xContext, angleUnit); - } - if (quadKG.absoluteError > eps) { + return adaptiveQuadrature(a, m, eps/2, numberOfIterations, xContext, angleUnit) + adaptiveQuadrature(m, b, eps/2, numberOfIterations, xContext, angleUnit); + } else { return NAN; } - return result; } #endif