Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, <code>exp(log_b)</code> is so much smaller than <code>exp(log_a)</code> that you can completely ignore that term and still get the correct answer with respect to double-precision:</p> <pre><code>exp(log_a) = 2.845550077506*10^-714 exp(log_b) = 4.05118588390*10^-1663 </code></pre> <p>If you are actually trying to compute <code>(exp(log_a) + exp(log_b)) / 2</code>, the answer would underflow to zero anyways. So it wouldn't really matter unless you're trying to take another logarithm at the end.</p> <p>If you're trying compute:</p> <pre><code>log((exp(log_a) + exp(log_b)) / 2) </code></pre> <p>Your best bet is to examine the difference between <code>log_a</code> and <code>log_b</code>. If the difference is large, then simply take the final value as equal to the larger term - log(2) since the smaller term will be small enough to completely vanish.</p> <p>EDIT:</p> <p>So your final algorithm could look like this:</p> <ol> <li>Check the magnitudes. If <code>abs(log_a - log_b) &gt; 800</code>. Return <code>max(log_a,log(b)) - log(2)</code>.</li> <li>Check either magnitude (they will be close together at this point.). If it is much larger or smaller than 1, add/subtract a constant from both <code>log_a</code> and <code>log_b</code>.</li> <li>Perform the calculation.</li> <li>If the values were scaled in step 2. Scale the result back.</li> </ol> <p>EDIT 2:</p> <p>Here's an even better solution:</p> <pre><code>if (log_a &gt; log_b) return log_a + log(1 + exp(log_b - log_a)) - log(2) else return log_b + log(1 + exp(log_a - log_b)) - log(2) </code></pre> <p>This will work if <code>log_a</code> and <code>log_b</code> are not too large or are negative.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload