From a1ee9c7a2ebc5d450a7428c8f8d7ab6864cb9497 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 6 Jun 2017 04:24:34 +0200 Subject: spline: prevent returning absolute of negative value Fixes: #628 --- spline/spline.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/spline/spline.cpp b/spline/spline.cpp index 7ca8147c..1fb38014 100644 --- a/spline/spline.cpp +++ b/spline/spline.cpp @@ -130,6 +130,12 @@ DEFUN_WARN_UNUSED bool spline::get_last_value(QPointF& point) return activep; } +template +static T signum(T val) +{ + return (T(0) < val) - (val < T(0)); +} + float spline::get_value_internal(int x) { if (!validp) @@ -138,10 +144,10 @@ float spline::get_value_internal(int x) validp = true; } - float sign = x < 0 ? -1 : 1; + const float sign = signum(x); x = std::abs(x); - float ret = data[std::min(unsigned(x), unsigned(value_count)-1u)]; - return ret * sign; + const float ret = data[std::min(unsigned(x), unsigned(value_count)-1u)]; + return sign * std::fmax(0, ret); } void spline::add_lone_point() -- cgit v1.2.3