Ticket #5199: pynac_is_integer.patch

File pynac_is_integer.patch, 2.0 KB (added by cwitty, 12 years ago)
  • sage/symbolic/pynac.pyx

    # HG changeset patch
    # User Carl Witty <cwitty@newtonlabs.com>
    # Date 1233983604 28800
    # Node ID 2d3519235ac5a8d41fac7cef9d70907383a223c5
    # Parent  eef7a91abded904b1b4f9b38277bc37cecec1461
    Adjust pynac py_is_integer to stop treating floating-point numbers as integers
    
    diff -r eef7a91abded -r 2d3519235ac5 sage/symbolic/pynac.pyx
    a b  
    1616include "../ext/stdsage.pxi"
    1717include "../libs/ginac/decl.pxi"
    1818
     19from sage.structure.element import Element
    1920from sage.rings.integer_ring import ZZ
    2021from sage.rings.integer cimport Integer
    2122from sage.rings.real_mpfr import RR, RealField
    2223from sage.rings.all import CC
     24from sage.calculus.all import SR
    2325
    2426from sage.symbolic.expression cimport Expression, new_Expression_from_GEx
    2527
     
    309311
    310312
    311313cdef public bint py_is_integer(object x):
    312     #return PY_TYPE_CHECK_EXACT(x, Integer) or\
    313     #       IS_INSTANCE(x, int) or IS_INSTANCE(x, long)
    314     return (x in ZZ)
     314    r"""
     315    Returns True if pynac should treat this object as an integer.
     316
     317    EXAMPLES:
     318        sage: from sage.symbolic.pynac import py_is_integer_for_doctests
     319        sage: py_is_integer = py_is_integer_for_doctests
     320
     321        sage: py_is_integer(1r)
     322        True
     323        sage: py_is_integer(long(1))
     324        True
     325        sage: py_is_integer(3^57)
     326        True
     327        sage: py_is_integer(SR(5))
     328        True
     329        sage: py_is_integer(4/2)
     330        True
     331        sage: py_is_integer(QQbar(sqrt(2))^2)
     332        True
     333        sage: py_is_integer(3.0)
     334        False
     335        sage: py_is_integer(3.0r)
     336        False
     337    """
     338    return IS_INSTANCE(x, int) or IS_INSTANCE(x, long) or \
     339           (IS_INSTANCE(x, Element) and
     340            (x.parent().is_exact() or x.parent() == SR) and
     341            (x in ZZ))
     342
     343def py_is_integer_for_doctests(x):
     344    return py_is_integer(x)
    315345
    316346cdef public bint py_is_real(object a):
    317347    return py_imag(a) == 0