Commits

Laurens Rodriguez committed c482d78

kmathlib.c: porting changes from Lua 5.2.2

Comments (0)

Files changed (3)

bin/windows/test.kia

 // Given that PI can be estimated using the function:
 // PI = 4 * (1 – 1/3 + 1/5 – 1/7 + ...) with more terms giving greater accuracy, 
 // write a function that calculates PI to an accuracy of 5 decimal places.
-
 function pi(precision) {
     var a = 4
     var b
 
 print(pi(10e-5))
 print(2 * Math.asin(1))
+print(Global.Math.PI)
 
 //*///--------------------------------------------------------------------------

src/libraries/killa/killaconf.h

 /*   http://www.opensource.org/licenses/mit-license.php                       */
 /* -------------------------------------------------------------------------- */
 
-#ifndef KCONFIG_H_
-#define KCONFIG_H_
+#ifndef KILLACONFIG_H_
+#define KILLACONFIG_H_
 
 #include <limits.h>
 #include <stddef.h>
 
 
 /*
+@@ killa_mathop allows the addition of an 'l' or 'f' to all math operations
+*/
+#define killa_mathop(x)		(x)
+
+
+/*
 @@ killa_str2number converts a decimal numeric string to a number.
 @@ killa_strx2number converts an hexadecimal numeric string to a number.
 ** In C99, 'strtod' do both conversions. C89, however, has no function

src/libraries/killa/kmathlib.c

 
 
 #undef PI
-#define PI (3.14159265358979323846)
-#define RADIANS_PER_DEGREE (PI/180.0)
-
-
-/* macro 'l_tg' allows the addition of an 'l' or 'f' to all math operations */
-#if !defined(l_tg)
-#define l_tg(x)		(x)
-#endif
+#define PI	((killa_Number)(3.1415926535897932384626433832795))
+#define RADIANS_PER_DEGREE	((killa_Number)(PI/180.0))
 
 
 
 static int math_abs (killa_State *L) {
-  killa_pushnumber(L, l_tg(fabs)(killaL_checknumber(L, 1)));
+  killa_pushnumber(L, killa_mathop(fabs)(killaL_checknumber(L, 1)));
   return 1;
 }
 
 static int math_sin (killa_State *L) {
-  killa_pushnumber(L, l_tg(sin)(killaL_checknumber(L, 1)));
+  killa_pushnumber(L, killa_mathop(sin)(killaL_checknumber(L, 1)));
   return 1;
 }
 
 static int math_sinh (killa_State *L) {
-  killa_pushnumber(L, l_tg(sinh)(killaL_checknumber(L, 1)));
+  killa_pushnumber(L, killa_mathop(sinh)(killaL_checknumber(L, 1)));
   return 1;
 }
 
 static int math_cos (killa_State *L) {
-  killa_pushnumber(L, l_tg(cos)(killaL_checknumber(L, 1)));
+  killa_pushnumber(L, killa_mathop(cos)(killaL_checknumber(L, 1)));
   return 1;
 }
 
 static int math_cosh (killa_State *L) {
-  killa_pushnumber(L, l_tg(cosh)(killaL_checknumber(L, 1)));
+  killa_pushnumber(L, killa_mathop(cosh)(killaL_checknumber(L, 1)));
   return 1;
 }
 
 static int math_tan (killa_State *L) {
-  killa_pushnumber(L, l_tg(tan)(killaL_checknumber(L, 1)));
+  killa_pushnumber(L, killa_mathop(tan)(killaL_checknumber(L, 1)));
   return 1;
 }
 
 static int math_tanh (killa_State *L) {
-  killa_pushnumber(L, l_tg(tanh)(killaL_checknumber(L, 1)));
+  killa_pushnumber(L, killa_mathop(tanh)(killaL_checknumber(L, 1)));
   return 1;
 }
 
 static int math_asin (killa_State *L) {
-  killa_pushnumber(L, l_tg(asin)(killaL_checknumber(L, 1)));
+  killa_pushnumber(L, killa_mathop(asin)(killaL_checknumber(L, 1)));
   return 1;
 }
 
 static int math_acos (killa_State *L) {
-  killa_pushnumber(L, l_tg(acos)(killaL_checknumber(L, 1)));
+  killa_pushnumber(L, killa_mathop(acos)(killaL_checknumber(L, 1)));
   return 1;
 }
 
 static int math_atan (killa_State *L) {
-  killa_pushnumber(L, l_tg(atan)(killaL_checknumber(L, 1)));
+  killa_pushnumber(L, killa_mathop(atan)(killaL_checknumber(L, 1)));
   return 1;
 }
 
 static int math_atan2 (killa_State *L) {
-  killa_pushnumber(L, l_tg(atan2)(killaL_checknumber(L, 1),
+  killa_pushnumber(L, killa_mathop(atan2)(killaL_checknumber(L, 1),
                                 killaL_checknumber(L, 2)));
   return 1;
 }
 
 static int math_ceil (killa_State *L) {
-  killa_pushnumber(L, l_tg(ceil)(killaL_checknumber(L, 1)));
+  killa_pushnumber(L, killa_mathop(ceil)(killaL_checknumber(L, 1)));
   return 1;
 }
 
 static int math_floor (killa_State *L) {
-  killa_pushnumber(L, l_tg(floor)(killaL_checknumber(L, 1)));
+  killa_pushnumber(L, killa_mathop(floor)(killaL_checknumber(L, 1)));
   return 1;
 }
 
 static int math_fmod (killa_State *L) {
-  killa_pushnumber(L, l_tg(fmod)(killaL_checknumber(L, 1),
+  killa_pushnumber(L, killa_mathop(fmod)(killaL_checknumber(L, 1),
                                killaL_checknumber(L, 2)));
   return 1;
 }
 
 static int math_modf (killa_State *L) {
   killa_Number ip;
-  killa_Number fp = l_tg(modf)(killaL_checknumber(L, 1), &ip);
+  killa_Number fp = killa_mathop(modf)(killaL_checknumber(L, 1), &ip);
   killa_pushnumber(L, ip);
   killa_pushnumber(L, fp);
   return 2;
 }
 
 static int math_sqrt (killa_State *L) {
-  killa_pushnumber(L, l_tg(sqrt)(killaL_checknumber(L, 1)));
+  killa_pushnumber(L, killa_mathop(sqrt)(killaL_checknumber(L, 1)));
   return 1;
 }
 
 static int math_pow (killa_State *L) {
-  killa_pushnumber(L, l_tg(pow)(killaL_checknumber(L, 1),
-                              killaL_checknumber(L, 2)));
+  killa_Number x = killaL_checknumber(L, 1);
+  killa_Number y = killaL_checknumber(L, 2);
+  killa_pushnumber(L, killa_mathop(pow)(x, y));
   return 1;
 }
 
   killa_Number x = killaL_checknumber(L, 1);
   killa_Number res;
   if (killa_isnoneornull(L, 2))
-    res = l_tg(log)(x);
+    res = killa_mathop(log)(x);
   else {
     killa_Number base = killaL_checknumber(L, 2);
-    if (base == 10.0) res = l_tg(log10)(x);
-    else res = l_tg(log)(x)/l_tg(log)(base);
+    if (base == (killa_Number)10.0) res = killa_mathop(log10)(x);
+    else res = killa_mathop(log)(x)/killa_mathop(log)(base);
   }
   killa_pushnumber(L, res);
   return 1;
 }
 
 static int math_exp (killa_State *L) {
-  killa_pushnumber(L, l_tg(exp)(killaL_checknumber(L, 1)));
+  killa_pushnumber(L, killa_mathop(exp)(killaL_checknumber(L, 1)));
   return 1;
 }
 
 
 static int math_frexp (killa_State *L) {
   int e;
-  killa_pushnumber(L, l_tg(frexp)(killaL_checknumber(L, 1), &e));
+  killa_pushnumber(L, killa_mathop(frexp)(killaL_checknumber(L, 1), &e));
   killa_pushinteger(L, e);
   return 2;
 }
 
 static int math_ldexp (killa_State *L) {
-  killa_pushnumber(L, l_tg(ldexp)(killaL_checknumber(L, 1),
-                                killaL_checkint(L, 2)));
+  killa_Number x = killaL_checknumber(L, 1);
+  int ep = killaL_checkint(L, 2);
+  killa_pushnumber(L, killa_mathop(ldexp)(x, ep));
   return 1;
 }
 
     }
     case 1: {  /* only upper limit */
       killa_Number u = killaL_checknumber(L, 1);
-      killaL_argcheck(L, 1.0 <= u, 1, "interval is empty");
-      killa_pushnumber(L, l_tg(floor)(r*u) + 1.0);  /* int in [1, u] */
+      killaL_argcheck(L, (killa_Number)1.0 <= u, 1, "interval is empty");
+      killa_pushnumber(L, killa_mathop(floor)(r*u) + (killa_Number)(1.0));  /* [1, u] */
       break;
     }
     case 2: {  /* lower and upper limits */
       killa_Number l = killaL_checknumber(L, 1);
       killa_Number u = killaL_checknumber(L, 2);
       killaL_argcheck(L, l <= u, 2, "interval is empty");
-      killa_pushnumber(L, l_tg(floor)(r*(u-l+1)) + l);  /* int in [l, u] */
+      killa_pushnumber(L, killa_mathop(floor)(r*(u-l+1)) + l);  /* [l, u] */
       break;
     }
     default: return killaL_error(L, "wrong number of arguments");