From 54a10c2f4ffda0be3fb3e503119e6e3764dc35f0 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Sat, 19 Aug 2017 14:54:32 -0400 Subject: [PATCH] add math library functions sqrt, exp, log, sin, cos, tan, asin, acos, atan --- builtins.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/builtins.c b/builtins.c index 3cad822..8cbb156 100644 --- a/builtins.c +++ b/builtins.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "llt.h" #include "flisp.h" #include "random.h" @@ -433,6 +434,29 @@ static value_t fl_randf(value_t *args, u_int32_t nargs) return mk_float(rand_float()); } +#define MATH_FUNC_1ARG(name) \ +static value_t fl_##name(value_t *args, u_int32_t nargs) \ +{ \ + argcount(#name, nargs, 1); \ + if (iscprim(args[0])) { \ + cprim_t *cp = (cprim_t*)ptr(args[0]); \ + numerictype_t nt = cp_numtype(cp); \ + if (nt == T_FLOAT) \ + return mk_float(name##f(*(float*)cp_data(cp))); \ + } \ + return mk_double(name(todouble(args[0], #name))); \ +} + +MATH_FUNC_1ARG(sqrt) +MATH_FUNC_1ARG(exp) +MATH_FUNC_1ARG(log) +MATH_FUNC_1ARG(sin) +MATH_FUNC_1ARG(cos) +MATH_FUNC_1ARG(tan) +MATH_FUNC_1ARG(asin) +MATH_FUNC_1ARG(acos) +MATH_FUNC_1ARG(atan) + extern void stringfuncs_init(void); extern void table_init(void); extern void iostream_init(void); @@ -469,6 +493,16 @@ static builtinspec_t builtin_info[] = { { "rand.double", fl_randd }, { "rand.float", fl_randf }, + { "sqrt", fl_sqrt }, + { "exp", fl_exp }, + { "log", fl_log }, + { "sin", fl_sin }, + { "cos", fl_cos }, + { "tan", fl_tan }, + { "asin", fl_asin }, + { "acos", fl_acos }, + { "atan", fl_atan }, + { "path.cwd", fl_path_cwd }, { "path.exists?", fl_path_exists },