I'm new to Cython, and have made a mean.pyx
file with
#cython: language_level=3
def cmean(list arr, int length):
cdef float tot
cdef float elem
tot = 0
for i in range(length):
elem = arr[i]
tot += elem
tot /= length
return tot
I then call this from a Python file main.py
:
import pyximport
pyximport.install()
from mean import cmean
arr = [1,2,4]
cres = cmean(arr, len(arr))
pyres = sum(arr)/len(arr)
print(cres)
print(pyres)
print(cres == pyres)
which outputs
2.3333332538604736
2.3333333333333335
False
Why are the results not the same?
I'm using Cython==0.29.30 and Python 3.9.2