Skip to content
Snippets Groups Projects
Commit 85e89fb9 authored by Daniel Waggoner's avatar Daniel Waggoner
Browse files

fixed AddScaledLogs() so zero weights can be safely passed

(cherry picked from commit fc074f98b74acb38d66dcd6e32ee0a5836e930e2)
parent 86ed91b8
No related branches found
No related tags found
No related merge requests found
......@@ -34,14 +34,19 @@ PRECISION AddLogs(PRECISION a, PRECISION b)
/*
Returns ln(x*exp(a) + y*exp(b)) computed to avoid overflow. If a = ln(c) and
b = ln(d), as is usually the case, then the routine returns ln(x*c + y*d).
Assumes that x*exp(a) + y*exp(b) is positive, but no checking is done. This
condition will always be satisfied if both x and y are positive.
b = ln(d), as is usually the case, then the routine returns ln(x*c + y*d).
Both x and y must be non-negative. If either x or y is negative, it is
treated as if it were zero.
*/
PRECISION AddScaledLogs(PRECISION x, PRECISION a, PRECISION y, PRECISION b)
{
return (a > b) ? a + log(x + y*exp(b-a)) : b + log(x*exp(a-b) + y);
if (x > 0)
if (y > 0)
return (a > b) ? a + log(x + y*exp(b-a)) : b + log(x*exp(a-b) + y);
else
return log(x) + a;
else
return (y > 0) ? log(y) + b : MINUS_INFINITY;
}
/*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment