I play with ctypes ... I have the following C code
Change 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 with the following command (on OSX)
$ gcc -shared -Wl,-install_name,sumrange.so -o ./sumrange.so -fPIC ./sumrange.c
I implemented the same function in python:
pysumrange = lambda arg: sum(xrange(arg))
Then I run both in an 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 do not match. Why should it be?
I tried using unsigned long long for all variables in C code with no effect (same output).
source share