Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operation 10**(-9) correct in python, but wrong in Cython

Tags:

python

cython

Very basic question: in my python 2.7 code I have situation roughly as follows:

b=5.0*10**(-9)
a=9
print(a)
c=a/(1.0*b)

the code runs in python / spyder (64bit), but fails in Cython, because of a float division by 0. The printed number is 0. When I define

b=0.000000005

the division is fine and the printed number, too. What is the error?

like image 967
mic Avatar asked May 13 '15 13:05

mic


1 Answers

I can reproduce your error with cython 0.22 and I think this error is related to this thread at cython-users.

It seems that cython gets into trouble when calculating 10**-9. If you use 10**-9.0 instead, everything works fine.

Note, that you could get rid of the error and prettify the code snippet by replacing 5.0*10**(-9) by 5e-9.

Nevertheless this seems to be a bug in cython and not in your code base.

like image 111
cel Avatar answered Oct 06 '22 16:10

cel