diff options
-rw-r--r-- | lmathlib.c | 30 |
1 files changed, 23 insertions, 7 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: $ | 2 | ** $Id: lmathlib.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ |
3 | ** Lua standard mathematical library | 3 | ** Lua standard mathematical library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -16,9 +16,23 @@ | |||
16 | #define PI 3.14159265358979323846 | 16 | #define PI 3.14159265358979323846 |
17 | #endif | 17 | #endif |
18 | 18 | ||
19 | #define TODEGREE(a) ((a)*180.0/PI) | ||
20 | #define TORAD(a) ((a)*PI/180.0) | ||
21 | 19 | ||
20 | static double torad = PI/180.0; | ||
21 | |||
22 | #define FROMRAD(a) ((a)/torad) | ||
23 | #define TORAD(a) ((a)*torad) | ||
24 | |||
25 | |||
26 | static void modeang (void) | ||
27 | { | ||
28 | char *s = luaL_opt_string(1, "degree"); | ||
29 | switch (*s) { | ||
30 | case 'd' : torad = PI/180.0; break; | ||
31 | case 'r' : torad = 1.0; break; | ||
32 | case 'g' : torad = PI/50.0; break; | ||
33 | default: luaL_arg_check(0, 1, "invalid mode"); | ||
34 | } | ||
35 | } | ||
22 | 36 | ||
23 | 37 | ||
24 | static void math_abs (void) | 38 | static void math_abs (void) |
@@ -45,22 +59,22 @@ static void math_tan (void) | |||
45 | 59 | ||
46 | static void math_asin (void) | 60 | static void math_asin (void) |
47 | { | 61 | { |
48 | lua_pushnumber(asin(TODEGREE(luaL_check_number(1)))); | 62 | lua_pushnumber(FROMRAD(asin(luaL_check_number(1)))); |
49 | } | 63 | } |
50 | 64 | ||
51 | static void math_acos (void) | 65 | static void math_acos (void) |
52 | { | 66 | { |
53 | lua_pushnumber(acos(TODEGREE(luaL_check_number(1)))); | 67 | lua_pushnumber(FROMRAD(acos(luaL_check_number(1)))); |
54 | } | 68 | } |
55 | 69 | ||
56 | static void math_atan (void) | 70 | static void math_atan (void) |
57 | { | 71 | { |
58 | lua_pushnumber(atan(TODEGREE(luaL_check_number(1)))); | 72 | lua_pushnumber(FROMRAD(atan(luaL_check_number(1)))); |
59 | } | 73 | } |
60 | 74 | ||
61 | static void math_atan2 (void) | 75 | static void math_atan2 (void) |
62 | { | 76 | { |
63 | lua_pushnumber(TODEGREE(atan2(luaL_check_number(1), luaL_check_number(2)))); | 77 | lua_pushnumber(FROMRAD(atan2(luaL_check_number(1), luaL_check_number(2)))); |
64 | } | 78 | } |
65 | 79 | ||
66 | static void math_ceil (void) | 80 | static void math_ceil (void) |
@@ -159,6 +173,7 @@ static void math_randomseed (void) | |||
159 | 173 | ||
160 | 174 | ||
161 | static struct luaL_reg mathlib[] = { | 175 | static struct luaL_reg mathlib[] = { |
176 | {"modeang", modeang}, | ||
162 | {"abs", math_abs}, | 177 | {"abs", math_abs}, |
163 | {"sin", math_sin}, | 178 | {"sin", math_sin}, |
164 | {"cos", math_cos}, | 179 | {"cos", math_cos}, |
@@ -191,5 +206,6 @@ void lua_mathlibopen (void) | |||
191 | lua_pushcfunction(math_pow); | 206 | lua_pushcfunction(math_pow); |
192 | lua_pushnumber(0); /* to get its tag */ | 207 | lua_pushnumber(0); /* to get its tag */ |
193 | lua_settagmethod(lua_tag(lua_pop()), "pow"); | 208 | lua_settagmethod(lua_tag(lua_pop()), "pow"); |
209 | lua_pushnumber(PI); lua_setglobal("PI"); | ||
194 | } | 210 | } |
195 | 211 | ||