diff options
| -rw-r--r-- | lmathlib.c | 32 | ||||
| -rw-r--r-- | manual/manual.of | 19 | ||||
| -rw-r--r-- | testes/math.lua | 12 |
3 files changed, 46 insertions, 17 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 */ |
diff --git a/manual/manual.of b/manual/manual.of index 61dd42f2..89e9b8f4 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
| @@ -8341,6 +8341,17 @@ that rounds the quotient towards zero. (integer/float) | |||
| 8341 | 8341 | ||
| 8342 | } | 8342 | } |
| 8343 | 8343 | ||
| 8344 | @LibEntry{math.frexp (x)| | ||
| 8345 | |||
| 8346 | Returns two numbers @id{m} and @id{e} such that @M{x = m2@sp{e}}, | ||
| 8347 | where @id{e} is an integer. | ||
| 8348 | When @id{x} is zero, NaN, +inf, or -inf, | ||
| 8349 | @id{m} is equal to @id{x}; | ||
| 8350 | otherwise, the absolute value of @id{m} | ||
| 8351 | is in the range @C{(} @M{[0.5, 1)} @C{]}. | ||
| 8352 | |||
| 8353 | } | ||
| 8354 | |||
| 8344 | @LibEntry{math.huge| | 8355 | @LibEntry{math.huge| |
| 8345 | 8356 | ||
| 8346 | The float value @idx{HUGE_VAL}, | 8357 | The float value @idx{HUGE_VAL}, |
| @@ -8348,6 +8359,12 @@ a value greater than any other numeric value. | |||
| 8348 | 8359 | ||
| 8349 | } | 8360 | } |
| 8350 | 8361 | ||
| 8362 | @LibEntry{math.ldexp(m, e)| | ||
| 8363 | |||
| 8364 | Returns @M{m2@sp{e}}, where @id{e} is an integer. | ||
| 8365 | |||
| 8366 | } | ||
| 8367 | |||
| 8351 | @LibEntry{math.log (x [, base])| | 8368 | @LibEntry{math.log (x [, base])| |
| 8352 | 8369 | ||
| 8353 | Returns the logarithm of @id{x} in the given base. | 8370 | Returns the logarithm of @id{x} in the given base. |
| @@ -8403,7 +8420,7 @@ Converts the angle @id{x} from degrees to radians. | |||
| 8403 | 8420 | ||
| 8404 | When called without arguments, | 8421 | When called without arguments, |
| 8405 | returns a pseudo-random float with uniform distribution | 8422 | returns a pseudo-random float with uniform distribution |
| 8406 | in the range @C{(} @M{[0,1)}. @C{]} | 8423 | in the range @C{(} @M{[0, 1)}. @C{]} |
| 8407 | When called with two integers @id{m} and @id{n}, | 8424 | When called with two integers @id{m} and @id{n}, |
| 8408 | @id{math.random} returns a pseudo-random integer | 8425 | @id{math.random} returns a pseudo-random integer |
| 8409 | with uniform distribution in the range @M{[m, n]}. | 8426 | with uniform distribution in the range @M{[m, n]}. |
diff --git a/testes/math.lua b/testes/math.lua index 0d228d09..54d19c40 100644 --- a/testes/math.lua +++ b/testes/math.lua | |||
| @@ -685,6 +685,18 @@ assert(eq(math.exp(0), 1)) | |||
| 685 | assert(eq(math.sin(10), math.sin(10%(2*math.pi)))) | 685 | assert(eq(math.sin(10), math.sin(10%(2*math.pi)))) |
| 686 | 686 | ||
| 687 | 687 | ||
| 688 | do print("testing ldexp/frexp") | ||
| 689 | global ipairs | ||
| 690 | for _, x in ipairs{0, 10, 32, -math.pi, 1e10, 1e-10, math.huge, -math.huge} do | ||
| 691 | local m, p = math.frexp(x) | ||
| 692 | assert(math.ldexp(m, p) == x) | ||
| 693 | local am = math.abs(m) | ||
| 694 | assert(m == x or (0.5 <= am and am < 1)) | ||
| 695 | end | ||
| 696 | |||
| 697 | end | ||
| 698 | |||
| 699 | |||
| 688 | assert(tonumber(' 1.3e-2 ') == 1.3e-2) | 700 | assert(tonumber(' 1.3e-2 ') == 1.3e-2) |
| 689 | assert(tonumber(' -1.00000000000001 ') == -1.00000000000001) | 701 | assert(tonumber(' -1.00000000000001 ') == -1.00000000000001) |
| 690 | 702 | ||
