diff options
author | ubq323 <ubq323@ubq323.website> | 2024-08-17 12:35:43 +0100 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2024-08-17 12:35:43 +0100 |
commit | 05cfb9bf2461785ec621b490747f48b96344017f (patch) | |
tree | 0b9fc13d8509b24c3950c668c9aa4cc28bcfa492 /lib.c | |
parent | 14b6304bb416795ca1016e9ce5f052b0861e5a48 (diff) |
sin, cos, more checks
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 38 |
1 files changed, 38 insertions, 0 deletions
@@ -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 }, }; |