float v. double

858 views
Skip to first unread message

Skip Montanaro

unread,
Jan 13, 2015, 12:56:05 PM1/13/15
to cython-users
I'm still using 0.17.1 (not much I can really do about it in the short
term). I just discovered a bug in some Cython code. I had naively
declared a couple local variables as float:

cdef float t = self._time
cdef float v

thinking that "float" was the same as Python's float (e.g., a C
double). That proved not to be the case. When I changed the
declarations to specify "double", things started working.

Looking in the Language Basics documentation:

http://docs.cython.org/src/userguide/language_basics.html

I saw no mention of the distinction between Python float and Cython
float. It seems like it should be mentioned somewhere. If so, the
searching I did turned nothing up.

Skip Montanaro

Sturla Molden

unread,
Jan 13, 2015, 1:35:30 PM1/13/15
to cython...@googlegroups.com
On 13/01/15 18:56, Skip Montanaro wrote:
> I'm still using 0.17.1 (not much I can really do about it in the short
> term). I just discovered a bug in some Cython code. I had naively
> declared a couple local variables as float:
>
> cdef float t = self._time
> cdef float v

cdef is a C type definition.

You have the same issue with other naming conflicts as well, e.g. int
and long.

Sturla


Nils Bruin

unread,
Jan 13, 2015, 5:27:50 PM1/13/15
to cython...@googlegroups.com
On Tuesday, January 13, 2015 at 9:56:05 AM UTC-8, Skip Montanaro wrote:
Looking in the Language Basics documentation:

http://docs.cython.org/src/userguide/language_basics.html

I saw no mention of the distinction between Python float and Cython
float. It seems like it should be mentioned somewhere. If so, the
searching I did turned nothing up.

It has this passage:

"""
Also, the Python types list, dict, tuple, etc. may be used for static typing, as well as any user defined extension types. The Python types int and long are not available for static typing and instead interpreted as C int and long respectively, as statically typing variables with Python integer types has zero advantages.
"""

Perhaps it should include "float" with the Python types not available for static typing? Provided that there is indeed zero advantage to statically type Python floats. Judging from https://docs.python.org/2/c-api/float.html there would indeed be very little advantage to statically type a PyFloat.

Robert Bradshaw

unread,
Jan 13, 2015, 7:06:22 PM1/13/15
to cython...@googlegroups.com

Stefan Behnel

unread,
Jan 16, 2015, 7:14:14 AM1/16/15
to cython...@googlegroups.com
Nils Bruin schrieb am 13.01.2015 um 23:27:
> Provided that there is indeed zero advantage to statically
> type Python floats. Judging from https://docs.python.org/2/c-api/float.html
> there would indeed be very little advantage to statically type a PyFloat.

Cython automatically coerces C floating point values back to Python "float"
objects at need, and Python's "float" otherwise behaves exactly like C's
"double", so there is really no advantage in typing anything as "Python
float". You can always safely use C "double" instead, or C "float" if your
use case allows it.

Stefan

Reply all
Reply to author
Forward
0 new messages