import math


def logsumexp2(v1, v2):
    """Computes log(exp(v1) + exp(v2)) in a numerically stable manner.
    """
    if v1 < v2:
        return v2 + math.log(1.0 + math.exp(v1 - v2))
    else:
        return v1 + math.log(1.0 + math.exp(v2 - v1))