From: Torsten Dreyer Date: Mon, 2 May 2016 19:57:35 +0000 (+0200) Subject: Fix Nasal math.clamp() X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=ab9880d582417b964cb35889524ab16c108db469;p=simgear.git Fix Nasal math.clamp() Thanks to "Red Leader" from the forum for spotting --- diff --git a/simgear/nasal/mathlib.c b/simgear/nasal/mathlib.c index f2434b3c..564168c5 100644 --- a/simgear/nasal/mathlib.c +++ b/simgear/nasal/mathlib.c @@ -117,15 +117,14 @@ static naRef f_fmod(naContext c, naRef me, int argc, naRef* args) static naRef f_clamp(naContext c, naRef me, int argc, naRef* args) { naRef a = naNumValue(argc > 0 ? args[0] : naNil()); - naRef b = naNumValue(argc > 1 ? args[1] : naNil()); - naRef x = naNumValue(argc > 2 ? args[2] : naNil()); + naRef min = naNumValue(argc > 1 ? args[1] : naNil()); + naRef max = naNumValue(argc > 2 ? args[2] : naNil()); - if(naIsNil(a) || naIsNil(b) || naIsNil(x)) + if(naIsNil(a) || naIsNil(min) || naIsNil(max)) naRuntimeError(c, "non numeric arguments to clamp()"); - - if (a.num < b.num) b.num = a.num; - if (b.num > x.num) b.num = x.num; - return VALIDATE(b); + + a.num = a.num < min.num ? min.num : ( a.num > max.num ? max.num : a.num ); + return VALIDATE(a); } static naRef f_periodic(naContext c, naRef me, int argc, naRef* args)