diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-07 17:54:38 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-07 17:54:38 -0300 |
commit | 00a40f4d6a94216f7e8d40d69ab5402292d5120d (patch) | |
tree | 26ac769b2823feb829290d511188795cffcde65a | |
parent | 3bb5079dd485a5abeb79b4597636b6e5a167dc58 (diff) | |
download | lua-00a40f4d6a94216f7e8d40d69ab5402292d5120d.tar.gz lua-00a40f4d6a94216f7e8d40d69ab5402292d5120d.tar.bz2 lua-00a40f4d6a94216f7e8d40d69ab5402292d5120d.zip |
ensure proper rounding
-rw-r--r-- | lmathlib.c | 6 |
1 files changed, 3 insertions, 3 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lmathlib.c,v 1.47 2002/06/18 15:16:18 roberto Exp roberto $ | 2 | ** $Id: lmathlib.c,v 1.48 2002/06/24 13:54:13 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 | */ |
@@ -177,14 +177,14 @@ static int math_random (lua_State *L) { | |||
177 | case 1: { /* only upper limit */ | 177 | case 1: { /* only upper limit */ |
178 | int u = luaL_check_int(L, 1); | 178 | int u = luaL_check_int(L, 1); |
179 | luaL_arg_check(L, 1<=u, 1, "interval is empty"); | 179 | luaL_arg_check(L, 1<=u, 1, "interval is empty"); |
180 | lua_pushnumber(L, (int)(r*u)+1); /* integer between 1 and `u' */ | 180 | lua_pushnumber(L, (int)floor(r*u)+1); /* int between 1 and `u' */ |
181 | break; | 181 | break; |
182 | } | 182 | } |
183 | case 2: { /* lower and upper limits */ | 183 | case 2: { /* lower and upper limits */ |
184 | int l = luaL_check_int(L, 1); | 184 | int l = luaL_check_int(L, 1); |
185 | int u = luaL_check_int(L, 2); | 185 | int u = luaL_check_int(L, 2); |
186 | luaL_arg_check(L, l<=u, 2, "interval is empty"); | 186 | luaL_arg_check(L, l<=u, 2, "interval is empty"); |
187 | lua_pushnumber(L, (int)(r*(u-l+1))+l); /* integer between `l' and `u' */ | 187 | lua_pushnumber(L, (int)floor(r*(u-l+1))+l); /* int between `l' and `u' */ |
188 | break; | 188 | break; |
189 | } | 189 | } |
190 | default: return luaL_error(L, "wrong number of arguments"); | 190 | default: return luaL_error(L, "wrong number of arguments"); |