(ctx, x)
return n, max(re_dist, im_dist)
r"""
        Return `(n,d)` where `n` is the nearest integer to `x` and `d` is
        an estimate of `\log_2(|x-n|)`. If `d < 0`, `-d` gives the precision
        (measured in bits) lost to cancellation when computing `x-n`.

            >>> from sympy.mpmath import *
            >>> n, d = nint_distance(5)
            >>> print(n); print(d)
            5
            -inf
            >>> n, d = nint_distance(mpf(5))
            >>> print(n); print(d)
            5
            -inf
            >>> n, d = nint_distance(mpf(5.00000001))
            >>> print(n); print(d)
            5
            -26
            >>> n, d = nint_distance(mpf(4.99999999))
            >>> print(n); print(d)
            5
            -26
            >>> n, d = nint_distance(mpc(5,10))
            >>> print(n); print(d)
            5
            4
            >>> n, d = nint_distance(mpc(5,0.000001))
            >>> print(n); print(d)
            5
            -19
r"""
        Return `(n,d)` where `n` is the nearest integer to `x` and `d` is
        an estimate of `\log_2(|x-n|)`. If `d < 0`, `-d` gives the precision
        (measured in bits) lost to cancellation when computing `x-n`.
r Return ( n d ) where n is the nearest integer to x and d is an estimate of \ log_2 ( |x - n| ) . If d < 0 - d gives the precision ( measured in bits ) lost to cancellation when computing x - n .
def nint_distance(ctx, x):
        r"""
        Return `(n,d)` where `n` is the nearest integer to `x` and `d` is
        an estimate of `\log_2(|x-n|)`. If `d < 0`, `-d` gives the precision
        (measured in bits) lost to cancellation when computing `x-n`.

            >>> from sympy.mpmath import *
            >>> n, d = nint_distance(5)
            >>> print(n); print(d)
            5
            -inf
            >>> n, d = nint_distance(mpf(5))
            >>> print(n); print(d)
            5
            -inf
            >>> n, d = nint_distance(mpf(5.00000001))
            >>> print(n); print(d)
            5
            -26
            >>> n, d = nint_distance(mpf(4.99999999))
            >>> print(n); print(d)
            5
            -26
            >>> n, d = nint_distance(mpc(5,10))
            >>> print(n); print(d)
            5
            4
            >>> n, d = nint_distance(mpc(5,0.000001))
            >>> print(n); print(d)
            5
            -19

        """
        typx = type(x)
        if typx in int_types:
            return int(x), ctx.ninf
        elif typx is rational.mpq:
            p, q = x._mpq_
            n, r = divmod(p, q)
            if 2*r >= q:
                n += 1
            elif not r:
                return n, ctx.ninf
            # log(p/q-n) = log((p-nq)/q) = log(p-nq) - log(q)
            d = bitcount(abs(p-n*q)) - bitcount(q)
            return n, d
        if hasattr(x, "_mpf_"):
            re = x._mpf_
            im_dist = ctx.ninf
        elif hasattr(x, "_mpc_"):
            re, im = x._mpc_
            isign, iman, iexp, ibc = im
            if iman:
                im_dist = iexp + ibc
            elif im == fzero:
                im_dist = ctx.ninf
            else:
                raise ValueError("requires a finite number")
        else:
            x = ctx.convert(x)
            if hasattr(x, "_mpf_") or hasattr(x, "_mpc_"):
                return ctx.nint_distance(x)
            else:
                raise TypeError("requires an mpf/mpc")
        sign, man, exp, bc = re
        mag = exp+bc
        # |x| < 0.5
        if mag < 0:
            n = 0
            re_dist = mag
        elif man:
            # exact integer
            if exp >= 0:
                n = man << exp
                re_dist = ctx.ninf
            # exact half-integer
            elif exp == -1:
                n = (man>>1)+1
                re_dist = 0
            else:
                d = (-exp-1)
                t = man >> d
                if t & 1:
                    t += 1
                    man = (t<<d) - man
                else:
                    man -= (t<<d)
                n = t>>1   # int(t)>>1
                re_dist = exp+bitcount(man)
            if sign:
                n = -n
        elif re == fzero:
            re_dist = ctx.ninf
            n = 0
        else:
            raise ValueError("requires a finite number")
        return n, max(re_dist, im_dist)
def nint_distance ( ctx , x ) : typx = type ( x ) if typx in int_types : return int ( x ) , ctx . ninf elif typx is rational . mpq : p , q = x . _mpq_ n , r = divmod ( p , q ) if 2 * r >= q : n += 1 elif not r : return n , ctx . ninf # log(p/q-n) = log((p-nq)/q) = log(p-nq) - log(q) d = bitcount ( abs ( p - n * q ) ) - bitcount ( q ) return n , d if hasattr ( x , "_mpf_" ) : re = x . _mpf_ im_dist = ctx . ninf elif hasattr ( x , "_mpc_" ) : re , im = x . _mpc_ isign , iman , iexp , ibc = im if iman : im_dist = iexp + ibc elif im == fzero : im_dist = ctx . ninf else : raise ValueError ( "requires a finite number" ) else : x = ctx . convert ( x ) if hasattr ( x , "_mpf_" ) or hasattr ( x , "_mpc_" ) : return ctx . nint_distance ( x ) else : raise TypeError ( "requires an mpf/mpc" ) sign , man , exp , bc = re mag = exp + bc # |x| < 0.5 if mag < 0 : n = 0 re_dist = mag elif man : # exact integer if exp >= 0 : n = man << exp re_dist = ctx . ninf # exact half-integer elif exp == - 1 : n = ( man >> 1 ) + 1 re_dist = 0 else : d = ( - exp - 1 ) t = man >> d if t & 1 : t += 1 man = ( t << d ) - man else : man -= ( t << d ) n = t >> 1 # int(t)>>1 re_dist = exp + bitcount ( man ) if sign : n = - n elif re == fzero : re_dist = ctx . ninf n = 0 else : raise ValueError ( "requires a finite number" ) return n , max ( re_dist , im_dist )