func logaddexp(a, b float64) float64 { return a + log1exp(b-a)}
func log1exp(x float64) float64 { if x > 35 { return x } if x < -10 { return math.Exp(x) } return math.Log1p(math.Exp(x))}
// logaddexp performs log(exp(x) + exp(y))
func logaddexp(x, y float64) float64 {
tmp := x - y
if tmp > 0 {
return x + math.Log1p(math.Exp(-tmp))
} else if tmp <= 0 {
return y + math.Log1p(math.Exp(tmp))
} else {
// Nans, or infinities of the same sign involved
log.Printf("logaddexp %f %f", x, y)
return x + y
}
}
Thanks for your answers and inspirations, though.