root/lib/lua/limathlib.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. intToFloat
  2. floatToInt
  3. imath_muldiv
  4. imath_mul
  5. imath_div
  6. imath_sinr
  7. imath_cosr
  8. imath_tanr
  9. imath_recr
  10. imath_asinr
  11. imath_acosr
  12. imath_atanr
  13. imath_polr
  14. imath_sind
  15. imath_cosd
  16. imath_tand
  17. imath_recd
  18. imath_asind
  19. imath_acosd
  20. imath_atand
  21. imath_pold
  22. imath_deg
  23. imath_rad
  24. imath_log
  25. imath_log2
  26. imath_log10
  27. imath_pow
  28. imath_sqrt
  29. imath_int
  30. imath_frac
  31. imath_ceil
  32. imath_floor
  33. imath_round
  34. luaopen_imath

   1 /*
   2 limathlib.c,v 1.1
   3 integer based trigonometric mathematics
   4 all values scaled by 1000 (imath.scale)
   5 
   6 (c)2012 rudi from CHDK[-DE] forum
   7 License GPL 2.0
   8 */
   9 
  10 #include "stdlib.h"
  11 #include "math.h"
  12 
  13 #define limathlib_c
  14 #define LUA_LIB
  15 
  16 #include "lua.h"
  17 
  18 #include "lauxlib.h"
  19 #include "lualib.h"
  20 #include "cordic_math.h"
  21 
  22 LUALIB_API double intToFloat(int val) {
  23     return FLOAT(INT2FIXED(val));
  24 }
  25 
  26 LUALIB_API int floatToInt(double val) {
  27     return FIXED2INTR(FIXED(val));
  28 }
  29 
  30 static int imath_muldiv (lua_State *L) {
  31     int c = luaL_checknumber(L, 3);
  32     if (c == 0) luaL_error(L, "divide by 0");
  33     lua_pushnumber(L, muldivScaled(luaL_checknumber(L, 1),
  34         luaL_checknumber(L, 2), c));
  35     return 1;
  36 }
  37 
  38 static int imath_mul (lua_State *L) {
  39     lua_pushnumber(L, muldivScaled(luaL_checknumber(L, 1),
  40         luaL_checknumber(L, 2), INT_SCALE));
  41     return 1;
  42 }
  43 
  44 static int imath_div (lua_State *L) {
  45     int c = luaL_checknumber(L, 2);
  46     if (c == 0) luaL_error(L, "divide by 0");
  47     lua_pushnumber(L, muldivScaled(INT_SCALE, luaL_checknumber(L, 1), c));
  48     return 1;
  49 }
  50 
  51 // RAD
  52 static int imath_sinr (lua_State *L) {
  53     lua_pushnumber(L, FIXED2INTR(sinr(INT2FIXED(luaL_checknumber(L, 1)))));
  54     return 1;
  55 }
  56 
  57 static int imath_cosr (lua_State *L) {
  58     lua_pushnumber(L, FIXED2INTR(cosr(INT2FIXED(luaL_checknumber(L, 1)))));
  59     return 1;
  60 }
  61 
  62 static int imath_tanr (lua_State *L) {
  63     lua_pushnumber(L, FIXED2INTR(tanr(INT2FIXED(luaL_checknumber(L, 1)))));
  64     return 1;
  65 }
  66 
  67 static int imath_recr (lua_State *L) {
  68     fixed px, py;
  69     recr(INT2FIXED(luaL_checknumber(L, 1)),
  70         INT2FIXED(luaL_checknumber(L, 2)), &px, &py);
  71     lua_pushnumber(L, FIXED2INTR(px));
  72     lua_pushnumber(L, FIXED2INTR(py));
  73     return 2;
  74 }
  75 
  76 static int imath_asinr (lua_State *L) {
  77     lua_pushnumber(L, FIXED2INTR(asinr(INT2FIXED(luaL_checknumber(L, 1)))));
  78     return 1;
  79 }
  80 
  81 static int imath_acosr (lua_State *L) {
  82     lua_pushnumber(L, FIXED2INTR(acosr(INT2FIXED(luaL_checknumber(L, 1)))));
  83     return 1;
  84 }
  85 
  86 static int imath_atanr (lua_State *L) {
  87     lua_pushnumber(L, FIXED2INTR(atanr(INT2FIXED(luaL_checknumber(L, 1)))));
  88     return 1;
  89 }
  90 
  91 static int imath_polr (lua_State *L) {
  92     fixed r, theta;
  93     polr(INT2FIXED(luaL_checknumber(L, 1)),
  94         INT2FIXED(luaL_checknumber(L, 2)), &r, &theta);
  95     lua_pushnumber(L, FIXED2INTR(r));
  96     lua_pushnumber(L, FIXED2INTR(theta));
  97     return 2;
  98 }
  99 
 100 // DEG
 101 static int imath_sind (lua_State *L) {
 102     lua_pushnumber(L, FIXED2INTR(sind(INT2FIXED(luaL_checknumber(L, 1)))));
 103     return 1;
 104 }
 105 
 106 static int imath_cosd (lua_State *L) {
 107     lua_pushnumber(L, FIXED2INTR(cosd(INT2FIXED(luaL_checknumber(L, 1)))));
 108     return 1;
 109 }
 110 
 111 static int imath_tand (lua_State *L) {
 112     lua_pushnumber(L, FIXED2INTR(tand(INT2FIXED(luaL_checknumber(L, 1)))));
 113     return 1;
 114 }
 115 
 116 static int imath_recd (lua_State *L) {
 117     fixed px, py;
 118     recd(INT2FIXED(luaL_checknumber(L, 1)),
 119         INT2FIXED(luaL_checknumber(L, 2)), &px, &py);
 120     lua_pushnumber(L, FIXED2INTR(px));
 121     lua_pushnumber(L, FIXED2INTR(py));
 122     return 2;
 123 }
 124 
 125 static int imath_asind (lua_State *L) {
 126     lua_pushnumber(L, FIXED2INTR(asind(INT2FIXED(luaL_checknumber(L, 1)))));
 127     return 1;
 128 }
 129 
 130 static int imath_acosd (lua_State *L) {
 131     lua_pushnumber(L, FIXED2INTR(acosd(INT2FIXED(luaL_checknumber(L, 1)))));
 132     return 1;
 133 }
 134 
 135 static int imath_atand (lua_State *L) {
 136     lua_pushnumber(L, FIXED2INTR(atand(INT2FIXED(luaL_checknumber(L, 1)))));
 137     return 1;
 138 }
 139 
 140 static int imath_pold (lua_State *L) {
 141     fixed r, theta;
 142     pold(INT2FIXED(luaL_checknumber(L, 1)),
 143         INT2FIXED(luaL_checknumber(L, 2)), &r, &theta);
 144     lua_pushnumber(L, FIXED2INTR(r));
 145     lua_pushnumber(L, FIXED2INTR(theta));
 146     return 2;
 147 }
 148 
 149 static int imath_deg (lua_State *L) {
 150     lua_pushnumber(L, floatToInt(intToFloat(luaL_checknumber(L, 1)) * 180 / M_PI));
 151     return 1;
 152 }
 153 
 154 static int imath_rad (lua_State *L) {
 155     lua_pushnumber(L, floatToInt(intToFloat(luaL_checknumber(L, 1)) * M_PI / 180));
 156     return 1;
 157 }
 158 
 159 static int imath_log (lua_State *L) {
 160     lua_pushnumber(L, floatToInt(log(intToFloat(luaL_checknumber(L, 1)))));
 161     return 1;
 162 }
 163 
 164 static int imath_log2 (lua_State *L) {
 165     lua_pushnumber(L, floatToInt(log2(intToFloat(luaL_checknumber(L, 1)))));
 166     return 1;
 167 }
 168 
 169 static int imath_log10 (lua_State *L) {
 170     lua_pushnumber(L, floatToInt(log10(intToFloat(luaL_checknumber(L, 1)))));
 171     return 1;
 172 }
 173 
 174 static int imath_pow (lua_State *L) {
 175     lua_pushnumber(L, floatToInt(pow(intToFloat(luaL_checknumber(L, 1)),
 176         intToFloat(luaL_checknumber(L, 2)))));
 177     return 1;
 178 }
 179 
 180 static int imath_sqrt (lua_State *L) {
 181     lua_pushnumber(L, floatToInt(sqrt(intToFloat(luaL_checknumber(L, 1)))));
 182     return 1;
 183 }
 184 
 185 //additional math
 186 static int imath_int (lua_State *L) {
 187     lua_pushnumber(L, FIXED2INT(fint(INT2FIXED(luaL_checknumber(L, 1)))));
 188     return 1;
 189 }
 190 
 191 static int imath_frac (lua_State *L) {
 192     lua_pushnumber(L, luaL_checknumber(L, 1) - FIXED2INT(fint(INT2FIXED(luaL_checknumber(L, 1)))));
 193     return 1;
 194 }
 195 
 196 static int imath_ceil (lua_State *L) {
 197     lua_pushnumber(L, FIXED2INT(fceil(INT2FIXED(luaL_checknumber(L, 1)))));
 198     return 1;
 199 }
 200 
 201 static int imath_floor (lua_State *L) {
 202     lua_pushnumber(L, FIXED2INT(ffloor(INT2FIXED(luaL_checknumber(L, 1)))));
 203     return 1;
 204 }
 205 
 206 static int imath_round (lua_State *L) {
 207     lua_pushnumber(L, FIXED2INT(fround(INT2FIXED(luaL_checknumber(L, 1)))));
 208     return 1;
 209 }
 210 
 211 static const luaL_Reg imathlib[] = {
 212     {"muldiv", imath_muldiv},
 213     {"mul",    imath_mul},
 214     {"div",    imath_div},
 215     {"sinr",   imath_sinr},
 216     {"cosr",   imath_cosr},
 217     {"tanr",   imath_tanr},
 218     {"recr",   imath_recr},
 219     {"asinr",  imath_asinr},
 220     {"acosr",  imath_acosr},
 221     {"atanr",  imath_atanr},
 222     {"polr",   imath_polr},
 223     {"sind",   imath_sind},
 224     {"cosd",   imath_cosd},
 225     {"tand",   imath_tand},
 226     {"recd",   imath_recd},
 227     {"asind",  imath_asind},
 228     {"acosd",  imath_acosd},
 229     {"atand",  imath_atand},
 230     {"pold",   imath_pold},
 231     {"deg",    imath_deg},
 232     {"rad",    imath_rad},
 233     {"log",    imath_log},
 234     {"log2",   imath_log2},
 235     {"log10",  imath_log10},
 236     {"pow",    imath_pow},
 237     {"sqrt",   imath_sqrt},
 238     {"int",    imath_int},
 239     {"frac",   imath_frac},
 240     {"ceil",   imath_ceil},
 241     {"floor",  imath_floor},
 242     {"round",  imath_round},
 243     {NULL, NULL}
 244 };
 245 
 246 
 247 /*
 248 ** Open imath library
 249 */
 250 LUALIB_API int luaopen_imath (lua_State *L) {
 251     luaL_register(L, LUA_IMATHLIBNAME, imathlib);
 252 
 253     lua_pushnumber(L, INT_SCALE);
 254     lua_setfield(L, -2, "scale");
 255     lua_pushnumber(L, FIXED2INTR(FULL_CIRCLE[RAD]));
 256     lua_setfield(L, -2, "pi2");
 257     lua_pushnumber(L, FIXED2INTR(HALF_CIRCLE[RAD]));
 258     lua_setfield(L, -2, "pi");
 259     lua_pushnumber(L, FIXED2INTR(QUART_CIRCLE[RAD]));
 260     lua_setfield(L, -2, "pi_2");
 261 
 262     return 1;
 263 }

/* [<][>][^][v][top][bottom][index][help] */