I am toying with ctypes… I have the following C code
Edit: The reason I'm trying to figure this out is to make this blog post more correct
sumrange.c
#include <stdio.h>
long sumrange(long);
long sumrange(long arg)
{
long i, x;
x = 0L;
for (i = 0L; i < arg; i++) {
x = x + i;
}
return x;
}
which I compile using the following command (on OSX)
$ gcc -shared -Wl,-install_name,sumrange.so -o ./sumrange.so -fPIC ./sumrange.c
I've implemented the same function in python:
pysumrange = lambda arg: sum(xrange(arg))
Then I run both at the interactive shell:
>>> import ctypes
>>> sumrange = ctypes.CDLL('./sumrange.so')
>>> pysumrange = lambda arg: sum(xrange(arg))
>>> print sumrange.sumrange(10**8), pysumrange(10**8)
... 887459712 4999999950000000
…and the numbers don't match. Why would this be?
I tried using unsigned long long
for all the variables in the C code with no effect (same output).