Ticket #8896: 8896-part2.patch

File 8896-part2.patch, 8.6 KB (added by robertwb, 9 years ago)

apply on top of previous

  • sage/rings/complex_interval.pyx

    # HG changeset patch
    # User Robert Bradshaw <robertwb@math.washington.edu>
    # Date 1274986081 25200
    # Node ID 1f34cd5a4eeee19c4efcd66c33a6f2be3264155a
    # Parent  e7e85a36d207c968c39ff85ded544cf89fabab6f
    #8896 - Better precision handling for real/complex literals.
    
    diff -r e7e85a36d207 -r 1f34cd5a4eee sage/rings/complex_interval.pyx
    a b  
    12121212        sage: ComplexIntervalFieldElement(1,2.000000000000000000000)
    12131213        1 + 2*I
    12141214        sage: ComplexIntervalFieldElement(1.234567890123456789012345, 5.4321098654321987654321)
    1215         1.234567890123456789012350? + 5.432109865432198765432000?*I
     1215        1.2345678901234567890123450? + 5.4321098654321987654321000?*I
    12161216    """
    12171217    if s_imag is None:
    12181218        s_imag = 0
  • sage/rings/complex_number.pyx

    diff -r e7e85a36d207 -r 1f34cd5a4eee sage/rings/complex_number.pyx
    a b  
    2828
    2929from complex_double cimport ComplexDoubleElement
    3030from real_mpfr cimport RealNumber
     31from real_mpfr import create_RealNumber
    3132
    3233import complex_field
    3334import sage.misc.misc
     
    18601861       
    18611862            sage: b = ComplexNumber(float(exp(42)),0)
    18621863            sage: b.log()
    1863             41.99999999999971
     1864            42.0000000000000
    18641865
    18651866        ::
    18661867       
     
    21522153
    21532154def create_ComplexNumber(s_real, s_imag=None, int pad=0, min_prec=53):
    21542155    r"""
     2156    This function is defined as ComplexNumber in the global namespace and
     2157    is used by the preparser to create complex literals.
     2158   
    21552159    Return the complex number defined by the strings s_real and
    21562160    s_imag as an element of ``ComplexField(prec=n)``,
    21572161    where n potentially has slightly more (controlled by pad) bits than
     
    21732177   
    21742178    EXAMPLES::
    21752179   
     2180        sage: 1.5j
     2181        1.50000000000000*I
     2182        sage: 1.50000000000000000000000000000000000000000000000000j
     2183        1.50000000000000000000000000000000000000000000000000*I
    21762184        sage: ComplexNumber('2.3')
    21772185        2.30000000000000
    21782186        sage: ComplexNumber('2.3','1.1')
     
    21822190        sage: ComplexNumber(10,10)
    21832191        10.0000000000000 + 10.0000000000000*I
    21842192        sage: ComplexNumber(1.000000000000000000000000000,2)
    2185         1.00000000000000000000000000 + 2.00000000000000000000000000*I
     2193        1.000000000000000000000000000 + 2.000000000000000000000000000*I
    21862194        sage: ComplexNumber(1,2.000000000000000000000)
    2187         1.00000000000000000000 + 2.00000000000000000000*I
     2195        1.000000000000000000000 + 2.000000000000000000000*I
    21882196   
    21892197    ::
    21902198   
    21912199        sage: sage.rings.complex_number.create_ComplexNumber(s_real=2,s_imag=1)
    21922200        2.00000000000000 + 1.00000000000000*I
    21932201    """
     2202    # Most common case (preparsed literal).
     2203    if s_real is 0:
     2204        if isinstance(s_imag, str) and len(s_imag) < 15 and pad == 0 and min_prec == 53:
     2205            return ComplexNumber(complex_field.ComplexField(53), 0, s_imag)
     2206        else:
     2207            imag = create_RealNumber(s_imag, 10, pad, "RNDN", min_prec)
     2208            return ComplexNumber(imag.parent().complex_field(), 0, imag)
     2209   
    21942210    if s_imag is None:
    2195         s_imag = 0
    2196        
    2197     if not isinstance(s_real, str):
    2198         s_real = str(s_real).strip()
    2199     if not isinstance(s_imag, str):
    2200         s_imag = str(s_imag).strip()
    2201     #if base == 10:
    2202     bits = max(int(3.32192*len(s_real)),int(3.32192*len(s_imag)))
    2203     #else:
    2204     #    bits = max(int(math.log(base,2)*len(s_imag)),int(math.log(base,2)*len(s_imag)))
    2205        
    2206     C = complex_field.ComplexField(prec=max(bits+pad, min_prec))
    2207        
    2208     return ComplexNumber(C, s_real, s_imag)
     2211        s_imag = 0.
     2212   
     2213    real = create_RealNumber(s_real, 10, pad, "RNDN", min_prec)
     2214    imag = create_RealNumber(s_imag, 10, pad, "RNDN", min_prec)
     2215
     2216    C = complex_field.ComplexField(prec=max(real.prec(), imag.prec(), min_prec))
     2217    return ComplexNumber(C, real, imag)
    22092218
    22102219
    22112220cdef class RRtoCC(Map):
  • sage/rings/rational_field.py

    diff -r e7e85a36d207 -r 1f34cd5a4eee sage/rings/rational_field.py
    a b  
    116116        sage: QQ(23.2, 2)
    117117        6530219459687219/281474976710656
    118118        sage: 6530219459687219.0/281474976710656
    119         23.20000000000000
     119        23.199999999999999
    120120        sage: a = 23.2; a
    121121        23.2000000000000
    122122        sage: QQ(a, 10)
  • sage/rings/real_mpfr.pyx

    diff -r e7e85a36d207 -r 1f34cd5a4eee sage/rings/real_mpfr.pyx
    a b  
    44834483
    44844484def create_RealNumber(s, int base=10, int pad=0, rnd="RNDN", int min_prec=53):
    44854485    r"""
    4486     Return the real number defined by the string s as an element of
     4486    This function is defined as RealNumber in the global namespace and
     4487    is used by the preparser to create real literals.
     4488   
     4489    Returns the real number defined by the string s as an element of
    44874490    ``RealField(prec=n)``, where n potentially has slightly
    4488     more (controlled by pad) bits than given by s.
     4491    more (controlled by pad) bits than given by the number of
     4492    significant figures of s.
    44894493   
    44904494    INPUT:
    44914495   
    44924496   
    4493     -  ``s`` - a string that defines a real number (or
     4497    -  `s` -- a string that defines a real number (or
    44944498       something whose string representation defines a number)
    44954499   
    4496     -  ``base`` - an integer between 2 and 36
    4497    
    4498     -  ``pad`` - an integer = 0.
    4499    
    4500     -  ``rnd`` - rounding mode: RNDN, RNDZ, RNDU, RNDD
    4501    
    4502     -  ``min_prec`` - number will have at least this many
    4503        bits of precision, no matter what.
     4500    -  `base` -- an integer (default 10) between 2 and 36
     4501   
     4502    -  `pad` -- an integer (default 0)
     4503   
     4504    -  `rnd` -- rounding mode (defa: RNDN, RNDZ, RNDU, RNDD
     4505   
     4506    -  `min_prec` -- (default 53) minimum bits of precision, no matter what.
    45044507   
    45054508   
    45064509    EXAMPLES::
     
    45164519        sage: (1.2).parent() is RR
    45174520        True
    45184521       
    4519     TESTS::
     4522   
     4523    Leading zeros don't contribute to the precision::
    45204524   
    45214525        sage: RealNumber('.000000000000000000000000000000001').prec()
    45224526        53
    45234527        sage: RealNumber('-.000000000000000000000000000000001').prec()
    45244528        53
    4525 
     4529   
     4530    Unless the value is exactly zero::
     4531
     4532        sage: RealNumber('.00000000000000000000000000000000000').prec()
     4533        120
     4534        sage: RealNumber('0.0000000000000000000000000000000000').prec()
     4535        120
     4536
     4537    TESTS:
     4538   
     4539    The placements of the decimal points and signs don't affect the precision::
     4540   
    45264541        sage: RealNumber('.11111111111111111111111111111111111').prec()
    45274542        120
    45284543        sage: RealNumber('1.1111111111111111111111111111111111').prec()
    45294544        120
    45304545        sage: RealNumber('11111111111111111111111111111111111.').prec()
    45314546        120
    4532         sage: RealNumber('.00000000000000000000000000000000000').prec()
    4533         120
    4534         sage: RealNumber('0.0000000000000000000000000000000000').prec()
    4535         120
     4547        sage: RealNumber('11111111111111111111111111111111111').prec()
     4548        120
     4549        sage: RealNumber('+11111111111111111111111111111111111').prec()
     4550        120
     4551        sage: RealNumber('-11111111111111111111111111111111111').prec()
     4552        120
     4553       
     4554    Make sure the the string representation agrees with the input::
     4555   
     4556        sage: s = 1.1111111111111111111111111111111111
     4557        sage: parent(s) is parent(RealNumber(str(s)))
     4558        True
    45364559    """
    45374560    cdef int i, sigfigs
    4538     if not isinstance(s, str):
     4561   
     4562    if isinstance(s, float):
     4563        return RealNumber(RR, s)
     4564    elif isinstance(s, RealNumber):
     4565        return s
     4566    elif not isinstance(s, str):
    45394567        s = str(s)
    45404568       
    4541     if base == 10 and min_prec == 53 and len(s) <= 15:
     4569    if base >= 10 and min_prec == 53 and len(s) <= 15 and pad == 0:
     4570        # Most common case by far.
    45424571        R = RR
    45434572   
    45444573    else:
     
    45564585        for i in range(len(mantissa)):
    45574586            if mantissa[i] == '0':
    45584587                sigfigs -= 1
    4559             elif mantissa[i] != '.' and mantissa[i] != '-':
     4588            elif mantissa[i] not in ".+-":
    45604589                break
    45614590        else:
    45624591            # Must be 0.00000000000000...0
    45634592            sigfigs = len(mantissa)
    45644593
    4565         if mantissa[0] == '-':
     4594        if mantissa[0] in '-+':
    45664595            sigfigs -= 1
     4596        if '.' in mantissa:
     4597            sigfigs -= 1
     4598        # This is so the input matches with the string representation.
     4599        sigfigs += 1
    45674600       
    45684601        if base == 10:
    45694602            bits = int(3.32192*sigfigs)+1