aboutsummaryrefslogtreecommitdiff
path: root/mathlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1994-11-17 17:43:34 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1994-11-17 17:43:34 -0200
commita84aa11f71be730d554aa208d2b40ad28f2c9e05 (patch)
tree1d8a0b99fc8df1f87379e5b2f00d558ac39b9d87 /mathlib.c
parent9bee23fd0550e33b2a3f9c8d1b53506b59407e5c (diff)
downloadlua-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.c27
1 files changed, 19 insertions, 8 deletions
diff --git a/mathlib.c b/mathlib.c
index 8c71eabb..f0e84b98 100644
--- a/mathlib.c
+++ b/mathlib.c
@@ -3,7 +3,7 @@
3** Mathematics library to LUA 3** Mathematics library to LUA
4*/ 4*/
5 5
6char *rcs_mathlib="$Id: mathlib.c,v 1.3 1994/08/15 14:13:44 celes Exp celes $"; 6char *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
163static int old_pow;
164
163static void math_pow (void) 165static 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
175static void math_min (void) 186static 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}