2 #define _USE_MATH_DEFINES
10 static naRef f_sin(naContext c, naRef me, int argc, naRef* args)
12 naRef a = naNumValue(argc > 0 ? args[0] : naNil());
14 naRuntimeError(c, "non numeric argument to sin()");
19 static naRef f_cos(naContext c, naRef me, int argc, naRef* args)
21 naRef a = naNumValue(argc > 0 ? args[0] : naNil());
23 naRuntimeError(c, "non numeric argument to cos()");
28 static naRef f_exp(naContext c, naRef me, int argc, naRef* args)
30 naRef a = naNumValue(argc > 0 ? args[0] : naNil());
32 naRuntimeError(c, "non numeric argument to exp()");
37 static naRef f_ln(naContext c, naRef me, int argc, naRef* args)
39 naRef a = naNumValue(argc > 0 ? args[0] : naNil());
41 naRuntimeError(c, "non numeric argument to ln()");
46 static naRef f_sqrt(naContext c, naRef me, int argc, naRef* args)
48 naRef a = naNumValue(argc > 0 ? args[0] : naNil());
50 naRuntimeError(c, "non numeric argument to sqrt()");
55 static naRef f_atan2(naContext c, naRef me, int argc, naRef* args)
57 naRef a = naNumValue(argc > 0 ? args[0] : naNil());
58 naRef b = naNumValue(argc > 1 ? args[1] : naNil());
59 if(naIsNil(a) || naIsNil(b))
60 naRuntimeError(c, "non numeric argument to atan2()");
61 a.num = atan2(a.num, b.num);
65 static struct func { char* name; naCFunction func; } funcs[] = {
74 naRef naMathLib(naContext c)
76 naRef name, namespace = naNewHash(c);
77 int i, n = sizeof(funcs)/sizeof(struct func);
79 naRef code = naNewCCode(c, funcs[i].func);
80 naRef name = naStr_fromdata(naNewString(c),
81 funcs[i].name, strlen(funcs[i].name));
82 naHash_set(namespace, name, naNewFunc(c, code));
85 // Set up constants for math.pi and math.e. Can't use M_PI or
86 // M_E, becuase those aren't technically part of the C standard. Sigh.
87 name = naStr_fromdata(naNewString(c), "pi", 2);
88 naHash_set(namespace, name, naNum(3.14159265358979323846));
90 name = naStr_fromdata(naNewString(c), "e", 1);
91 name = naInternSymbol(name);
92 naHash_set(namespace, name, naNum(2.7182818284590452354));