def METHOD_NAME(self, s, limit=True):
    p, q = METHOD_NAME(s._mpf_)
    if not limit or q <= self.max_denom:
        return p, q
    p0, q0, p1, q1 = 0, 1, 1, 0
    n, d = p, q
    while True:
        a = n//d
        q2 = q0 + a*q1
        if q2 > self.max_denom:
            break
        p0, q0, p1, q1 = p1, q1, p0 + a*p1, q2
        n, d = d, n - a*d
    k = (self.max_denom - q0)//q1
    number = Fraction(p, q)
    bound1 = Fraction(p0 + k*p1, q0 + k*q1)
    bound2 = Fraction(p1, q1)
    if not bound2 or not bound1:
        return p, q
    if abs(bound2 - number) <= abs(bound1 - number):
        return bound2.as_integer_ratio()
    return bound1.as_integer_ratio()