Ignore:
Timestamp:
01/23/07 13:23:38 (6 years ago)
Author:
William Stein <wstein@…>
Branch:
default
Message:

Fix trac # 204 and do some optimization.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sage/rings/real_mpfr.pyx

    r2569 r2585  
    99   -- William Stein (2006-10): default printing truncates to avoid base-2 
    1010              rounding confusing (fix suggested by Bill Hart) 
     11 
     12EXAMPLES: 
     13 
     14A difficult conversion: 
     15 
     16    sage: RR(sys.maxint) 
     17    9223372036854770000     # 64-bit  
    1118""" 
    1219  
     
    202209        if hasattr(x, '_mpfr_'): 
    203210            return x._mpfr_(self) 
    204         return RealNumber(self, x, base) 
     211        cdef RealNumber z 
     212        z = self._new() 
     213        z._set(x, base) 
     214        return z 
    205215 
    206216    cdef _coerce_c_impl(self, x): 
     
    471481        return x 
    472482         
    473     def __init__(self, RealField parent, x=0, int base=10, special=None): 
     483    def __init__(self, RealField parent, x=0, int base=10): 
    474484        """ 
    475485        Create a real number.  Should be called by first creating 
     
    514524        self.init = 1 
    515525        if x is None: return 
     526        self._set(x, base) 
     527 
     528    cdef _set(self, x, int base): 
     529        # This should not be called except when the number is being created. 
     530        # Real Numbers are supposed to be immutable.  
    516531        cdef RealNumber _x, n, d 
    517         cdef int _ix 
     532        cdef Integer _ix 
     533        cdef RealField parent 
     534        parent = self._parent 
    518535        if PY_TYPE_CHECK(x, RealNumber): 
    519536            _x = x  # so we can get at x.value 
    520537            mpfr_set(self.value, _x.value, parent.rnd) 
    521         elif PY_TYPE_CHECK(x, sage.rings.rational.Rational): 
    522             n = parent(x.numerator()) 
    523             d = parent(x.denominator()) 
    524             mpfr_div(self.value, n.value, d.value, parent.rnd) 
    525         elif PY_TYPE_CHECK(x, int): 
    526             _ix = x 
    527             mpfr_set_si(self.value, _ix, parent.rnd) 
     538        elif PY_TYPE_CHECK(x, Integer): 
     539            mpfr_set_z(self.value, (<Integer>x).value, parent.rnd)             
     540        elif PY_TYPE_CHECK(x, Rational): 
     541            mpfr_set_q(self.value, (<Rational>x).value, parent.rnd) 
     542        elif isinstance(x, (int, long)): 
     543            _ix = Integer(x) 
     544            mpfr_set_z(self.value, _ix.value, parent.rnd) 
    528545        else: 
    529546            s = str(x).replace(' ','') 
Note: See TracChangeset for help on using the changeset viewer.