Ask Your Question

static typing of mpf variables in cython

asked 2013-05-16 01:02:42 +0200

anonymous user



I am a new user of cython. I was looking for the static typing of python's mpf type variables in cython for speed up.

For example:

python: N=10 static typing in cython: cdef int N=10

Now I'm looking for the equivalent for mpf variables:

python: from mpmath import mpf , N = mpf(10)

cython: ??? maybe sth like: 'cdef mpf_t N=10' but this doesn't work.

Thank you,

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-05-16 04:04:07 +0200

vdelecroix gravatar image

updated 2013-05-16 06:40:44 +0200


It depends. You have the choice of using a Cython variable as follows

from sage.rings.real_mpfr cimport RealNumber
from sage.rings.real_mpfr import RR

cdef RealNumber my_number = RR(5.634)

Or you can use pure C as follows

cdef mpfr_t value
mpfr_init2(value, 53)
mpfr_set_flt(value, 5.364, MPFR_RNDN)

Anyway, in the latter case, it is not possible to write value = 5 because there is no cast from int to mpfr_t (those are C variables)!

It might help to have a look at how the real number are implemented in Sage in the files

  • $SAGE_ROOT/devel/sage-main/sage/rings/real_mpfr.pyx
  • $SAGE_ROOT/devel/sage-main/sage/rings/real_mpfr.pxd
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-05-16 01:02:42 +0200

Seen: 9,792 times

Last updated: May 16 '13