From 433fbc40195ddf1c837c1c56e14e15e5633e1bb3 Mon Sep 17 00:00:00 2001 From: underscorediscovery Date: Fri, 10 Jul 2020 19:38:45 -0700 Subject: [PATCH] core; num; add exp & log2 I've had a couple use cases in time that the code is significantly clearer with these, and makes porting less error prone --- doc/site/modules/core/num.markdown | 10 +++++++++- src/vm/wren_core.c | 4 ++++ test/core/number/exp.wren | 3 +++ test/core/number/log2.wren | 4 ++++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/core/number/exp.wren create mode 100644 test/core/number/log2.wren diff --git a/doc/site/modules/core/num.markdown b/doc/site/modules/core/num.markdown index 968a57534..d1f9fb832 100644 --- a/doc/site/modules/core/num.markdown +++ b/doc/site/modules/core/num.markdown @@ -96,7 +96,15 @@ Whether the number is [not a number](http://en.wikipedia.org/wiki/NaN). This is ### **log** -The natural logarithm of the number. +The natural logarithm of the number. Returns `nan` if the base is negative. + +### **log2** + +The binary (base-2) logarithm of the number. Returns `nan` if the base is negative. + +### **exp** + +The exponential `e` (Euler’s number) raised to the number. This: `eⁿ`. ### **pow**(power) diff --git a/src/vm/wren_core.c b/src/vm/wren_core.c index 4aca5bb0d..3ae159c18 100644 --- a/src/vm/wren_core.c +++ b/src/vm/wren_core.c @@ -652,6 +652,8 @@ DEF_NUM_FN(sin, sin) DEF_NUM_FN(sqrt, sqrt) DEF_NUM_FN(tan, tan) DEF_NUM_FN(log, log) +DEF_NUM_FN(log2, log2) +DEF_NUM_FN(exp, exp) DEF_PRIMITIVE(num_mod) { @@ -1299,6 +1301,8 @@ void wrenInitializeCore(WrenVM* vm) PRIMITIVE(vm->numClass, "sqrt", num_sqrt); PRIMITIVE(vm->numClass, "tan", num_tan); PRIMITIVE(vm->numClass, "log", num_log); + PRIMITIVE(vm->numClass, "log2", num_log2); + PRIMITIVE(vm->numClass, "exp", num_exp); PRIMITIVE(vm->numClass, "%(_)", num_mod); PRIMITIVE(vm->numClass, "~", num_bitwiseNot); PRIMITIVE(vm->numClass, "..(_)", num_dotDot); diff --git a/test/core/number/exp.wren b/test/core/number/exp.wren new file mode 100644 index 000000000..08dde70bd --- /dev/null +++ b/test/core/number/exp.wren @@ -0,0 +1,3 @@ +System.print(5.exp) // expect: 148.41315910258 +System.print(10.exp) // expect: 22026.465794807 +System.print((-1).exp) // expect: 0.36787944117144 diff --git a/test/core/number/log2.wren b/test/core/number/log2.wren new file mode 100644 index 000000000..2fd6396ce --- /dev/null +++ b/test/core/number/log2.wren @@ -0,0 +1,4 @@ +System.print(1024.log2) // expect: 10 +System.print(2048.log2) // expect: 11 +System.print(100.log2) // expect: 6.6438561897747 +System.print((-1).log2) // expect: nan