lua-users home
lua-l archive

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

Am 07.03.2014 09:17 schröbte Thomas Jericke:
On one of our targets a PowerPc P2020 we accountered a problem with the
casting from double to int. The instruction that casts from double to in
checks for the size of the number and traps on numbers that don't fit
the int.

The probem is the lua_number2int(k, n) in cases n > INT_MAX

I use the default definition which is:
/* the following definitions always work, but may be slow */

#define lua_number2int(i,n)    ((i)=(int)(n))

So much about the "always work" :-)

Now I looked into the C rationale and was not really sure if this is a
non-standard behaviour, the standard says that in a cast to int the
result is not defined in this case, but it doesn't say that the
behaviour is undefined. Floating and integral

When a value of floating type is converted to integral type, the fractional part is discarded. If the value of the integral part cannot be represented by the integral type, the behavior is undefined. /23/

Footnote /23/ is:
23. The remaindering operation done when a value of integral type is converted to unsigned type need not be done when a value of floating type is converted to unsigned type. Thus the range of portable values is [0, U type _MAX +1).

Looks clear-cut to me ...