diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-17 17:43:34 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-17 17:43:34 -0200 |
commit | a84aa11f71be730d554aa208d2b40ad28f2c9e05 (patch) | |
tree | 1d8a0b99fc8df1f87379e5b2f00d558ac39b9d87 /mathlib.c | |
parent | 9bee23fd0550e33b2a3f9c8d1b53506b59407e5c (diff) | |
download | lua-a84aa11f71be730d554aa208d2b40ad28f2c9e05.tar.gz lua-a84aa11f71be730d554aa208d2b40ad28f2c9e05.tar.bz2 lua-a84aa11f71be730d554aa208d2b40ad28f2c9e05.zip |
pow operation is defined in mathlib.c
Diffstat (limited to 'mathlib.c')
-rw-r--r-- | mathlib.c | 27 |
1 files changed, 19 insertions, 8 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** Mathematics library to LUA | 3 | ** Mathematics library to LUA |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_mathlib="$Id: mathlib.c,v 1.3 1994/08/15 14:13:44 celes Exp celes $"; | 6 | char *rcs_mathlib="$Id: mathlib.c,v 1.4 1994/10/11 13:06:47 celes Exp roberto $"; |
7 | 7 | ||
8 | #include <stdio.h> /* NULL */ | 8 | #include <stdio.h> /* NULL */ |
9 | #include <math.h> | 9 | #include <math.h> |
@@ -160,16 +160,27 @@ static void math_sqrt (void) | |||
160 | lua_pushnumber (sqrt(d)); | 160 | lua_pushnumber (sqrt(d)); |
161 | } | 161 | } |
162 | 162 | ||
163 | static int old_pow; | ||
164 | |||
163 | static void math_pow (void) | 165 | static void math_pow (void) |
164 | { | 166 | { |
165 | double d1, d2; | ||
166 | lua_Object o1 = lua_getparam (1); | 167 | lua_Object o1 = lua_getparam (1); |
167 | lua_Object o2 = lua_getparam (2); | 168 | lua_Object o2 = lua_getparam (2); |
168 | if (!lua_isnumber(o1) || !lua_isnumber(o2)) | 169 | lua_Object op = lua_getparam(3); |
169 | { lua_error ("incorrect arguments to function `pow'"); return; } | 170 | if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p') |
170 | d1 = lua_getnumber(o1); | 171 | { |
171 | d2 = lua_getnumber(o2); | 172 | lua_pushobject(o1); |
172 | lua_pushnumber (pow(d1,d2)); | 173 | lua_pushobject(o2); |
174 | lua_pushobject(op); | ||
175 | if (lua_callfunction(lua_getlocked(old_pow)) != 0) | ||
176 | lua_error(NULL); | ||
177 | } | ||
178 | else | ||
179 | { | ||
180 | double d1 = lua_getnumber(o1); | ||
181 | double d2 = lua_getnumber(o2); | ||
182 | lua_pushnumber (pow(d1,d2)); | ||
183 | } | ||
173 | } | 184 | } |
174 | 185 | ||
175 | static void math_min (void) | 186 | static void math_min (void) |
@@ -292,7 +303,6 @@ void mathlib_open (void) | |||
292 | lua_register ("floor", math_floor); | 303 | lua_register ("floor", math_floor); |
293 | lua_register ("mod", math_mod); | 304 | lua_register ("mod", math_mod); |
294 | lua_register ("sqrt", math_sqrt); | 305 | lua_register ("sqrt", math_sqrt); |
295 | lua_register ("pow", math_pow); | ||
296 | lua_register ("min", math_min); | 306 | lua_register ("min", math_min); |
297 | lua_register ("max", math_max); | 307 | lua_register ("max", math_max); |
298 | lua_register ("log", math_log); | 308 | lua_register ("log", math_log); |
@@ -300,4 +310,5 @@ void mathlib_open (void) | |||
300 | lua_register ("exp", math_exp); | 310 | lua_register ("exp", math_exp); |
301 | lua_register ("deg", math_deg); | 311 | lua_register ("deg", math_deg); |
302 | lua_register ("rad", math_rad); | 312 | lua_register ("rad", math_rad); |
313 | old_pow = lua_lock(lua_setfallback("arith", math_pow)); | ||
303 | } | 314 | } |