root / pobysoPythonSage / src / sageMpfr.spyx @ 37
Historique | Voir | Annoter | Télécharger (1,26 ko)
1 | 5 | storres | from sage.rings.real_mpfr cimport * |
---|---|---|---|
2 | 5 | storres | # |
3 | 5 | storres | # Two functions to manipulate the real RealNumber values. |
4 | 5 | storres | # |
5 | 5 | storres | # Get the address of the value of a RealNumber. This address can be |
6 | 5 | storres | # used to to set the value as well. |
7 | 5 | storres | # |
8 | 5 | storres | cpdef int get_rn_value(RealNumber x): |
9 | 5 | storres | return(<int>&(x.value)) |
10 | 5 | storres | # |
11 | 5 | storres | # We use the "array trick" to workaround the pointer dereferencing |
12 | 5 | storres | # issue (in Cython there is no unary operator "*"). |
13 | 5 | storres | # |
14 | 5 | storres | cpdef set_rn_value(RealNumber x, int p, mp_rnd_t rnd=GMP_RNDN): |
15 | 5 | storres | mpfr_set(x.value, (<mpfr_t *>p)[0], rnd) |
16 | 5 | storres | # |
17 | 5 | storres | # Do not use this function! |
18 | 5 | storres | # It changes the precision of the instance behind the curtain, but precision is |
19 | 5 | storres | # class attribute. If the precision is increased, memory space is wasted and |
20 | 5 | storres | # other problems may surface in subsequent operations. |
21 | 5 | storres | # If the precision is decreased the semantics |
22 | 5 | storres | # are broken since the expected precision (from the class attribute) is not |
23 | 5 | storres | # the actual precision. |
24 | 5 | storres | # |
25 | 5 | storres | cpdef s_rn_value(RealNumber x, int p): |
26 | 5 | storres | mpfr_clear(x.value) |
27 | 5 | storres | mpfr_init2(x.value, mpfr_get_prec((<mpfr_t *>p)[0])) |
28 | 5 | storres | #x.__prec = mpfr_get_prec(x.value) |
29 | 5 | storres | printf("x.value prec: %d\n", mpfr_get_prec(x.value)) |
30 | 5 | storres | mpfr_set(x.value, (<mpfr_t *>p)[0], GMP_RNDN) |
31 | 5 | storres | # |
32 | 5 | storres | # Compare two RN with the mpfr_cmp function |
33 | 5 | storres | cpdef cmp_rn_value(RealNumber x, RealNumber y): |
34 | 5 | storres | return(mpfr_cmp(x.value, y.value)) |