From b7a74c2c46c232889b4e5870f8d7345630cce5de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Tue, 5 Dec 2017 12:10:10 +0100 Subject: [PATCH] [apps] In Probability: fix bug when a = 0 and b = 1E-43 < FLT_EPSILON Change-Id: Ib4fd405178fa0d7e8571f2da14e177fe67e3c96d --- apps/probability/law/uniform_law.cpp | 12 +++++++----- apps/probability/law/uniform_law.h | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/apps/probability/law/uniform_law.cpp b/apps/probability/law/uniform_law.cpp index 419ede400..9dc323f4a 100644 --- a/apps/probability/law/uniform_law.cpp +++ b/apps/probability/law/uniform_law.cpp @@ -1,4 +1,6 @@ #include "uniform_law.h" +#include +#include #include namespace Probability { @@ -40,14 +42,14 @@ I18n::Message UniformLaw::parameterDefinitionAtIndex(int index) { float UniformLaw::xMin() { assert(m_parameter2 >= m_parameter1); - if (m_parameter1 == m_parameter2) { + if (m_parameter2 - m_parameter1 < FLT_EPSILON) { return m_parameter1 - 1.0f; } return m_parameter1 - 0.6f*(m_parameter2 - m_parameter1); } float UniformLaw::xMax() { - if (m_parameter1 == m_parameter2) { + if (m_parameter2 - m_parameter1 < FLT_EPSILON) { return m_parameter1 + 1.0f; } return m_parameter2 + 0.6f*(m_parameter2 - m_parameter1); @@ -58,15 +60,15 @@ float UniformLaw::yMin() { } float UniformLaw::yMax() { - float result = m_parameter1 == m_parameter2 ? k_diracMaximum : 1.0f/(m_parameter2-m_parameter1); - if (result <= 0.0f) { + float result = m_parameter2 - m_parameter1 < FLT_EPSILON ? k_diracMaximum : 1.0f/(m_parameter2-m_parameter1); + if (result <= 0.0f || std::isnan(result) || std::isinf(result)) { result = 1.0f; } return result*(1.0f+ k_displayTopMarginRatio); } float UniformLaw::evaluateAtAbscissa(float t) const { - if (m_parameter1 == m_parameter2) { + if (m_parameter2 - m_parameter1 < FLT_EPSILON) { if (m_parameter1 - k_diracWidth<= t && t <= m_parameter2 + k_diracWidth) { return 2.0f*k_diracMaximum; } diff --git a/apps/probability/law/uniform_law.h b/apps/probability/law/uniform_law.h index 33fe9c003..7d095fa8a 100644 --- a/apps/probability/law/uniform_law.h +++ b/apps/probability/law/uniform_law.h @@ -24,7 +24,7 @@ public: double cumulativeDistributiveInverseForProbability(double * probability) override; private: constexpr static float k_diracMaximum = 10.0f; - constexpr static float k_diracWidth = 0.001f; + constexpr static float k_diracWidth = 0.005f; }; }