# Log(0) and Stan Arithmetic

I have been making frequent use of log_sum_exp() and log_diff_exp() in my modelling. But the following struck me as curious:

log(0) = -inf
log_sum_exp(log(1), log(0)) = 0
log_diff_exp(log(1), log(0)) = 0
log_sum_exp(log(0), log(0)) = nan
log_diff_exp(log(0), log(0)) = nan
log_diff_exp(log(1), log(1)) = nan

In the first three cases, Stan properly treats log(0) = -inf as a number and allows arithmetic to be performed.

For the last three cases I would also expect that the proper return should also be -inf, but instead we get nan. Iâ€™ve gotten around this for my immediate purpose by writing a custom function, but Iâ€™m wondering if this should be â€śfixedâ€ť on the developer side.

1 Like

I think arithmetic on infs can turn into NaNs (for instance â€śNaNs are produced by these operationsâ€ť here: https://www.doc.ic.ac.uk/~eedwards/compsys/float/nan.html).

I agree the last one should probably be an inf, but is there a way to avoid infs in your code? Depending on infs seems really fragile, cause Iâ€™m sure there are a lot more confusing examples like the ones you brought up (and who knows what the autodiff will do).

I encountered it when I was â€śslicingâ€ť a (log)normal CDF. The last case, log(1 - 1), is what I had in mind when I asked the question.

I think this should replicate that last case:
log_diff_exp(normal_lcdf(9, 1, 1), normal_lcdf(8.5, 1, 1));

I now have code to catch it when it occurs in my model. And I suppose I could also try log(normal_cdf() - normal_cdf()).

Thatâ€™s a bug. I filed an issue:

Hopefully weâ€™ll get that fixed by 2.18 (no quotes necessary!).

We ran into a similar issue with our truncation that I fixed by doing what you suggest, `log(normal_cdf() - normal_cdf())` (and @bgoodri suggested for the patch).

1 Like

Iâ€™m back at it. In Rstan 2.19.3 I get:
log(0) * 0 = nan
0^0 = 1
log(0^0) = 0

Seems like the first case should also be 0. Infinity times zero is zero, yes?

(Patiently waiting for passionate mathematical debate to follow my concluding innocent question).

1 Like

Nope. Not in general. In measure theory, yes.