lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Norman Ramsey <nr@cs.tufts.edu> wrote:

> I'm hoping that the relevant tests might look like this:
>
>   if ((LUA_NUMBER)(int) n != n)
>     lua_error(L, "numeric overflow in string.format(... %d ...)");

Sadly C compilers are allowed to compile this code wrong, so to be
strictly correct you have to check the floating point value against
the range limits of the integer type before conversion.

ISO/IEC 9899:TC3 http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf

6.3 Conversions
6.3.1.4 Real floating and integer

1 When a finite value of real floating type is converted to an integer
  type other than _Bool, the fractional part is discarded (i.e., the value
  is truncated toward zero). If the value of the integral part cannot be
  represented by the integer type, the behavior is undefined. [50]

[50] The remaindering operation performed when a value of integer type is
  converted to unsigned type need not be performed when a value of real
  floating type is converted to unsigned type. Thus, the range of portable
  real floating values is (-1, Utype_MAX+1).

Tony.
-- 
f.anthony.n.finch  <dot@dotat.at>  http://dotat.at/
Rockall, Malin: Mainly southeast veering southwest 5 to 7, but cyclonic 3 or 4
at times. Moderate or rough, but slight in southeast Malin. Rain. Moderate or
good, occasionally poor.