2 #define _USE_MATH_DEFINES
10 static naRef f_sin(naContext c, naRef args)
12 naRef a = naNumValue(naVec_get(args, 0));
14 naRuntimeError(c, "non numeric argument to sin()");
19 static naRef f_cos(naContext c, naRef args)
21 naRef a = naNumValue(naVec_get(args, 0));
23 naRuntimeError(c, "non numeric argument to cos()");
28 static naRef f_exp(naContext c, naRef args)
30 naRef a = naNumValue(naVec_get(args, 0));
32 naRuntimeError(c, "non numeric argument to exp()");
37 static naRef f_ln(naContext c, naRef args)
39 naRef a = naNumValue(naVec_get(args, 0));
41 naRuntimeError(c, "non numeric argument to ln()");
46 static naRef f_sqrt(naContext c, naRef args)
48 naRef a = naNumValue(naVec_get(args, 0));
50 naRuntimeError(c, "non numeric argument to sqrt()");
55 static naRef f_atan2(naContext c, naRef args)
57 naRef a = naNumValue(naVec_get(args, 0));
58 naRef b = naNumValue(naVec_get(args, 1));
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
86 name = naStr_fromdata(naNewString(c), "pi", 2);
87 naHash_set(namespace, name, naNum(M_PI));
89 name = naStr_fromdata(naNewString(c), "e", 1);
90 naHash_set(namespace, name, naNum(M_E));