6 static naRef f_sin(naContext c, naRef args)
8 naRef a = naNumValue(naVec_get(args, 0));
10 naRuntimeError(c, "non numeric argument to sin()");
15 static naRef f_cos(naContext c, naRef args)
17 naRef a = naNumValue(naVec_get(args, 0));
19 naRuntimeError(c, "non numeric argument to cos()");
24 static naRef f_exp(naContext c, naRef args)
26 naRef a = naNumValue(naVec_get(args, 0));
28 naRuntimeError(c, "non numeric argument to exp()");
33 static naRef f_ln(naContext c, naRef args)
35 naRef a = naNumValue(naVec_get(args, 0));
37 naRuntimeError(c, "non numeric argument to ln()");
42 static naRef f_sqrt(naContext c, naRef args)
44 naRef a = naNumValue(naVec_get(args, 0));
46 naRuntimeError(c, "non numeric argument to sqrt()");
51 static naRef f_atan2(naContext c, naRef args)
53 naRef a = naNumValue(naVec_get(args, 0));
54 naRef b = naNumValue(naVec_get(args, 1));
55 if(naIsNil(a) || naIsNil(b))
56 naRuntimeError(c, "non numeric argument to atan2()");
57 a.num = atan2(a.num, b.num);
61 static struct func { char* name; naCFunction func; } funcs[] = {
70 naRef naMathLib(naContext c)
72 naRef name, namespace = naNewHash(c);
73 int i, n = sizeof(funcs)/sizeof(struct func);
75 naRef code = naNewCCode(c, funcs[i].func);
76 naRef name = naStr_fromdata(naNewString(c),
77 funcs[i].name, strlen(funcs[i].name));
78 naHash_set(namespace, name, naNewFunc(c, code));
81 // Set up constants for math.pi and math.e
82 name = naStr_fromdata(naNewString(c), "pi", 2);
83 naHash_set(namespace, name, naNum(M_PI));
85 name = naStr_fromdata(naNewString(c), "e", 1);
86 naHash_set(namespace, name, naNum(M_E));