class_docstr: Context for multiprecision arithmetic with a global precision.
class_docstr_tok: ["Context","for","multiprecision","arithmetic","with","a","global","precision","."]
docstr: 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 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
docstr_tok: ["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","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"]
code: 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 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)
  
imports: import functools
  import re
  from .ctx_base import StandardBaseContext
  from .libmp.backend import basestring, BACKEND
  from .None import libmp
  from .libmp import MPZ, MPZ_ZERO, MPZ_ONE, int_types, repr_dps, round_floor, round_ceiling, dps_to_prec, round_nearest, prec_to_dps, ComplexResult, to_pickable, from_pickable, normalize, from_int, from_float, from_str, to_int, to_float, to_str, from_rational, from_man_exp, fone, fzero, finf, fninf, fnan, mpf_abs, mpf_pos, mpf_neg, mpf_add, mpf_sub, mpf_mul, mpf_mul_int, mpf_div, mpf_rdiv_int, mpf_pow_int, mpf_mod, mpf_eq, mpf_cmp, mpf_lt, mpf_gt, mpf_le, mpf_ge, mpf_hash, mpf_rand, mpf_sum, bitcount, to_fixed, mpc_to_str, mpc_to_complex, mpc_hash, mpc_pos, mpc_is_nonzero, mpc_neg, mpc_conjugate, mpc_abs, mpc_add, mpc_add_mpf, mpc_sub, mpc_sub_mpf, mpc_mul, mpc_mul_mpf, mpc_mul_int, mpc_div, mpc_div_mpf, mpc_pow, mpc_pow_mpf, mpc_pow_int, mpc_mpf_div, mpf_pow, mpf_pi, mpf_degree, mpf_e, mpf_phi, mpf_ln2, mpf_ln10, mpf_euler, mpf_catalan, mpf_apery, mpf_khinchin, mpf_glaisher, mpf_twinprime, mpf_mertens, int_types
  from .None import function_docs
  from .None import rational
  from .ctx_mp_python import _mpf, _mpc, mpnumeric
label_desc: Use image node_id 42 for calling the MPContext obj's underlying member method code with example usage: obj.nint_distance(ctx, x) and returns: n,  max, int,  ctx, n,  d, ctx, n,  ctx