From c4397e38f1ba24993445867791ccd61b4bd1b0f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Nusser?= Date: Mon, 14 Oct 2019 21:33:03 +0200 Subject: Fix bug of infinite loop in sample selection when all powers were same. Division by zero was the issue. --- src/sample_selection.cc | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/sample_selection.cc b/src/sample_selection.cc index 2e08e9b..7267faf 100644 --- a/src/sample_selection.cc +++ b/src/sample_selection.cc @@ -75,7 +75,10 @@ const Sample* SampleSelection::get(level_t level, std::size_t pos) const float f_diverse = (1./2.)*settings.sample_selection_f_diverse.load(); const float f_random = (1./3.)*settings.sample_selection_f_random.load(); - float const power_range = powerlist.getMaxPower() - powerlist.getMinPower(); + float power_range = powerlist.getMaxPower() - powerlist.getMinPower(); + // If all power values are the same then power_range is invalid but we want + // to avoid division by zero. + if (power_range == 0.) { power_range = 1.0; } // start with most promising power value and then stop when reaching far values // which cannot become opt anymore @@ -83,9 +86,16 @@ const Sample* SampleSelection::get(level_t level, std::size_t pos) std::size_t up_index = std::distance(samples.begin(), closest_it); std::size_t down_index = (up_index == 0 ? 0 : up_index - 1); - auto const close_up = (samples[up_index].power-level)/power_range; + float up_value_lb; + if (up_index < samples.size()) { + auto const close_up = (samples[up_index].power-level)/power_range; + up_value_lb = f_close*pow2(close_up); + } + else { + --up_index; + up_value_lb = std::numeric_limits::max(); + } auto const close_down = (samples[down_index].power-level)/power_range; - float up_value_lb = (up_index < samples.size() ? f_close*pow2(close_up) : std::numeric_limits::max()); float down_value_lb = (up_index != 0 ? f_close*pow2(close_down) : std::numeric_limits::max()); std::size_t count = 0; -- cgit v1.2.3