Révision 51 pobysoPythonSage/src/sageMpfr.spyx

sageMpfr.spyx (revision 51)
1 1
from sage.rings.real_mpfr cimport *
2 2
#
3
# Two functions to manipulate the real RealNumber values.
3
# Functions to manipulate the real RealNumber values.
4 4
#
5
# Get the address of the value of a RealNumber. This address can be
6
# used to to set the value as well.
7
#
5
# Import constants and declarations that are not exported
6
# from the Sage real_mpfr.pyx file.
7
# Notes:
8
# - notice how #define constants are imported.
9
# - mpfr_min_prec is declared with an int return value, not
10
#   a mpfr_prec_t since it is not recognized as type in Cython.
11
cdef extern from "mpfr.h":
12
    cdef int MPFR_PREC_MIN "MPFR_PREC_MIN"
13
    cdef int mpfr_min_prec(mpfr_t x)
14
        
15

  
8 16
cpdef int get_rn_value(RealNumber x):
17
    """
18
    Get the address of the value of a RealNumber. This address can be
19
    used to to set the value as well.
20
    """
9 21
    return(<int>&(x.value))
10
#
11
# We use the "array trick" to workaround the pointer dereferencing
12
# issue (in Cython there is no unary operator "*").
13
#
22

  
14 23
cpdef set_rn_value(RealNumber x, int p, mp_rnd_t rnd=GMP_RNDN):
24
    """
25
    Set the value of x from MPFR number p.
26
    """
27
    # We use the "array trick" to workaround the pointer dereferencing
28
    # issue (in Cython there is no unary operator "*").
29
    #
15 30
    mpfr_set(x.value, (<mpfr_t *>p)[0], rnd)
16 31
#
32
cpdef min_rn_size(RealNumber x):
33
    """
34
    Compute the minimum number of bits necessary to represent
35
    a RealNumber's value.
36
    If x is a power of 2 (only 1 bit needed) return the MPFR
37
    MPFR_PREC_MIN value.
38
    """
39
    cdef int min_prec = mpfr_min_prec(x.value) 
40
    if min_prec < MPFR_PREC_MIN:
41
        return(MPFR_PREC_MIN)
42
    else:
43
        return(min_prec)
44

  
17 45
# Do not use this function!
18 46
# It changes the precision of the instance behind the curtain, but precision is
19 47
# class attribute. If the precision is increased, memory space is wasted and

Formats disponibles : Unified diff