import torch


def gumbel_log_survival(x):
    """Computes log P(g > x) = log(1 - P(g < x)) = log(1 - exp(-exp(-x))) for a standard Gumbel"""
    y = torch.exp(-x)
    return torch.where(
        x
        >= 10,  # means that y < 1e-4 so O(y^6) <= 1e-24 so we can use series expansion
        -x
        - y / 2
        + y ** 2 / 24
        - y ** 4
        / 2880,  # + O(y^6), https://www.wolframalpha.com/input/?i=log(1+-+exp(-y))
        torch.log(-torch.expm1(-torch.exp(-x))),  # Hope for the best
    )