-2

Due to rounding error, cannot get mean of three numbers:

a=-1.11e4 
b=-1.12e4
c=-1.13e4
Mean=1/3 *[exp(a)+exp(b)+exp(c)]

How to get the results in a log value?

ASE
  • 1,702
  • 2
  • 21
  • 29

1 Answers1

1

You're trying to find log((exp(a) + exp(b) + exp(c)) / 3), but a, b, and c are so low that the result of exp underflows to 0. You can fix this by adjusting the values so exp doesn't underflow.

Let d = max(a, b, c). Then we have the following equality:

M = log((exp(a) + exp(b) + exp(c)) / 3)
  = log(exp(d) * (exp(a-d) + exp(b-d) + exp(c-d)) / 3)
  = log(exp(d)) + log((exp(a-d) + exp(b-d) + exp(c-d)) / 3)
  = d + log((exp(a-d) + exp(b-d) + exp(c-d)) / 3)

So we can calculate the result as d + log((exp(a-d) + exp(b-d) + exp(c-d)) / 3). Since d is equal to one of a, b, or c, one of the exp arguments is 0, and the rest are at most 0. Thus, one of the exp outputs is 1, and the rest are at most 1. We don't have to worry about overflow or underflow; while an underflow might still occur in one or more exp calls, it won't be a problem any more, since the log argument won't be 0.

user2357112
  • 260,549
  • 28
  • 431
  • 505