diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-05-02 13:36:51 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-05-02 13:36:51 -0300 |
| commit | 6d5324f92f0a6e0bb19b2482c437652e3c2f82c0 (patch) | |
| tree | a41879e34eafe10a68ad90d93f60bd6c887eb1be | |
| parent | 4fe11ae232e37045feaaf08d72a5414fca463c1c (diff) | |
| download | lua-6d5324f92f0a6e0bb19b2482c437652e3c2f82c0.tar.gz lua-6d5324f92f0a6e0bb19b2482c437652e3c2f82c0.tar.bz2 lua-6d5324f92f0a6e0bb19b2482c437652e3c2f82c0.zip | |
'math.fmod' handles integers as integers
| -rw-r--r-- | lmathlib.c | 18 |
1 files changed, 14 insertions, 4 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lmathlib.c,v 1.97 2014/04/10 17:53:33 roberto Exp roberto $ | 2 | ** $Id: lmathlib.c,v 1.98 2014/04/17 16:09:40 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 | */ |
| @@ -117,8 +117,18 @@ static int math_ifloor (lua_State *L) { | |||
| 117 | } | 117 | } |
| 118 | 118 | ||
| 119 | static int math_fmod (lua_State *L) { | 119 | static int math_fmod (lua_State *L) { |
| 120 | lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), | 120 | if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) { |
| 121 | luaL_checknumber(L, 2))); | 121 | lua_Integer d = lua_tointeger(L, 2); |
| 122 | if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */ | ||
| 123 | luaL_argcheck(L, d != 0, 2, "zero"); | ||
| 124 | lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */ | ||
| 125 | } | ||
| 126 | else | ||
| 127 | lua_pushinteger(L, lua_tointeger(L, 1) % d); | ||
| 128 | } | ||
| 129 | else | ||
| 130 | lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), | ||
| 131 | luaL_checknumber(L, 2))); | ||
| 122 | return 1; | 132 | return 1; |
| 123 | } | 133 | } |
| 124 | 134 | ||
| @@ -132,7 +142,7 @@ static int math_modf (lua_State *L) { | |||
| 132 | /* integer part (rounds toward zero) */ | 142 | /* integer part (rounds toward zero) */ |
| 133 | lua_Number ip = (n < 0) ? -l_mathop(floor)(-n) : l_mathop(floor)(n); | 143 | lua_Number ip = (n < 0) ? -l_mathop(floor)(-n) : l_mathop(floor)(n); |
| 134 | lua_pushnumber(L, ip); | 144 | lua_pushnumber(L, ip); |
| 135 | /* fractionary part (test handles inf/-inf) */ | 145 | /* fractionary part (test needed for inf/-inf) */ |
| 136 | lua_pushnumber(L, (n == ip) ? 0.0 : (n - ip)); | 146 | lua_pushnumber(L, (n == ip) ? 0.0 : (n - ip)); |
| 137 | return 2; | 147 | return 2; |
| 138 | } | 148 | } |
