diff options
| author | Roberto I <roberto@inf.puc-rio.br> | 2025-08-09 15:15:20 -0300 |
|---|---|---|
| committer | Roberto I <roberto@inf.puc-rio.br> | 2025-08-09 15:15:20 -0300 |
| commit | 53dc5a3bbadac166a8b40904790f91b351e55dd9 (patch) | |
| tree | e3bab3f52b3db44ade2a21877e874815d126919e /lmathlib.c | |
| parent | 5b179eaf6a78af5f000d76147af94669d04487b2 (diff) | |
| download | lua-53dc5a3bbadac166a8b40904790f91b351e55dd9.tar.gz lua-53dc5a3bbadac166a8b40904790f91b351e55dd9.tar.bz2 lua-53dc5a3bbadac166a8b40904790f91b351e55dd9.zip | |
Functions 'frexp'-'ldexp' back to the math library
They are basic for anything that handles the representation of
floating numbers.
Diffstat (limited to 'lmathlib.c')
| -rw-r--r-- | lmathlib.c | 32 |
1 files changed, 16 insertions, 16 deletions
| @@ -203,6 +203,20 @@ static int math_rad (lua_State *L) { | |||
| 203 | return 1; | 203 | return 1; |
| 204 | } | 204 | } |
| 205 | 205 | ||
| 206 | static int math_frexp (lua_State *L) { | ||
| 207 | int e; | ||
| 208 | lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); | ||
| 209 | lua_pushinteger(L, e); | ||
| 210 | return 2; | ||
| 211 | } | ||
| 212 | |||
| 213 | static int math_ldexp (lua_State *L) { | ||
| 214 | lua_Number x = luaL_checknumber(L, 1); | ||
| 215 | int ep = (int)luaL_checkinteger(L, 2); | ||
| 216 | lua_pushnumber(L, l_mathop(ldexp)(x, ep)); | ||
| 217 | return 1; | ||
| 218 | } | ||
| 219 | |||
| 206 | 220 | ||
| 207 | static int math_min (lua_State *L) { | 221 | static int math_min (lua_State *L) { |
| 208 | int n = lua_gettop(L); /* number of arguments */ | 222 | int n = lua_gettop(L); /* number of arguments */ |
| @@ -666,20 +680,6 @@ static int math_pow (lua_State *L) { | |||
| 666 | return 1; | 680 | return 1; |
| 667 | } | 681 | } |
| 668 | 682 | ||
| 669 | static int math_frexp (lua_State *L) { | ||
| 670 | int e; | ||
| 671 | lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); | ||
| 672 | lua_pushinteger(L, e); | ||
| 673 | return 2; | ||
| 674 | } | ||
| 675 | |||
| 676 | static int math_ldexp (lua_State *L) { | ||
| 677 | lua_Number x = luaL_checknumber(L, 1); | ||
| 678 | int ep = (int)luaL_checkinteger(L, 2); | ||
| 679 | lua_pushnumber(L, l_mathop(ldexp)(x, ep)); | ||
| 680 | return 1; | ||
| 681 | } | ||
| 682 | |||
| 683 | static int math_log10 (lua_State *L) { | 683 | static int math_log10 (lua_State *L) { |
| 684 | lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); | 684 | lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); |
| 685 | return 1; | 685 | return 1; |
| @@ -702,7 +702,9 @@ static const luaL_Reg mathlib[] = { | |||
| 702 | {"tointeger", math_toint}, | 702 | {"tointeger", math_toint}, |
| 703 | {"floor", math_floor}, | 703 | {"floor", math_floor}, |
| 704 | {"fmod", math_fmod}, | 704 | {"fmod", math_fmod}, |
| 705 | {"frexp", math_frexp}, | ||
| 705 | {"ult", math_ult}, | 706 | {"ult", math_ult}, |
| 707 | {"ldexp", math_ldexp}, | ||
| 706 | {"log", math_log}, | 708 | {"log", math_log}, |
| 707 | {"max", math_max}, | 709 | {"max", math_max}, |
| 708 | {"min", math_min}, | 710 | {"min", math_min}, |
| @@ -718,8 +720,6 @@ static const luaL_Reg mathlib[] = { | |||
| 718 | {"sinh", math_sinh}, | 720 | {"sinh", math_sinh}, |
| 719 | {"tanh", math_tanh}, | 721 | {"tanh", math_tanh}, |
| 720 | {"pow", math_pow}, | 722 | {"pow", math_pow}, |
| 721 | {"frexp", math_frexp}, | ||
| 722 | {"ldexp", math_ldexp}, | ||
| 723 | {"log10", math_log10}, | 723 | {"log10", math_log10}, |
| 724 | #endif | 724 | #endif |
| 725 | /* placeholders */ | 725 | /* placeholders */ |
