From 8361217369825cd3ce730586d43f3994733b997e Mon Sep 17 00:00:00 2001 From: ruby0x1 Date: Thu, 3 Dec 2020 13:18:13 -0800 Subject: [PATCH] Num; add min, max and clamp --- doc/site/modules/core/num.markdown | 14 ++++++++++++++ src/vm/wren_core.c | 26 ++++++++++++++++++++++++++ test/core/number/clamp.wren | 7 +++++++ test/core/number/min_max.wren | 5 +++++ 4 files changed, 52 insertions(+) create mode 100644 test/core/number/clamp.wren create mode 100644 test/core/number/min_max.wren diff --git a/doc/site/modules/core/num.markdown b/doc/site/modules/core/num.markdown index 3639712b9..094a8ba03 100644 --- a/doc/site/modules/core/num.markdown +++ b/doc/site/modules/core/num.markdown @@ -116,6 +116,20 @@ The binary (base-2) logarithm of the number. Returns `nan` if the base is negati The exponential `e` (Euler’s number) raised to the number. This: `eⁿ`. +### **min**(other) + +Returns the minimum value when comparing this number and `other`. + +### **max**(other) + +Returns the maximum value when comparing this number and `other`. + +### **clamp**(min, max) + +Clamps a number into the range of `min` and `max`. If this number is less than min, +`min` is returned. If bigger than `max`, `max` is returned. Otherwise, the number +itself is returned. + ### **pow**(power) Raises this number (the base) to `power`. Returns `nan` if the base is negative. diff --git a/src/vm/wren_core.c b/src/vm/wren_core.c index 9de1bf56c..a467d8e29 100644 --- a/src/vm/wren_core.c +++ b/src/vm/wren_core.c @@ -735,6 +735,29 @@ DEF_PRIMITIVE(num_atan2) RETURN_NUM(atan2(AS_NUM(args[0]), AS_NUM(args[1]))); } +DEF_PRIMITIVE(num_min) +{ + double value = AS_NUM(args[0]); + double other = AS_NUM(args[1]); + RETURN_NUM(value <= other ? value : other); +} + +DEF_PRIMITIVE(num_max) +{ + double value = AS_NUM(args[0]); + double other = AS_NUM(args[1]); + RETURN_NUM(value > other ? value : other); +} + +DEF_PRIMITIVE(num_clamp) +{ + double value = AS_NUM(args[0]); + double min = AS_NUM(args[1]); + double max = AS_NUM(args[2]); + double result = (value < min) ? min : ((value > max) ? max : value); + RETURN_NUM(result); +} + DEF_PRIMITIVE(num_pow) { RETURN_NUM(pow(AS_NUM(args[0]), AS_NUM(args[1]))); @@ -1325,6 +1348,9 @@ void wrenInitializeCore(WrenVM* vm) PRIMITIVE(vm->numClass, "floor", num_floor); PRIMITIVE(vm->numClass, "-", num_negate); PRIMITIVE(vm->numClass, "round", num_round); + PRIMITIVE(vm->numClass, "min(_)", num_min); + PRIMITIVE(vm->numClass, "max(_)", num_max); + PRIMITIVE(vm->numClass, "clamp(_,_)", num_clamp); PRIMITIVE(vm->numClass, "sin", num_sin); PRIMITIVE(vm->numClass, "sqrt", num_sqrt); PRIMITIVE(vm->numClass, "tan", num_tan); diff --git a/test/core/number/clamp.wren b/test/core/number/clamp.wren new file mode 100644 index 000000000..73a3598df --- /dev/null +++ b/test/core/number/clamp.wren @@ -0,0 +1,7 @@ +var num = 4 + +System.print(num.clamp(0, 10)) // expect: 4 +System.print(num.clamp(0, 1)) // expect: 1 +System.print(2.clamp(0, 1)) // expect: 1 +System.print((-1).clamp(0, 1)) // expect: 0 +System.print((-1).clamp(-20, 0)) // expect: -1 diff --git a/test/core/number/min_max.wren b/test/core/number/min_max.wren new file mode 100644 index 000000000..6c565f33b --- /dev/null +++ b/test/core/number/min_max.wren @@ -0,0 +1,5 @@ +var num = 4 +var num2 = 6 + +System.print(num.max(num2)) // expect: 6 +System.print(num.min(num2)) // expect: 4 \ No newline at end of file