From f5174e340e0eef0d91469dc9ad14d5e7d38891f4 Mon Sep 17 00:00:00 2001 From: Tom Poole Date: Tue, 27 Mar 2018 11:06:41 +0100 Subject: [PATCH] Added some assertions when the input to a NormalisableRange conversion function is outside of the expected range --- .../juce_core/maths/juce_NormalisableRange.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/modules/juce_core/maths/juce_NormalisableRange.h b/modules/juce_core/maths/juce_NormalisableRange.h index 4ba72f4db5..f4ff23ee5f 100644 --- a/modules/juce_core/maths/juce_NormalisableRange.h +++ b/modules/juce_core/maths/juce_NormalisableRange.h @@ -143,9 +143,9 @@ public: ValueType convertTo0to1 (ValueType v) const noexcept { if (convertTo0To1Function != nullptr) - return convertTo0To1Function (start, end, v); + return clampTo0To1 (convertTo0To1Function (start, end, v)); - auto proportion = (v - start) / (end - start); + auto proportion = clampTo0To1 ((v - start) / (end - start)); if (skew == static_cast (1)) return proportion; @@ -166,6 +166,8 @@ public: */ ValueType convertFrom0to1 (ValueType proportion) const noexcept { + proportion = clampTo0To1 (proportion); + if (convertFrom0To1Function != nullptr) return convertFrom0To1Function (start, end, proportion); @@ -261,6 +263,17 @@ private: jassert (skew > ValueType()); } + static ValueType clampTo0To1 (ValueType value) + { + auto clampedValue = jlimit (static_cast (0), static_cast (1), value); + + // If you his this assertion then either your normalisation function is not working + // correctly or your input is out of the expected bounds. + jassert (clampedValue == value); + + return clampedValue; + } + typedef std::function ConverstionFunction; ConverstionFunction convertFrom0To1Function = {}, convertTo0To1Function = {},