(expr, no, options, return_ints=False)
return re, im, re_acc, im_acc
With no = 1, computes ceiling(expr)
    With no = -1, computes floor(expr)

    Note: this function either gives the exact result or signals failure.
With no = 1, computes ceiling(expr)
    With no = -1, computes floor(expr)
With no = 1 computes ceiling ( expr ) With no = - 1 computes floor ( expr )
def get_integer_part(expr, no, options, return_ints=False):
    """
    With no = 1, computes ceiling(expr)
    With no = -1, computes floor(expr)

    Note: this function either gives the exact result or signals failure.
    """

    # The expression is likely less than 2^30 or so
    assumed_size = 30
    ire, iim, ire_acc, iim_acc = evalf(expr, assumed_size, options)

    # We now know the size, so we can calculate how much extra precision
    # (if any) is needed to get within the nearest integer
    if ire and iim:
        gap = max(fastlog(ire) - ire_acc, fastlog(iim) - iim_acc)
    elif ire:
        gap = fastlog(ire) - ire_acc
    elif iim:
        gap = fastlog(iim) - iim_acc
    else:
        # ... or maybe the expression was exactly zero
        return None, None, None, None

    margin = 10

    if gap >= -margin:
        ire, iim, ire_acc, iim_acc = \
            evalf(expr, margin + assumed_size + gap, options)

    # We can now easily find the nearest integer, but to find floor/ceil, we
    # must also calculate whether the difference to the nearest integer is
    # positive or negative (which may fail if very close).
    def calc_part(expr, nexpr):
        nint = int(to_int(nexpr, rnd))
        expr = C.Add(expr, -nint, evaluate=False)
        x, _, x_acc, _ = evalf(expr, 10, options)
        try:
            check_target(expr, (x, None, x_acc, None), 3)
        except PrecisionExhausted:
            if not expr.equals(0):
                raise PrecisionExhausted
            x = fzero
        nint += int(no*(mpf_cmp(x or fzero, fzero) == no))
        nint = from_int(nint)
        return nint, fastlog(nint) + 10

    re, im, re_acc, im_acc = None, None, None, None

    if ire:
        re, re_acc = calc_part(C.re(expr, evaluate=False), ire)
    if iim:
        im, im_acc = calc_part(C.im(expr, evaluate=False), iim)

    if return_ints:
        return int(to_int(re or fzero)), int(to_int(im or fzero))
    return re, im, re_acc, im_acc
def get_integer_part ( expr , no , options , return_ints = False ) : # The expression is likely less than 2^30 or so assumed_size = 30 ire , iim , ire_acc , iim_acc = evalf ( expr , assumed_size , options ) # We now know the size, so we can calculate how much extra precision # (if any) is needed to get within the nearest integer if ire and iim : gap = max ( fastlog ( ire ) - ire_acc , fastlog ( iim ) - iim_acc ) elif ire : gap = fastlog ( ire ) - ire_acc elif iim : gap = fastlog ( iim ) - iim_acc else : # ... or maybe the expression was exactly zero return None , None , None , None margin = 10 if gap >= - margin : ire , iim , ire_acc , iim_acc = evalf ( expr , margin + assumed_size + gap , options ) # We can now easily find the nearest integer, but to find floor/ceil, we # must also calculate whether the difference to the nearest integer is # positive or negative (which may fail if very close). def calc_part ( expr , nexpr ) : nint = int ( to_int ( nexpr , rnd ) ) expr = C . Add ( expr , - nint , evaluate = False ) x , _ , x_acc , _ = evalf ( expr , 10 , options ) try : check_target ( expr , ( x , None , x_acc , None ) , 3 ) except PrecisionExhausted : if not expr . equals ( 0 ) : raise PrecisionExhausted x = fzero nint += int ( no * ( mpf_cmp ( x or fzero , fzero ) == no ) ) nint = from_int ( nint ) return nint , fastlog ( nint ) + 10 re , im , re_acc , im_acc = None , None , None , None if ire : re , re_acc = calc_part ( C . re ( expr , evaluate = False ) , ire ) if iim : im , im_acc = calc_part ( C . im ( expr , evaluate = False ) , iim ) if return_ints : return int ( to_int ( re or fzero ) ) , int ( to_int ( im or fzero ) ) return re , im , re_acc , im_acc