mirror of
https://github.com/lua/lua
synced 2024-11-22 12:51:30 +03:00
new conversion float->integer: conversion is valid only when
float has an exact representation as an integer
This commit is contained in:
parent
c229ed597f
commit
8bb272a3e3
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lauxlib.c,v 1.264 2014/06/26 17:25:11 roberto Exp roberto $
|
||||
** $Id: lauxlib.c,v 1.265 2014/07/16 14:51:36 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -396,8 +396,8 @@ LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) {
|
||||
|
||||
|
||||
static void interror (lua_State *L, int arg) {
|
||||
if (lua_type(L, arg) == LUA_TNUMBER)
|
||||
luaL_argerror(L, arg, "float value out of integer range");
|
||||
if (lua_isnumber(L, arg))
|
||||
luaL_argerror(L, arg, "number has no integer representation");
|
||||
else
|
||||
tag_error(L, arg, LUA_TNUMBER);
|
||||
}
|
||||
|
12
ldebug.c
12
ldebug.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldebug.c,v 2.97 2013/12/09 14:21:10 roberto Exp roberto $
|
||||
** $Id: ldebug.c,v 2.98 2014/07/15 21:26:50 roberto Exp roberto $
|
||||
** Debug Interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -534,18 +534,20 @@ l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
|
||||
|
||||
l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
|
||||
lua_Number temp;
|
||||
if (!tonumber(p1, &temp))
|
||||
p2 = p1; /* first operand is wrong */
|
||||
if (!tonumber(p1, &temp)) /* first operand is wrong? */
|
||||
p2 = p1; /* now second is wrong */
|
||||
luaG_typeerror(L, p2, "perform arithmetic on");
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Error when both values are convertible to numbers, but not to integers
|
||||
*/
|
||||
l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
|
||||
lua_Integer temp;
|
||||
if (!tointeger(p1, &temp))
|
||||
p2 = p1;
|
||||
luaG_runerror(L, "attempt to convert an out of range float%s to an integer",
|
||||
varinfo(L, p2));
|
||||
luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
|
||||
}
|
||||
|
||||
|
||||
|
41
lmathlib.c
41
lmathlib.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmathlib.c,v 1.105 2014/06/30 19:48:08 roberto Exp roberto $
|
||||
** $Id: lmathlib.c,v 1.106 2014/07/16 13:47:13 roberto Exp roberto $
|
||||
** Standard mathematical library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -76,39 +76,39 @@ static int math_atan (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int math_ifloor (lua_State *L) {
|
||||
static int math_toint (lua_State *L) {
|
||||
int valid;
|
||||
lua_Integer n = lua_tointegerx(L, 1, &valid);
|
||||
if (valid)
|
||||
lua_pushinteger(L, n); /* floor computed by Lua */
|
||||
lua_pushinteger(L, n);
|
||||
else {
|
||||
luaL_checktype(L, 1, LUA_TNUMBER); /* argument must be a number */
|
||||
lua_pushnil(L); /* number is not convertible to integer */
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushnil(L); /* value is not convertible to integer */
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int math_floor (lua_State *L) {
|
||||
int valid;
|
||||
lua_Integer n = lua_tointegerx(L, 1, &valid);
|
||||
if (valid)
|
||||
lua_pushinteger(L, n); /* floor computed by Lua */
|
||||
else
|
||||
lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void pushnumint (lua_State *L, lua_Number d) {
|
||||
lua_Integer n;
|
||||
if (lua_numtointeger(d, &n)) /* fits in an integer? */
|
||||
if (lua_numtointeger(d, &n)) /* does 'd' fit in an integer? */
|
||||
lua_pushinteger(L, n); /* result is integer */
|
||||
else
|
||||
lua_pushnumber(L, d); /* result is float */
|
||||
}
|
||||
|
||||
|
||||
static int math_floor (lua_State *L) {
|
||||
if (lua_isinteger(L, 1))
|
||||
lua_settop(L, 1); /* integer is its own floor */
|
||||
else {
|
||||
lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
|
||||
pushnumint(L, d);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int math_ceil (lua_State *L) {
|
||||
if (lua_isinteger(L, 1))
|
||||
lua_settop(L, 1); /* integer is its own ceil */
|
||||
@ -264,15 +264,16 @@ static int math_randomseed (lua_State *L) {
|
||||
|
||||
|
||||
static int math_type (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
if (lua_type(L, 1) == LUA_TNUMBER) {
|
||||
if (lua_isinteger(L, 1))
|
||||
lua_pushliteral(L, "integer");
|
||||
else
|
||||
lua_pushliteral(L, "float");
|
||||
}
|
||||
else
|
||||
else {
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushnil(L);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -339,7 +340,7 @@ static const luaL_Reg mathlib[] = {
|
||||
{"cos", math_cos},
|
||||
{"deg", math_deg},
|
||||
{"exp", math_exp},
|
||||
{"ifloor", math_ifloor},
|
||||
{"tointeger", math_toint},
|
||||
{"floor", math_floor},
|
||||
{"fmod", math_fmod},
|
||||
{"log", math_log},
|
||||
|
22
lvm.c
22
lvm.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lvm.c,v 2.216 2014/06/19 18:27:20 roberto Exp roberto $
|
||||
** $Id: lvm.c,v 2.217 2014/06/30 19:48:08 roberto Exp roberto $
|
||||
** Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -80,15 +80,23 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
|
||||
|
||||
|
||||
/*
|
||||
** try to convert a value to an integer, rounding up if 'up' is true
|
||||
** try to convert a value to an integer, rounding according to 'mode':
|
||||
** mode == 0: accepts only integral values
|
||||
** mode < 0: takes the floor of the number
|
||||
** mode > 0: takes the ceil of the number
|
||||
*/
|
||||
static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) {
|
||||
static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) {
|
||||
TValue v;
|
||||
again:
|
||||
if (ttisfloat(obj)) {
|
||||
lua_Number n = fltvalue(obj);
|
||||
n = (up ? -l_floor(-n) : l_floor(n));
|
||||
return lua_numtointeger(n, p);
|
||||
lua_Number f = l_floor(n);
|
||||
if (n != f) { /* not an integral value? */
|
||||
if (mode == 0) return 0; /* fails if mode demands integral value */
|
||||
else if (mode > 0) /* needs ceil? */
|
||||
f += 1; /* convert floor to ceil (remember: n != f) */
|
||||
}
|
||||
return lua_numtointeger(f, p);
|
||||
}
|
||||
else if (ttisinteger(obj)) {
|
||||
*p = ivalue(obj);
|
||||
@ -104,7 +112,7 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) {
|
||||
|
||||
|
||||
/*
|
||||
** try to convert a non-integer value to an integer, rounding down
|
||||
** try to convert a value to an integer
|
||||
*/
|
||||
int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
|
||||
return tointeger_aux(obj, p, 0);
|
||||
@ -155,7 +163,7 @@ int luaV_tostring (lua_State *L, StkId obj) {
|
||||
static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step,
|
||||
int *stopnow) {
|
||||
*stopnow = 0; /* usually, let loops run */
|
||||
if (!tointeger_aux(obj, p, (step < 0))) { /* does not fit in integer? */
|
||||
if (!tointeger_aux(obj, p, (step < 0 ? 1 : -1))) { /* not fit in integer? */
|
||||
lua_Number n; /* try to convert to float */
|
||||
if (!tonumber(obj, &n)) /* cannot convert to float? */
|
||||
return 0; /* not a number */
|
||||
|
Loading…
Reference in New Issue
Block a user