Ticket #10761: trac_10761_AlgebraicNumber_numerical_approx.patch

File trac_10761_AlgebraicNumber_numerical_approx.patch, 1.5 KB (added by spice, 10 years ago)

Replaces existing patch.

  • sage/misc/functional.py

    # HG changeset patch
    # User Simon Spicer <mlungu@uw.edu>
    # Date 1300915138 25200
    # Node ID 56681ee951d826097119ebf4faa1114e4d863e6e
    # Parent  361a4ad7d52c69b64ae2e658ffd0820af0d87e93
    Ticket 10761: fixes numerical_approx() in misc/functional.py so that it works on AlgebraicNumbers
    
    diff -r 361a4ad7d52c -r 56681ee951d8 sage/misc/functional.py
    a b  
    12461246
    12471247        sage: type(numerical_approx(CC(1/2)))
    12481248        <type 'sage.rings.complex_number.ComplexNumber'>
     1249
     1250    The following tests Trac 10761, in which n() would break when
     1251    called on complex-valued AlgebraicNumbers::
     1252
     1253        sage: E = matrix(3, [3,1,6,5,2,9,7,3,13]).eigenvalues(); E
     1254        [18.16815365088822?, -0.08407682544410650? - 0.2190261484802906?*I, -0.08407682544410650? + 0.2190261484802906?*I]
     1255        sage: E[1].parent()
     1256        Algebraic Field
     1257        sage: [a.n() for a in E]
     1258        [18.1681536508882, -0.0840768254441065 - 0.219026148480291*I, -0.0840768254441065 + 0.219026148480291*I]
    12491259    """
    12501260    if prec is None:
    12511261        if digits is None:
     
    12601270        if not (is_ComplexNumber(x) or is_ComplexDoubleElement(x)):
    12611271            try:
    12621272                return sage.rings.real_mpfr.RealField(prec)(x)
    1263             except TypeError:
     1273            # Trac 10761: now catches ValueErrors as well as TypeErrors
     1274            except (TypeError, ValueError):
    12641275                pass
    12651276        return sage.rings.complex_field.ComplexField(prec)(x)
    12661277