[Cython] Problem with cdef long
Robert Bradshaw
robertwb at math.washington.edu
Mon Jun 2 22:00:32 CEST 2008
On Jun 2, 2008, at 12:51 PM, Simon King wrote:
> Dear Cython team,
>
> hopefully the following really is a cython-question, not a Sage-
> question.
>
> Write a file Problem.pyx:
>
> ctypedef struct Term_t:
> long coef
>
> cdef class Term:
> cdef Term_t Data
> def __init__(self, c):
> self.Data.coef = c
> def coefficient(self):
> return self.Data.coef
>
>
> Start Sage and do
> sage: attach Problem.pyx
> sage: T=Term(3)
> sage: type(T.coefficient())
>
> Then the result is
> <type 'int'>
> and *not* <type 'long'>!
>
> Why is it of type 'int' although coef is defined 'long' in Term_t?
I think your confusion here is over the difference between C ints/
longs/etc. and Python ints/longs. The Python "int" type is a Python
object that wraps a C long. Python "long" type is an arbitrary-
precision integer. Your Term.coefficient function returns a Python
object, so it takes the self.Data.coef (which is a C long) and wraps
it in a Python object (of type "int").
> How can i avoid this automatic down-grading of coef?
>
> I really want coef to be of type 'long', since 'int' isn't good
> enough in
> my application.
When you say "int" isn't good enough, do you mean you need arbitrary
precision? Because there isn't a (simple) C type that will give you
that (you would have to use mpz_t or something like that).
- Robert
More information about the Cython-dev
mailing list