Actually, I don't think it's 'annoyingly vague'. The example code appears to to compute a value that can be computed in different ways, most of which are simpler and/or take less code to represent. I take the question to mean "What are the benefits of doing it this way as opposed to the more obvious ways?"
–
johneJul 21 '10 at 6:10

Computers and logs don't always get along. As has been alluded to by others, accuracy becomes a real problem. This blog post goes a long way toward explaining this phenomenon. The article is about seemingly unnecessary library functions, and why they are actually very handy.

The function log1p computes log(1 + x). How hard could this be to implement?

There are all sorts of crazy rules and transforms you can use when dealing with logs/exponentials. What I'm guessing is that the author used some of these rules to either make the calculation more accurate, more efficient, or both.

If you mean as opposed to log(exp(log_a) + exp(log_b)), then the benefit is pretty clear; the way you mention only has to calculate one log and one exp, while this way has to calculate two exps. That is far more costly than an extra addition/subtraction/if test.

Numbers kept in "log form" can be multiplied or divided by simply adding or subtracting the numbers. For example, exp(log(a) + log(b)) is the same as a * b. Or, using a = 41, b = 101, this would be exp(3.71357 + 4.61512), which is exp(8.32869), or 4140.98930. Obviously precision plays a part, and I truncated the numbers to 5 digits. 41 * 101 is 4141.

I haven't worked through your example code, nor is it immediately obvious to me why your code does things the way it does, but hopefully the above will be able to help you piece it together.

EDIT: I ran some numbers through your example code. If a = 41 and b = 101, and log_a = 3.71357 and log_b = 4.61512, then your example code computes 4.95582, and exp(4.95582) is equal to 142.0. The "simpler" way to get this same result is log(exp(log_a) + exp(log_b)), but as others have pointed out, this way involves three expensive transcendental functions, whereas your example code requires only two (plus a trivial comparison).

Others have posted good answers for why to do this at all. I'm wondering about the if/else part. Regardless of whether log_a or log_b is bigger, both expressions for v should be equivalent to log(a+b). In each case 0 < exp( ... ) <= 1, and log(1+exp( ... )) is a small positive number. For some reason I don't know this must be good.

If you're asking about the if/else, it's to avoid loss of precision. All arithmetic operations on floating point numbers (except for multiplication by powers of 2 and certain cases of addition/subtraction of numbers of the same exponent) destroy information, and good floating point code will choose the method with the least loss of precision.