def gamma_above(a, x):
    assert 0. < a               # XXX NaN?
    assert 0. <= x              # XXX NaN?
    if x <= max(1., a):
        return 1. - gamma_below(a, x)

    # m = \exp [a \log x - x - \log \Gamma(a)] = x^a e^{-x} / \Gamma(a)
    w = a*math.log(x) - x - math.lgamma(a)
    if w < -MAXLOG:
        return 0.
    m = ieee_exp(w)

    def contfrac():
        yield 1, x              # 1/(x + ...), without division.
        i = 0
        while True:
            if (i % 2) == 0:
                yield (i//2) + 1 - a, 1
            else:
                yield (i + 1)//2, x
            i += 1

    return m*limit(convergents(contfrac()))
Convert HTML text to plaintext
dataset/ETHPy150Open probcomp/bayeslite/src/math_util.py/gamma_above--crossover with--dataset/ETHPy150Open kiberpipa/Intranet/intranet/org/templatetags/box.py/html2text