diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-03-04 15:57:03 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-03-04 15:57:03 -0300 |
commit | 49f90ffdebf2ae10f9fe9579d90bcf2da692b7a6 (patch) | |
tree | ff35d11973a584e4f20a1f0d7e1842fcef88ddb6 /lmathlib.c | |
parent | dcabf721f85fae54954b7b3d18926ace21958964 (diff) | |
download | lua-49f90ffdebf2ae10f9fe9579d90bcf2da692b7a6.tar.gz lua-49f90ffdebf2ae10f9fe9579d90bcf2da692b7a6.tar.bz2 lua-49f90ffdebf2ae10f9fe9579d90bcf2da692b7a6.zip |
new functions (to complete math.h)
Diffstat (limited to 'lmathlib.c')
-rw-r--r-- | lmathlib.c | 31 |
1 files changed, 30 insertions, 1 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lmathlib.c,v 1.61 2004/05/10 18:11:32 roberto Exp roberto $ | 2 | ** $Id: lmathlib.c,v 1.62 2005/01/07 20:00:33 roberto Exp roberto $ |
3 | ** Standard mathematical library | 3 | ** Standard mathematical library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -33,16 +33,31 @@ static int math_sin (lua_State *L) { | |||
33 | return 1; | 33 | return 1; |
34 | } | 34 | } |
35 | 35 | ||
36 | static int math_sinh (lua_State *L) { | ||
37 | lua_pushnumber(L, sinh(luaL_checknumber(L, 1))); | ||
38 | return 1; | ||
39 | } | ||
40 | |||
36 | static int math_cos (lua_State *L) { | 41 | static int math_cos (lua_State *L) { |
37 | lua_pushnumber(L, cos(luaL_checknumber(L, 1))); | 42 | lua_pushnumber(L, cos(luaL_checknumber(L, 1))); |
38 | return 1; | 43 | return 1; |
39 | } | 44 | } |
40 | 45 | ||
46 | static int math_cosh (lua_State *L) { | ||
47 | lua_pushnumber(L, cosh(luaL_checknumber(L, 1))); | ||
48 | return 1; | ||
49 | } | ||
50 | |||
41 | static int math_tan (lua_State *L) { | 51 | static int math_tan (lua_State *L) { |
42 | lua_pushnumber(L, tan(luaL_checknumber(L, 1))); | 52 | lua_pushnumber(L, tan(luaL_checknumber(L, 1))); |
43 | return 1; | 53 | return 1; |
44 | } | 54 | } |
45 | 55 | ||
56 | static int math_tanh (lua_State *L) { | ||
57 | lua_pushnumber(L, tanh(luaL_checknumber(L, 1))); | ||
58 | return 1; | ||
59 | } | ||
60 | |||
46 | static int math_asin (lua_State *L) { | 61 | static int math_asin (lua_State *L) { |
47 | lua_pushnumber(L, asin(luaL_checknumber(L, 1))); | 62 | lua_pushnumber(L, asin(luaL_checknumber(L, 1))); |
48 | return 1; | 63 | return 1; |
@@ -78,6 +93,14 @@ static int math_mod (lua_State *L) { | |||
78 | return 1; | 93 | return 1; |
79 | } | 94 | } |
80 | 95 | ||
96 | static int math_modf (lua_State *L) { | ||
97 | double ip; | ||
98 | double fp = modf(luaL_checknumber(L, 1), &ip); | ||
99 | lua_pushnumber(L, ip); | ||
100 | lua_pushnumber(L, fp); | ||
101 | return 2; | ||
102 | } | ||
103 | |||
81 | static int math_sqrt (lua_State *L) { | 104 | static int math_sqrt (lua_State *L) { |
82 | lua_pushnumber(L, sqrt(luaL_checknumber(L, 1))); | 105 | lua_pushnumber(L, sqrt(luaL_checknumber(L, 1))); |
83 | return 1; | 106 | return 1; |
@@ -192,8 +215,11 @@ static int math_randomseed (lua_State *L) { | |||
192 | static const luaL_reg mathlib[] = { | 215 | static const luaL_reg mathlib[] = { |
193 | {"abs", math_abs}, | 216 | {"abs", math_abs}, |
194 | {"sin", math_sin}, | 217 | {"sin", math_sin}, |
218 | {"sinh", math_sinh}, | ||
195 | {"cos", math_cos}, | 219 | {"cos", math_cos}, |
220 | {"cosh", math_cosh}, | ||
196 | {"tan", math_tan}, | 221 | {"tan", math_tan}, |
222 | {"tanh", math_tanh}, | ||
197 | {"asin", math_asin}, | 223 | {"asin", math_asin}, |
198 | {"acos", math_acos}, | 224 | {"acos", math_acos}, |
199 | {"atan", math_atan}, | 225 | {"atan", math_atan}, |
@@ -201,6 +227,7 @@ static const luaL_reg mathlib[] = { | |||
201 | {"ceil", math_ceil}, | 227 | {"ceil", math_ceil}, |
202 | {"floor", math_floor}, | 228 | {"floor", math_floor}, |
203 | {"mod", math_mod}, | 229 | {"mod", math_mod}, |
230 | {"modf", math_modf}, | ||
204 | {"frexp", math_frexp}, | 231 | {"frexp", math_frexp}, |
205 | {"ldexp", math_ldexp}, | 232 | {"ldexp", math_ldexp}, |
206 | {"sqrt", math_sqrt}, | 233 | {"sqrt", math_sqrt}, |
@@ -225,6 +252,8 @@ LUALIB_API int luaopen_math (lua_State *L) { | |||
225 | luaL_openlib(L, LUA_MATHLIBNAME, mathlib, 0); | 252 | luaL_openlib(L, LUA_MATHLIBNAME, mathlib, 0); |
226 | lua_pushnumber(L, PI); | 253 | lua_pushnumber(L, PI); |
227 | lua_setfield(L, -2, "pi"); | 254 | lua_setfield(L, -2, "pi"); |
255 | lua_pushnumber(L, HUGE_VAL); | ||
256 | lua_setfield(L, -2, "huge"); | ||
228 | return 1; | 257 | return 1; |
229 | } | 258 | } |
230 | 259 | ||