Multiplying two numbers in log space means adding them:
log_multiply(x, y) = log( exp(x) * exp(y) )
= x + y
Adding two numbers in log space means you do a special log-add operation:
log_add(x, y) = log( exp(x) + exp(y) )
which is implemented in the following code, in a way that doesn't require us to take the two exponentials (and lose runtime speed and precision):
double log_add(double x, double y) {
if(x == neginf)
return y;
if(y == neginf)
return x;
return max(x, y) + log1p(exp( -fabs(x - y) ));
}
(Here is another one.)
But here is the question:
Is there a trick to do it for subtraction as well?
log_subtract(x, y) = log( exp(x) - exp(y) )
without having to take the exponents and lose precision?
double log_subtract(double x, double y) {
// ?
}