lua-users home
lua-l archive

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


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