Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log(1+x) is to log1p as log(1-x) is to?

<math.h> provides a more accurate method for computing log(1+x) for doubles.

Is there a similarly precise manner for computing log(1-x)?

The reason I ask is because I'm trying to do some work in log-space for greater precision (I'm mostly multiplying and summing numbers very close to zero). I found it easy to write a function that gives log( exp(log_of_a) + exp(log_of_b) ) = log( a + b ) by using log1p. I am trying to make a similar function for the difference:

log( exp(log_of_a) - exp(log_of_b) ) = log( a - b ) where a > b, of course.

Essentially, as long as neither log_a or log_b == -inf, the function should simply return:

return log( 1 - exp(log_b-log_a) ) + log_a;

In my log_add function, I end up with a log( 1 + ... ), and so I use log1p. But here I have log( 1 - ... ). Just in case, I even googled log1m, but no luck...

When the argument x is in the range [-inf, 1), then I could simply use log1p(-x) (given my assertion a > b).

Is that the best way to go about solving this? I feel I must be doing work that has been done before...

I'd really appreciate your help knowing how to get the most accurate results I can (or explaining why I can't get results more accurate than this).

like image 983
user Avatar asked Mar 28 '12 05:03

user


1 Answers

@Raymond Chen is spot on: "Negation of floating point numbers is exact, so log1p(-x) is as accurate as log1p(x)." Just making it into a real answer.

like image 76
StilesCrisis Avatar answered Sep 22 '22 16:09

StilesCrisis