diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2017-06-06 04:24:34 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2017-06-06 04:24:34 +0200 |
commit | a1ee9c7a2ebc5d450a7428c8f8d7ab6864cb9497 (patch) | |
tree | a5c4ed1c7c1141f916db2fbac1d726b7ebde8834 /spline | |
parent | 7025f2307afd9c794ff9b64868beb3cbbe8c713f (diff) |
spline: prevent returning absolute of negative value
Fixes: #628
Diffstat (limited to 'spline')
-rw-r--r-- | spline/spline.cpp | 12 |
1 files 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 <typename T> +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() |