def convergents(contfrac):
    """Convergents of a continued fraction.

    contfrac must yield an infinite sequence (n0, d0), (n1, d1), (n2,
    d2), ..., representing the continued fraction

                n0
        ------------------
                  n1
        d0 + -------------
                     n2
             d1 + --------
                  d2 + ...

    The kth convergent is the continued fraction truncated at the kth
    term, i.e. with zero instead of the rest in the ellipsis.
    """
    cs = continuants(contfrac)
    p0, q0 = cs.next()
    yield p0/q0
    for p, q in cs:
        if p == 0:
            break
        yield p/q
        p0, q0 = p, q
    c = p0/q0
    while True:
        yield c
dataset/ETHPy150Open probcomp/bayeslite/src/math_util.py/convergents/original