From 05cfb9bf2461785ec621b490747f48b96344017f Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 17 Aug 2024 12:35:43 +0100 Subject: sin, cos, more checks --- lib.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 5d5d2b3..9660003 100644 --- a/lib.c +++ b/lib.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -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 }, }; -- cgit v1.2.3