Integrand produced a NaN. Domain error using QuadGK

You can avoid the NaN problem if you refactor log(2*cosh(z)) in a way that avoids spurious overflow via the identity (for real z): \ln(2\cosh(z)) = |z| + \ln(1 + e^{-2|z|}):

log2cosh(z) = let za = abs(z); za + log1p(exp(-2za)); end

Then you get:

julia> quadgk(z -> exp(-z^2/2) * log2cosh(z), -Inf, Inf)
(2.676363074319933, 1.5888091489219617e-8)
1 Like