Révision 52 pobysoPythonSage/src/sageMpfr.spyx
sageMpfr.spyx (revision 52) | ||
---|---|---|
6 | 6 |
# from the Sage real_mpfr.pyx file. |
7 | 7 |
# Notes: |
8 | 8 |
# - notice how #define constants are imported. |
9 |
# - mpfr_min_prec is declared with an int return value, not |
|
9 |
# - mpfr_min_prec is declared with an unsigned int return value, not
|
|
10 | 10 |
# a mpfr_prec_t since it is not recognized as type in Cython. |
11 | 11 |
cdef extern from "mpfr.h": |
12 | 12 |
cdef int MPFR_PREC_MIN "MPFR_PREC_MIN" |
13 |
cdef int mpfr_min_prec(mpfr_t x) |
|
13 |
cdef unsigned int mpfr_min_prec(mpfr_t x)
|
|
14 | 14 |
|
15 | 15 |
|
16 | 16 |
cpdef int get_rn_value(RealNumber x): |
... | ... | |
32 | 32 |
cpdef min_rn_size(RealNumber x): |
33 | 33 |
""" |
34 | 34 |
Compute the minimum number of bits necessary to represent |
35 |
a RealNumber's value.
|
|
35 |
the x RealNumber's value.
|
|
36 | 36 |
If x is a power of 2 (only 1 bit needed) return the MPFR |
37 | 37 |
MPFR_PREC_MIN value. |
38 | 38 |
""" |
... | ... | |
42 | 42 |
else: |
43 | 43 |
return(min_prec) |
44 | 44 |
|
45 |
cpdef min_mpfr_size(int p): |
|
46 |
""" |
|
47 |
Compute the minimum number of bits necessary to represent |
|
48 |
the MPFR value pointed by p. |
|
49 |
If x is a power of 2 (only 1 bit needed) return the MPFR |
|
50 |
MPFR_PREC_MIN value. |
|
51 |
""" |
|
52 |
cdef int min_prec = mpfr_min_prec((<mpfr_t *>p)[0]) |
|
53 |
if min_prec < MPFR_PREC_MIN: |
|
54 |
return(MPFR_PREC_MIN) |
|
55 |
else: |
|
56 |
return(min_prec) |
|
57 |
|
|
58 |
cpdef new_mpfr_num(unsigned int prec): |
|
59 |
cdef mpfr_t *newValue = NULL |
|
60 |
print "Precision : ", prec |
|
61 |
newValue = <mpfr_t*>malloc(sizeof(mpfr_t)) |
|
62 |
mpfr_init2(newValue[0], prec) |
|
63 |
print "Creation done!" |
|
64 |
return(<int>newValue) |
|
65 |
|
|
66 |
cpdef free_mpfr_num(int mpfrNum): |
|
67 |
mpfr_clear((<mpfr_t*>mpfrNum)[0]) |
|
68 |
|
|
69 |
# |
|
70 |
|
|
71 |
cpdef cmp_rn_value(RealNumber x, RealNumber y): |
|
72 |
""" |
|
73 |
Compare two RN with the mpfr_cmp function |
|
74 |
""" |
|
75 |
return(mpfr_cmp(x.value, y.value)) |
|
76 |
|
|
45 | 77 |
# Do not use this function! |
46 | 78 |
# It changes the precision of the instance behind the curtain, but precision is |
47 | 79 |
# class attribute. If the precision is increased, memory space is wasted and |
... | ... | |
56 | 88 |
#x.__prec = mpfr_get_prec(x.value) |
57 | 89 |
printf("x.value prec: %d\n", mpfr_get_prec(x.value)) |
58 | 90 |
mpfr_set(x.value, (<mpfr_t *>p)[0], GMP_RNDN) |
59 |
# |
|
60 |
# Compare two RN with the mpfr_cmp function |
|
61 |
cpdef cmp_rn_value(RealNumber x, RealNumber y): |
|
62 |
return(mpfr_cmp(x.value, y.value)) |
Formats disponibles : Unified diff