def exp_safe(x):
    """
    Compute exponentials which safely underflow to zero.

    Slow, but convenient to use. Note that numpy provides proper
    floating point exception handling with access to the underlying
    hardware.
    """

    if type(x) is np.ndarray:
        return exp(np.clip(x,exp_safe_MIN,exp_safe_MAX))
    else:
        return math.exp(x)