On 02/14/2013 04:49 PM, Adam D. Ruppe wrote:
const long DIM = 1024*1024*1024*1024*4;

Those are all int (32 bit) literals so it is prolly wrapping around the
intermediates before it actually assigns to DIM.

If you used 1024L it should be better, by making the right hand side 64 bit too.

Oh, snap. :-P

First time I ran into this kind of issue was when I was calculating a power of 2 in C++ by using:

    size_t p = 1 << m;

m was calculated to ensure that 2 ^^ m was the largest power of 2 that could (i) be multiplied by 2 without integer wraparound and (ii) fell within the range of the uniform random number generator. And all was fine until I upgraded my system to 64-bit, and suddenly I was getting a nonsense value of p.

It shouldn't have changed, because the RNG used a 32-bit unsigned integer as its internal data type. After much head-scratching I cottoned on to the fact that as it was, 1 << m was a regular integer that was wrapping around to a negative value before being converted to size_t (which in practice meant: it came out as the maximum of size_t less the negative value). With 32-bit size_t this was the correct value. With 64-bit size_t it meant a value of p far larger than it should have been.

Replace the power-of-2 statement with

    size_t p = 1UL << m;

... and everything was cured.

Reply via email to