]> git.mxchange.org Git - simgear.git/blobdiff - simgear/nasal/mathlib.c
Olaf Flebbe:
[simgear.git] / simgear / nasal / mathlib.c
index 07f8ef81dba63f056899fea113cde5930dc114bf..4982912d281851351531cdce3b2c1462e4cb5159 100644 (file)
@@ -1,57 +1,58 @@
+
 #include <math.h>
 #include <string.h>
 
 #include "nasal.h"
 
-static naRef f_sin(naContext c, naRef args)
+static naRef f_sin(naContext c, naRef me, int argc, naRef* args)
 {
-    naRef a = naNumValue(naVec_get(args, 0));
+    naRef a = naNumValue(argc > 0 ? args[0] : naNil());
     if(naIsNil(a))
         naRuntimeError(c, "non numeric argument to sin()");
     a.num = sin(a.num);
     return a;
 }
 
-static naRef f_cos(naContext c, naRef args)
+static naRef f_cos(naContext c, naRef me, int argc, naRef* args)
 {
-    naRef a = naNumValue(naVec_get(args, 0));
+    naRef a = naNumValue(argc > 0 ? args[0] : naNil());
     if(naIsNil(a))
         naRuntimeError(c, "non numeric argument to cos()");
     a.num = cos(a.num);
     return a;
 }
 
-static naRef f_exp(naContext c, naRef args)
+static naRef f_exp(naContext c, naRef me, int argc, naRef* args)
 {
-    naRef a = naNumValue(naVec_get(args, 0));
+    naRef a = naNumValue(argc > 0 ? args[0] : naNil());
     if(naIsNil(a))
         naRuntimeError(c, "non numeric argument to exp()");
     a.num = exp(a.num);
     return a;
 }
 
-static naRef f_ln(naContext c, naRef args)
+static naRef f_ln(naContext c, naRef me, int argc, naRef* args)
 {
-    naRef a = naNumValue(naVec_get(args, 0));
+    naRef a = naNumValue(argc > 0 ? args[0] : naNil());
     if(naIsNil(a))
         naRuntimeError(c, "non numeric argument to ln()");
     a.num = log(a.num);
     return a;
 }
 
-static naRef f_sqrt(naContext c, naRef args)
+static naRef f_sqrt(naContext c, naRef me, int argc, naRef* args)
 {
-    naRef a = naNumValue(naVec_get(args, 0));
+    naRef a = naNumValue(argc > 0 ? args[0] : naNil());
     if(naIsNil(a))
         naRuntimeError(c, "non numeric argument to sqrt()");
     a.num = sqrt(a.num);
     return a;
 }
 
-static naRef f_atan2(naContext c, naRef args)
+static naRef f_atan2(naContext c, naRef me, int argc, naRef* args)
 {
-    naRef a = naNumValue(naVec_get(args, 0));
-    naRef b = naNumValue(naVec_get(args, 1));
+    naRef a = naNumValue(argc > 0 ? args[0] : naNil());
+    naRef b = naNumValue(argc > 1 ? args[1] : naNil());
     if(naIsNil(a) || naIsNil(b))
         naRuntimeError(c, "non numeric argument to atan2()");
     a.num = atan2(a.num, b.num);
@@ -78,12 +79,14 @@ naRef naMathLib(naContext c)
         naHash_set(namespace, name, naNewFunc(c, code));
     }
 
-    // Set up constants for math.pi and math.e
+    // Set up constants for math.pi and math.e.  Can't use M_PI or
+    // M_E, becuase those aren't technically part of the C standard.  Sigh.
     name = naStr_fromdata(naNewString(c), "pi", 2);
-    naHash_set(namespace, name, naNum(M_PI));
+    naHash_set(namespace, name, naNum(3.14159265358979323846));
 
     name = naStr_fromdata(naNewString(c), "e", 1);
-    naHash_set(namespace, name, naNum(M_E));
+    name = naInternSymbol(name);
+    naHash_set(namespace, name, naNum(2.7182818284590452354));
 
     return namespace;
 }