summaryrefslogtreecommitdiff
path: root/lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib.c b/lib.c
index 5d5d2b3..9660003 100644
--- a/lib.c
+++ b/lib.c
@@ -1,3 +1,4 @@
+#include <math.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
@@ -115,6 +116,37 @@ static Val fn_ssplit(State *S, int nargs, Val *args) {
return VAL_OBJ(out);
}
+// math
+static Val fn_sin(State *S, int nargs, Val *args) {
+ CHECK(nargs == 1, "need exactly 1 arg for sin");
+ CHECK(IS_NUM(args[0]), "need num for sin");
+ double x = AS_NUM(args[0]);
+ return VAL_NUM(sin(x));
+}
+static Val fn_cos(State *S, int nargs, Val *args) {
+ CHECK(nargs == 1, "need exactly 1 arg for cos");
+ CHECK(IS_NUM(args[0]), "need num for cos");
+ double x = AS_NUM(args[0]);
+ return VAL_NUM(cos(x));
+}
+static Val fn_ceil(State *S, int nargs, Val *args) {
+ CHECK(nargs == 1, "need exactly 1 arg for ceil");
+ CHECK(IS_NUM(args[0]), "need num for ceil");
+ double x = AS_NUM(args[0]);
+ return VAL_NUM(ceil(x));
+}
+static Val fn_floor(State *S, int nargs, Val *args) {
+ CHECK(nargs == 1, "need exactly 1 arg for floor");
+ CHECK(IS_NUM(args[0]), "need num for floor");
+ double x = AS_NUM(args[0]);
+ return VAL_NUM(floor(x));
+}
+static Val fn_abs(State *S, int nargs, Val *args) {
+ CHECK(nargs == 1, "need exactly 1 arg for abs");
+ CHECK(IS_NUM(args[0]), "need num for abs");
+ double x = AS_NUM(args[0]);
+ return VAL_NUM(abs(x));
+}
typedef struct {
@@ -133,6 +165,12 @@ static BuiltinFunc builtin_funcs[] = {
{ ",", fn_pend },
{ "s,", fn_spend },
{ "ssplit", fn_ssplit },
+
+ { "sin", fn_sin },
+ { "cos", fn_cos },
+ { "ceil", fn_ceil },
+ { "floor", fn_floor },
+ { "abs", fn_abs },
{ 0 },
};