lua-users home
lua-l archive

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


On Tue, Apr 15, 2014 at 2:13 PM, Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:
> Can someone with more experience with MS check this?
>
> Consider the next program:
>
> /*---------------------------------------------------*/
> #include <stdio.h>
> #include <windows.h>
>
> int main (void) {
>   unsigned __int64 x = 0xFFFFFFFFFFFFFFFFL;
>   double d = (double)x * 0.9;
>   unsigned __int64 y = (unsigned __int64)d;
>   printf("%I64x %I64x  %g\n", x, y, d);
>   return 0;
> }
> /*---------------------------------------------------*/
>
> 'x' is the maximum value allowed for its type, 2^64-1. Then it is
> converted to double, and rounded to 2^64. The product with 0.9 gives
> a number "much" smaller than 2^64 (~1.6e19 versus ~1.8e19) which,
> when rounded, clearly fits in 'y'. However, when I run this program
> in Visual Studio 2010, it prints 'y' as 8000000000000000, signaling
> an overflow.
>
> (I need this casting, so that math.random(math.mininteger, math.maxinteger)
> can give its full range of values.)
>
> Thanks in advance,
>
> -- Roberto
>
>

I'm just taking a wild guess here, but don't you need long long for
that 0xFFFFF... literal?  "0xFFFFFFFFFFFFFFFFLL" ?

I thought long int on Window x64 was still 32 bits (don't you need
long long for 64 bits?), I was looking for the wikipedia article on
data models for different operating systems and I couldn't find it :(
Maybe I'm wrong and this is standard per x86_64.