M_PI = 3

124 views
Skip to first unread message

Thomas Wiecki

unread,
Aug 16, 2011, 2:58:51 PM8/16/11
to cython...@googlegroups.com
Hi,

for some reason from libc.math cimport M_PI imports M_PI as type int
and of value 3. I think it might have to do with declaring M_PI as
enum (not sure what that does). Can anyone reproduce this?

-Thomas

Blair Bonnett

unread,
Aug 16, 2011, 6:47:16 PM8/16/11
to cython...@googlegroups.com
I get the same behaviour if I simply return the value. However, if you cast it to a more suitable type (e.g., double) you get a more accurate value:

pi.pyx:

from libc.math cimport M_PI

def return_pi():
    return <double>M_PI


Python console:

>>> import pi
>>> pi.return_pi()
3.141592653589793

Looking at the generated C code for a version without the cast, it makes the call PyInt_FromLong(M_PI); which indicates it thinks M_PI is a long integer in C. If you put the cast in, this becomes PyFloat_FromDouble(((double)M_PI)) which makes C convert it to a double first which seems to work. I'm not entirely sure why this is happening, or indeed whether this is a bug or not, but at least you can work around it in the meantime.

Blair

Thomas Wiecki

unread,
Aug 16, 2011, 6:51:07 PM8/16/11
to cython...@googlegroups.com
Hi Blair,

for now my workaround is:

cdef extern from "math.h":
double M_PI

Which works fine. But this seems to be a bug.

-Thomas

Robert Bradshaw

unread,
Aug 16, 2011, 7:23:15 PM8/16/11
to cython...@googlegroups.com
On Tue, Aug 16, 2011 at 3:51 PM, Thomas Wiecki
<thomas...@googlemail.com> wrote:
> Hi Blair,
>
> for now my workaround is:
>
> cdef extern from "math.h":
>    double M_PI
>
> Which works fine. But this seems to be a bug.

Definitely a bug, fixed at
https://github.com/cython/cython/commit/8a74aefcde864c76dffd2eeeb497395ae7aea172

Declaring M_PI as an enum makes it more "constant-like" but also
caused it to treat it as an integer for purposes of
conversion/inference. Thanks for the report.

- Robert

Gökhan Sever

unread,
Aug 16, 2011, 8:44:00 PM8/16/11
to cython...@googlegroups.com

Thanks for the report and very quick fix. In my calculations M_PI is
used at least more than 5 times. Knowing that M_PI was misbehaving is
important to me. With this example I see the importance of writing
test cases, hopefully soon I will adopt this habit for my code.

--
Gökhan

Robert Bradshaw

unread,
Aug 16, 2011, 8:47:42 PM8/16/11
to cython...@googlegroups.com
On Tue, Aug 16, 2011 at 5:44 PM, Gökhan Sever <gokha...@gmail.com> wrote:
> On Tue, Aug 16, 2011 at 5:23 PM, Robert Bradshaw
> <robe...@math.washington.edu> wrote:
>> On Tue, Aug 16, 2011 at 3:51 PM, Thomas Wiecki
>> <thomas...@googlemail.com> wrote:
>>> Hi Blair,
>>>
>>> for now my workaround is:
>>>
>>> cdef extern from "math.h":
>>>    double M_PI
>>>
>>> Which works fine. But this seems to be a bug.
>>
>> Definitely a bug, fixed at
>> https://github.com/cython/cython/commit/8a74aefcde864c76dffd2eeeb497395ae7aea172
>>
>> Declaring M_PI as an enum makes it more "constant-like" but also
>> caused it to treat it as an integer for purposes of
>> conversion/inference. Thanks for the report.
>>
>> - Robert
>>
>
> Thanks for the report and very quick fix. In my calculations M_PI is
> used at least more than 5 times. Knowing that M_PI was misbehaving is
> important to me.

Note that it only happened if it was immediately used in an
object/integer context, but still a very bad bug.

> With this example I see the importance of writing
> test cases, hopefully soon I will adopt this habit for my code.

Yep. We even had a test of math.pxd here, just not a good enough one :).

- Robert

Reply all
Reply to author
Forward
0 new messages