aboutsummaryrefslogtreecommitdiff
path: root/lmathlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-03-17 14:55:39 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-03-17 14:55:39 -0300
commit3ca739b418243544ecc1098e34c71f2378bad915 (patch)
treed4414fa93d35161ddb6df067cbef72ca3a9fff6e /lmathlib.c
parent9e613b85838f044327b3735c1ff66b98a1a3567e (diff)
downloadlua-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).
Diffstat (limited to 'lmathlib.c')
-rw-r--r--lmathlib.c16
1 files changed, 8 insertions, 8 deletions
diff --git a/lmathlib.c b/lmathlib.c
index 4999310a..eca93ecc 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -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");