On 26/02/2018 13:42, Ned Batchelder wrote:
On 2/26/18 7:13 AM, bartc wrote:
A C version is given below. (One I may have messed around with, which
I'm not sure works properly. For an original, google for Marsaglia and
KISS64 or SUPRKISS64.)
Most integers are unsigned, which have well-defined overflow in C
With proper 64-bit masking (def only64(x): return x &
0xFFFFFFFFFFFFFFFF), the Python version produces the correct answer
using a reasonable amount of memory.
I did try sometime like that, but I must have missed something because I
didn't get quite the same results as a working version.
And with interpreted code, you tend not to test using loops of a billion
iterations.
Well, once you notice that the
Python code had N=1e5, and the C code had N=1e9 :) If you want to
experiment, with N=1e5, the final number should be 5255210926702073855.
OK, I'll try that.
Also, I note that you said, "Most integers are unsigned", but the C code
has them all declared as signed? It doesn't seem to have mattered to
your result, but I'm not an expert on C portability guarantees.
The C code I first pasted used 'unsigned', but the main program logic
wasn't right, and I found another version that looked better. That one
used 'signed' for some reason, which I completely missed.
Even if with C it works with either, the signed version might have
'undefined behaviour'. As said, google for the original; the ones I can
see have 'unsigned'. But I can also see a Fortran version that just uses
'integer*8', which I believe is signed 64-bit.
--
bartc
--
https://mail.python.org/mailman/listinfo/python-list