DSPRelated.com
Forums

approximation of log(x+y) ??

Started by Lars Hansen October 6, 2005
Hello

Is it possible to approximate log(x+y) ?

Thanks :o)



"Lars Hansen" <invalid@nospam.com> writes:

> Hello > > Is it possible to approximate log(x+y) ?
Yes. One useful way of re-writing this is: If x >= y, log(x+y) = log(x) + log(1+y/x) If y > x, interchange the roles of x and y. Or, in other words, log(x+y) = log(max(x,y)) + log(1+min(x,y)/max(x,y)) Scott -- Scott Hemphill hemphill@alumni.caltech.edu "This isn't flying. This is falling, with style." -- Buzz Lightyear
Scott Hemphill wrote:
> "Lars Hansen" <invalid@nospam.com> writes: > > >>Hello >> >>Is it possible to approximate log(x+y) ? > > > Yes. One useful way of re-writing this is: > > If x >= y, log(x+y) = log(x) + log(1+y/x) > > If y > x, interchange the roles of x and y. Or, in other words, > > log(x+y) = log(max(x,y)) + log(1+min(x,y)/max(x,y))
IIRC, this was mainly useful when one of the values is *much much much* larger than the other one, such that the addition would severely suffer from floating point rounding effects -- presumably the division would preserve the information better, even if the 1+ is not done correctly; then, when adding the logs, the range is not as wide as when adding the original numbers. Carlos --
Carlos Moreno <moreno_at_mochima_dot_com@xx.xxx> writes:

> Scott Hemphill wrote: > > "Lars Hansen" <invalid@nospam.com> writes: > > > >>Hello > >> > >>Is it possible to approximate log(x+y) ? > > Yes. One useful way of re-writing this is: > > If x >= y, log(x+y) = log(x) + log(1+y/x) > > If y > x, interchange the roles of x and y. Or, in other words, > > log(x+y) = log(max(x,y)) + log(1+min(x,y)/max(x,y)) > > IIRC, this was mainly useful when one of the values is > *much much much* larger than the other one, such that > the addition would severely suffer from floating point > rounding effects -- presumably the division would > preserve the information better, even if the 1+ is not > done correctly; then, when adding the logs, the range > is not as wide as when adding the original numbers.
You shouldn't do a "1+". It throws away precision. Instead, you should call a log(1+x) function. Anyway, I was trying to guess what the OP wanted, since he didn't give very much information. There must be *some* reason he wanted to calculate log(x+y) directly, instead of first adding x and y, and then taking the log. Another use for this way of calculating log(x+y) is when x and y are outside your floating point range, so that all you have is log(x) and log(y). Then you can write log(x+y) = log(x) + log(1+exp(log(y)-log(x))). Since the argument is negative, the exp won't exceed your floating point range, but it might underflow. Scott -- Scott Hemphill hemphill@alumni.caltech.edu "This isn't flying. This is falling, with style." -- Buzz Lightyear
thanks, but that is not an approximation, is it?

what I meant was :

is it possible to approximate log(x+y) with something that does not contain 
the log-function?

thanks :o)






Lars Hansen wrote:
> thanks, but that is not an approximation, is it? > > what I meant was : > > is it possible to approximate log(x+y) with something that does not contain > the log-function? > > thanks :o)
If it were, then you could also approximate log(x) without a log function by setting z = x-a and taking log(z+a). log(1) = 0, and when the base is e, the slope there is 1. So for small x, log_e(1+x) = x. There is a constant multiplier for other bases. Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
"Lars Hansen" <invalid@nospam.com> writes:

> thanks, but that is not an approximation, is it? > > what I meant was : > > is it possible to approximate log(x+y) with something that does not contain > the log-function?
Is there some reason you can't just add x and y and use an approximation for the log function? Scott -- Scott Hemphill hemphill@alumni.caltech.edu "This isn't flying. This is falling, with style." -- Buzz Lightyear
> > Is there some reason you can't just add x and y and use an approximation > for > the log function?
Well...i guess not...before I posted the question I was looking at some other expressions like: log(ab)=log(a)+log(b) etc So I guess that's why I asked for an approximation for log(x+y)... But then...tell me how do I approximate log(x) ? I thought about this: log(x) = (1/a)(x-a)+log(a) But are there better approximations? cheers...
"Lars Hansen" <invalid@nospam.com> writes:

> > > > Is there some reason you can't just add x and y and use an approximation > > for > > the log function? > > Well...i guess not...before I posted the question I was looking at some > other > expressions like: > > log(ab)=log(a)+log(b) > etc > > So I guess that's why I asked for an approximation for log(x+y)... > > But then...tell me how do I approximate log(x) ? > > I thought about this: > > log(x) = (1/a)(x-a)+log(a) > > But are there better approximations?
Yes, there are. Questions like that come up frequently in this group, so you might find a suitable answer by googling. A starting point is the series log(1+x) = x - x^2/2 + x^3/3 - x^4/4 + ... - (-x)^n/n + ... This converges for -1 < x < 1, but not terribly well unless abs(x) is small. There is the related series log(1/(1-x)) = x + x^2/2 + x^3/3 + x^4/4 + ... + x^n/n + ... which also converges for -1 < x < 1. Between the two of them you should (in theory) be able to calculate the natural log for any positive real number. If you truncate these series at any point, you then have an approximation. There are several ways to improve the performance of an approximation, including domain folding, Chebyshev polynomial approximation, and table lookups, but in order to figure out what combination of these might be suitable, you need to answer the questions: o what processor are you using? (does it have native division? how about log? :-) o what is the representation for x? (floating point--what kind? fixed point? integer?) o what is the domain of x? (minimum value, maximum value; how many possible values will you calculate?) o how much error can you tolerate? o how much memory can you devote to the function, and how fast does the calculation have to be? Scott -- Scott Hemphill hemphill@alumni.caltech.edu "This isn't flying. This is falling, with style." -- Buzz Lightyear
Thanks :o)

That was a very good, helpful and precise answer.

I will look into it..