diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-03-17 14:55:39 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-03-17 14:55:39 -0300 |
| commit | 3ca739b418243544ecc1098e34c71f2378bad915 (patch) | |
| tree | d4414fa93d35161ddb6df067cbef72ca3a9fff6e | |
| parent | 9e613b85838f044327b3735c1ff66b98a1a3567e (diff) | |
| download | lua-3ca739b418243544ecc1098e34c71f2378bad915.tar.gz lua-3ca739b418243544ecc1098e34c71f2378bad915.tar.bz2 lua-3ca739b418243544ecc1098e34c71f2378bad915.zip | |
'math.random' uses lua_Number to manage its arguments (there is no
reason to lose range).
| -rw-r--r-- | lmathlib.c | 16 |
1 files changed, 8 insertions, 8 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lmathlib.c,v 1.71 2009/02/18 13:06:05 roberto Exp roberto $ | 2 | ** $Id: lmathlib.c,v 1.72 2009/02/18 13:17:10 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 | */ |
| @@ -201,16 +201,16 @@ static int math_random (lua_State *L) { | |||
| 201 | break; | 201 | break; |
| 202 | } | 202 | } |
| 203 | case 1: { /* only upper limit */ | 203 | case 1: { /* only upper limit */ |
| 204 | int u = luaL_checkint(L, 1); | 204 | lua_Number u = luaL_checknumber(L, 1); |
| 205 | luaL_argcheck(L, 1<=u, 1, "interval is empty"); | 205 | luaL_argcheck(L, 1.0 <= u, 1, "interval is empty"); |
| 206 | lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */ | 206 | lua_pushnumber(L, floor(r*u) + 1.0); /* int between 1 and `u' */ |
| 207 | break; | 207 | break; |
| 208 | } | 208 | } |
| 209 | case 2: { /* lower and upper limits */ | 209 | case 2: { /* lower and upper limits */ |
| 210 | int l = luaL_checkint(L, 1); | 210 | lua_Number l = luaL_checknumber(L, 1); |
| 211 | int u = luaL_checkint(L, 2); | 211 | lua_Number u = luaL_checknumber(L, 2); |
| 212 | luaL_argcheck(L, l<=u, 2, "interval is empty"); | 212 | luaL_argcheck(L, l <= u, 2, "interval is empty"); |
| 213 | lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */ | 213 | lua_pushnumber(L, floor(r*(u-l+1)) + l); /* int between `l' and `u' */ |
| 214 | break; | 214 | break; |
| 215 | } | 215 | } |
| 216 | default: return luaL_error(L, "wrong number of arguments"); | 216 | default: return luaL_error(L, "wrong number of arguments"); |
