7 static naRef f_sin(naContext c, naRef me, int argc, naRef* args)
9 naRef a = naNumValue(argc > 0 ? args[0] : naNil());
11 naRuntimeError(c, "non numeric argument to sin()");
16 static naRef f_cos(naContext c, naRef me, int argc, naRef* args)
18 naRef a = naNumValue(argc > 0 ? args[0] : naNil());
20 naRuntimeError(c, "non numeric argument to cos()");
25 static naRef f_exp(naContext c, naRef me, int argc, naRef* args)
27 naRef a = naNumValue(argc > 0 ? args[0] : naNil());
29 naRuntimeError(c, "non numeric argument to exp()");
34 static naRef f_ln(naContext c, naRef me, int argc, naRef* args)
36 naRef a = naNumValue(argc > 0 ? args[0] : naNil());
38 naRuntimeError(c, "non numeric argument to ln()");
43 static naRef f_sqrt(naContext c, naRef me, int argc, naRef* args)
45 naRef a = naNumValue(argc > 0 ? args[0] : naNil());
47 naRuntimeError(c, "non numeric argument to sqrt()");
52 static naRef f_atan2(naContext c, naRef me, int argc, naRef* args)
54 naRef a = naNumValue(argc > 0 ? args[0] : naNil());
55 naRef b = naNumValue(argc > 1 ? args[1] : naNil());
56 if(naIsNil(a) || naIsNil(b))
57 naRuntimeError(c, "non numeric argument to atan2()");
58 a.num = atan2(a.num, b.num);
62 static struct func { char* name; naCFunction func; } funcs[] = {
71 naRef naMathLib(naContext c)
73 naRef name, namespace = naNewHash(c);
74 int i, n = sizeof(funcs)/sizeof(struct func);
76 naRef code = naNewCCode(c, funcs[i].func);
77 naRef name = naStr_fromdata(naNewString(c),
78 funcs[i].name, strlen(funcs[i].name));
79 naHash_set(namespace, name, naNewFunc(c, code));
82 // Set up constants for math.pi and math.e. Can't use M_PI or
83 // M_E, becuase those aren't technically part of the C standard. Sigh.
84 name = naStr_fromdata(naNewString(c), "pi", 2);
85 naHash_set(namespace, name, naNum(3.14159265358979323846));
87 name = naStr_fromdata(naNewString(c), "e", 1);
88 name = naInternSymbol(name);
89 naHash_set(namespace, name, naNum(2.7182818284590452354));