diff --git a/apps/probability/law/law.cpp b/apps/probability/law/law.cpp index a16f7a2a6..c41094156 100644 --- a/apps/probability/law/law.cpp +++ b/apps/probability/law/law.cpp @@ -14,6 +14,10 @@ float Law::cumulativeDistributiveFunctionAtAbscissa(float x) const { float result = 0.0f; for (int k = 0; k <=end; k++) { result += evaluateAtAbscissa(k); + /* Avoid too long loop */ + if (result > k_maxProbability || k > k_maxNumberOfOperations) { + break; + } } return result; } @@ -39,6 +43,10 @@ float Law::finiteIntegralBetweenAbscissas(float a, float b) const { float result = 0.0f; for (int k = start; k <=end; k++) { result += evaluateAtAbscissa(k); + /* Avoid too long loop */ + if (result > k_maxProbability || k-start > k_maxNumberOfOperations) { + break; + } } return result; } diff --git a/apps/probability/law/law.h b/apps/probability/law/law.h index b00be48b2..a33780994 100644 --- a/apps/probability/law/law.h +++ b/apps/probability/law/law.h @@ -36,6 +36,7 @@ public: virtual float rightIntegralInverseForProbability(float * probability); protected: constexpr static int k_maxNumberOfOperations = 1000000; + constexpr static float k_maxProbability = 0.9995f; constexpr static float k_displayTopMarginRatio = 0.05f; constexpr static float k_displayBottomMarginRatio = 0.2f; constexpr static float k_displayLeftMarginRatio = 0.05f;