[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: problem with string.format %d and very large integers
- From: Tony Finch <dot@...>
- Date: Thu, 11 Aug 2011 18:46:49 +0100
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.