From 3834fcc36e4d583bca02ac2ff79f22197ef3558a Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 19 Aug 2016 11:59:14 +0100 Subject: [PATCH] Add a math.clamp function. --- Nasal/math.nas | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Nasal/math.nas b/Nasal/math.nas index 94f79f343..098c993cd 100644 --- a/Nasal/math.nas +++ b/Nasal/math.nas @@ -27,6 +27,12 @@ var avg = func { return x; } +# this follows std::clamp for argument order, as opposed to +# qBound which uses (min, value, max) +var clamp = func(value, min, max) { + return (value < min) ? min : (value > max) ? max : value; +} + # note - mathlib defines an fmod function (added after this was written) # It uses C-library fmod(), which has different rounding behaviour to # this code (eg, fmod(-5, 4) gives -1, whereas this code gives 3)