code: def mpf_cos_sin(x, prec, rnd=round_fast, which=0, pi=False):
      """
      which:
      0 -- return cos(x), sin(x)
      1 -- return cos(x)
      2 -- return sin(x)
      3 -- return tan(x)
  
      if pi=True, compute for pi*x
      """
      sign, man, exp, bc = x
      if not man:
          if exp:
              c, s = fnan, fnan
          else:
              c, s = fone, fzero
          if which == 0:
              return c, s
          if which == 1:
              return c
          if which == 2:
              return s
          if which == 3:
              return s
  
      mag = bc + exp
      wp = prec + 10
  
      # Extremely small?
      if mag < 0:
          if mag < -wp:
              if pi:
                  x = mpf_mul(x, mpf_pi(wp))
              c = mpf_perturb(fone, 1, prec, rnd)
              s = mpf_perturb(x, 1 - sign, prec, rnd)
              if which == 0:
                  return c, s
              if which == 1:
                  return c
              if which == 2:
                  return s
              if which == 3:
                  return mpf_perturb(x, sign, prec, rnd)
      if pi:
          if exp >= -1:
              if exp == -1:
                  c = fzero
                  s = (fone, fnone)[bool(man & 2) ^ sign]
              elif exp == 0:
                  c, s = (fnone, fzero)
              else:
                  c, s = (fone, fzero)
              if which == 0:
                  return c, s
              if which == 1:
                  return c
              if which == 2:
                  return s
              if which == 3:
                  return mpf_div(s, c, prec, rnd)
          # Subtract nearest half-integer (= mod by pi/2)
          n = ((man >> (-exp - 2)) + 1) >> 1
          man = man - (n << (-exp - 1))
          mag2 = bitcount(man) + exp
          wp = prec + 10 - mag2
          offset = exp + wp
          if offset >= 0:
              t = man << offset
          else:
              t = man >> (-offset)
          t = (t * pi_fixed(wp)) >> wp
      else:
          t, n, wp = mod_pi2(man, exp, mag, wp)
      c, s = cos_sin_basecase(t, wp)
      m = n & 3
      if m == 1:
          c, s = -s, c
      elif m == 2:
          c, s = -c, -s
      elif m == 3:
          c, s = s, -c
      if sign:
          s = -s
      if which == 0:
          c = from_man_exp(c, -wp, prec, rnd)
          s = from_man_exp(s, -wp, prec, rnd)
          return c, s
      if which == 1:
          return from_man_exp(c, -wp, prec, rnd)
      if which == 2:
          return from_man_exp(s, -wp, prec, rnd)
      if which == 3:
          return from_rational(s, c, prec, rnd)
  
imports: import math
  from bisect import bisect
  from .backend import xrange
  from .backend import MPZ, MPZ_ZERO, MPZ_ONE, MPZ_TWO, MPZ_FIVE, BACKEND
  from .libmpf import round_floor, round_ceiling, round_down, round_up, round_nearest, round_fast, ComplexResult, bitcount, bctable, lshift, rshift, giant_steps, sqrt_fixed, from_int, to_int, from_man_exp, to_fixed, to_float, from_float, from_rational, normalize, fzero, fone, fnone, fhalf, finf, fninf, fnan, mpf_cmp, mpf_sign, mpf_abs, mpf_pos, mpf_neg, mpf_add, mpf_sub, mpf_mul, mpf_div, mpf_shift, mpf_rdiv_int, mpf_pow_int, mpf_sqrt, reciprocal_rnd, negative_rnd, mpf_perturb, isqrt_fast
  from .libintmath import ifib
label_desc: Use image node_id 47 for calling a global function with example usage: mpf_cos_sin(x, prec, rnd, which, pi) and returns: c,  s, from_man_exp, from_man_exp, from_rational, c,  s, c, s, s, c,  s, c, s, mpf_perturb, c,  s, c, s, mpf_div