code: def _calc_p_continue(desired_avg: float, max_size: int) -> float:
      """Return the p_continue which will generate the desired average size."""
      assert desired_avg <= max_size, (desired_avg, max_size)
      if desired_avg == max_size:
          return 1.0
      p_continue = 1 - 1.0 / (1 + desired_avg)
      if p_continue == 0 or max_size == float("inf"):
          assert 0 <= p_continue < 1, p_continue
          return p_continue
      assert 0 < p_continue < 1, p_continue
      # For small max_size, the infinite-series p_continue is a poor approximation,
      # and while we can't solve the polynomial a few rounds of iteration quickly
      # gets us a good approximate solution in almost all cases (sometimes exact!).
      while _p_continue_to_avg(p_continue, max_size) > desired_avg:
          # This is impossible over the reals, but *can* happen with floats.
          p_continue -= 0.0001
          # If we've reached zero or gone negative, we want to break out of this loop,
          # and do so even if we're on a system with the unsafe denormals-are-zero flag.
          # We make that an explicit error in st.floats(), but here we'd prefer to
          # just get somewhat worse precision on collection lengths.
          if p_continue < SMALLEST_POSITIVE_FLOAT:
              p_continue = SMALLEST_POSITIVE_FLOAT
              break
      # Let's binary-search our way to a better estimate!  We tried fancier options
      # like gradient descent, but this is numerically stable and works better.
      hi = 1.0
      while (
          desired_avg - _p_continue_to_avg(p_continue, max_size) > 0.01
      ):
          assert 0 < p_continue < hi, (p_continue, hi)
          mid = (p_continue + hi) / 2
          if _p_continue_to_avg(mid, max_size) <= desired_avg:
              p_continue = mid
          else:
              hi = mid
      assert 0 < p_continue < 1, p_continue
      assert _p_continue_to_avg(p_continue, max_size) <= desired_avg
      return p_continue
  
imports: import enum
  import hashlib
  import heapq
  import sys
  from collections import OrderedDict, abc
  from functools import lru_cache
  from typing import TYPE_CHECKING, List, Optional, Sequence, Tuple, Type, TypeVar, Union
  from hypothesis.errors import InvalidArgument
  from hypothesis.internal.compat import int_from_bytes
  from hypothesis.internal.floats import next_up
label_desc: Use image node_id 6 for calling a global function with example usage: _calc_p_continue(desired_avg, max_size) and returns: p_continue, int, p_continue