From 97ac9f02f92f62770b63a8471ea004eaddd57a70 Mon Sep 17 00:00:00 2001 From: shabbyrobe Date: Tue, 15 Oct 2019 01:00:28 +1100 Subject: [PATCH] Replace best_diff when a better diff comes along --- src/main/cpp/tiv.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/cpp/tiv.cpp b/src/main/cpp/tiv.cpp index 2ac344b..167d84c 100644 --- a/src/main/cpp/tiv.cpp +++ b/src/main/cpp/tiv.cpp @@ -301,8 +301,10 @@ int best_index(int value, const int data[], int count) { int best_diff = std::abs(data[0] - value); int result = 0; for (int i = 1; i < count; i++) { - if (std::abs(data[i] - value) < best_diff) { + int diff = std::abs(data[i] - value); + if (diff < best_diff) { result = i; + best_diff = diff; } } return result;