aboutsummaryrefslogtreecommitdiff
path: root/lmathlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-07-17 09:30:53 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-07-17 09:30:53 -0300
commit8bb272a3e3d0693a1d587cfa3469153978ae617f (patch)
treec3a87e37c1921fcd27433036b84e500e0dc6735f /lmathlib.c
parentc229ed597f939eacfe1e9b7113e2a082fe93a3ae (diff)
downloadlua-8bb272a3e3d0693a1d587cfa3469153978ae617f.tar.gz
lua-8bb272a3e3d0693a1d587cfa3469153978ae617f.tar.bz2
lua-8bb272a3e3d0693a1d587cfa3469153978ae617f.zip
new conversion float->integer: conversion is valid only when
float has an exact representation as an integer
Diffstat (limited to 'lmathlib.c')
-rw-r--r--lmathlib.c41
1 files changed, 21 insertions, 20 deletions
diff --git a/lmathlib.c b/lmathlib.c
index 61800799..79232e4d 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmathlib.c,v 1.105 2014/06/30 19:48:08 roberto Exp roberto $ 2** $Id: lmathlib.c,v 1.106 2014/07/16 13:47: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*/
@@ -76,39 +76,39 @@ static int math_atan (lua_State *L) {
76} 76}
77 77
78 78
79static int math_ifloor (lua_State *L) { 79static int math_toint (lua_State *L) {
80 int valid; 80 int valid;
81 lua_Integer n = lua_tointegerx(L, 1, &valid); 81 lua_Integer n = lua_tointegerx(L, 1, &valid);
82 if (valid) 82 if (valid)
83 lua_pushinteger(L, n); /* floor computed by Lua */ 83 lua_pushinteger(L, n);
84 else { 84 else {
85 luaL_checktype(L, 1, LUA_TNUMBER); /* argument must be a number */ 85 luaL_checkany(L, 1);
86 lua_pushnil(L); /* number is not convertible to integer */ 86 lua_pushnil(L); /* value is not convertible to integer */
87 } 87 }
88 return 1; 88 return 1;
89} 89}
90 90
91 91
92static int math_floor (lua_State *L) {
93 int valid;
94 lua_Integer n = lua_tointegerx(L, 1, &valid);
95 if (valid)
96 lua_pushinteger(L, n); /* floor computed by Lua */
97 else
98 lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1)));
99 return 1;
100}
101
102
103static void pushnumint (lua_State *L, lua_Number d) { 92static void pushnumint (lua_State *L, lua_Number d) {
104 lua_Integer n; 93 lua_Integer n;
105 if (lua_numtointeger(d, &n)) /* fits in an integer? */ 94 if (lua_numtointeger(d, &n)) /* does 'd' fit in an integer? */
106 lua_pushinteger(L, n); /* result is integer */ 95 lua_pushinteger(L, n); /* result is integer */
107 else 96 else
108 lua_pushnumber(L, d); /* result is float */ 97 lua_pushnumber(L, d); /* result is float */
109} 98}
110 99
111 100
101static int math_floor (lua_State *L) {
102 if (lua_isinteger(L, 1))
103 lua_settop(L, 1); /* integer is its own floor */
104 else {
105 lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
106 pushnumint(L, d);
107 }
108 return 1;
109}
110
111
112static int math_ceil (lua_State *L) { 112static int math_ceil (lua_State *L) {
113 if (lua_isinteger(L, 1)) 113 if (lua_isinteger(L, 1))
114 lua_settop(L, 1); /* integer is its own ceil */ 114 lua_settop(L, 1); /* integer is its own ceil */
@@ -264,15 +264,16 @@ static int math_randomseed (lua_State *L) {
264 264
265 265
266static int math_type (lua_State *L) { 266static int math_type (lua_State *L) {
267 luaL_checkany(L, 1);
268 if (lua_type(L, 1) == LUA_TNUMBER) { 267 if (lua_type(L, 1) == LUA_TNUMBER) {
269 if (lua_isinteger(L, 1)) 268 if (lua_isinteger(L, 1))
270 lua_pushliteral(L, "integer"); 269 lua_pushliteral(L, "integer");
271 else 270 else
272 lua_pushliteral(L, "float"); 271 lua_pushliteral(L, "float");
273 } 272 }
274 else 273 else {
274 luaL_checkany(L, 1);
275 lua_pushnil(L); 275 lua_pushnil(L);
276 }
276 return 1; 277 return 1;
277} 278}
278 279
@@ -339,7 +340,7 @@ static const luaL_Reg mathlib[] = {
339 {"cos", math_cos}, 340 {"cos", math_cos},
340 {"deg", math_deg}, 341 {"deg", math_deg},
341 {"exp", math_exp}, 342 {"exp", math_exp},
342 {"ifloor", math_ifloor}, 343 {"tointeger", math_toint},
343 {"floor", math_floor}, 344 {"floor", math_floor},
344 {"fmod", math_fmod}, 345 {"fmod", math_fmod},
345 {"log", math_log}, 346 {"log", math_log},