# Thread: An approximation for log(a+b)

1. ## An approximation for log(a+b)

Hello,
I have very big numbers that overflow as an integer value. Therefore, I keep the logarithms of those values. The problem is that: I need log(a+b) but I only know loga and logb. How can I approximate to find log(a+b)?
Thanks.

2. If we suppose that $a>b$ then...

$\displaystyle \ln (a+b)= \ln a + \ln (1+\frac{b}{a}) = \ln a + \frac{b}{a} - \frac{b^{2}}{2\ a^{2}} + \frac{b^{3}}{3\ a^{3}} - ...$ (1)

If $a then swap a and b...

Kind regards

$\chi$ $\sigma$

3. Originally Posted by malaguena
Hello,
I have very big numbers that overflow as an integer value. Therefore, I keep the logarithms of those values. The problem is that: I need log(a+b) but I only know loga and logb. How can I approximate to find log(a+b)?
Thanks.

But if $a\ge b$

$\ln(a+b)\approx\ln(a)+\left[ 0.985605 \left(\frac{b}{a}\right)+0.111097 \left(\frac{b}{a}\right)^2-0.403140\left(\frac{b}{a}\right)^3\right]$

and if $b \ge a$

$\ln(a+b)\approx\ln(b)+\left[ 0.985605 \left(\frac{a}{b}\right)+0.111097 \left(\frac{a}{b}\right)^2-0.403140\left(\frac{a}{b}\right)^3\right]$

With absolute error $<0.0007$ over the appropriate interval for each of the approximations

CB

4. More specifically problem is exactly like this: I need to calculate this: dk=alpha*gamma(nk)+dleftk*drightk
alpha is a small double value here.
gamma(nk)=(n-1)! (overflows for big integers - where nk is an integer value)
dleftk and drightk are calculated recursively according to the same equation. Therefore, I have log(gamma(nk)) and log(dleftk*drightk) and I need to calculate dk.

dk is calculated for a tree, and here dleftk and drightk represent the children of the node dk.

I thought keeping the logarithms will be easier, but any other solution will be appreciated as well.

5. Originally Posted by CaptainBlack

CB
More specifically problem is exactly like this: I need to calculate this: dk=alpha*gamma(nk)+dleftk*drightk
alpha is a small double value here.
gamma(nk)=(n-1)! (overflows for big integers - where nk is an integer value)
dleftk and drightk are calculated recursively according to the same equation. Therefore, I have log(alpha*gamma(nk)) and log(dleftk*drightk) and I need to calculate dk.

dk is calculated for a tree, and here dleftk and drightk represent the children of the node dk.

I thought keeping the logarithms will be easier, but any other solution will be appreciated as well.

6. Originally Posted by chisigma
If we suppose that $a>b$ then...

$\displaystyle \ln (a+b)= \ln a + \ln (1+\frac{b}{a}) = \ln a + \frac{b}{a} - \frac{b^{2}}{2\ a^{2}} + \frac{b^{3}}{3\ a^{3}} - ...$ (1)

If $a then swap a and b...

Kind regards

$\chi$ $\sigma$

But is not that the case, when a is much bigger than b, and vice versa?

7. Originally Posted by CaptainBlack

But if $a\ge b$

$\ln(a+b)\approx\ln(a)+\left[ 0.985605 \left(\frac{b}{a}\right)+0.111097 \left(\frac{b}{a}\right)^2-0.403140\left(\frac{b}{a}\right)^3\right]$

and if $b \ge a$

$\ln(a+b)\approx\ln(b)+\left[ 0.985605 \left(\frac{a}{b}\right)+0.111097 \left(\frac{a}{b}\right)^2-0.403140\left(\frac{a}{b}\right)^3\right]$

With absolute error $<0.0007$ over the appropriate interval for each of the approximations

CB
Great! Thanks very much for the solution. I really appreciated it.

8. Originally Posted by CaptainBlack

But if $a\ge b$

$\ln(a+b)\approx\ln(a)+\left[ 0.985605 \left(\frac{b}{a}\right)+0.111097 \left(\frac{b}{a}\right)^2-0.403140\left(\frac{b}{a}\right)^3\right]$

and if $b \ge a$

$\ln(a+b)\approx\ln(b)+\left[ 0.985605 \left(\frac{a}{b}\right)+0.111097 \left(\frac{a}{b}\right)^2-0.403140\left(\frac{a}{b}\right)^3\right]$

With absolute error $<0.0007$ over the appropriate interval for each of the approximations

CB
If we are less fussy and an absolute error <0.005 is acceptabe a second degree fit will do:

if $a\ge b$

$\ln(a+b)\approx\ln(a)+\left[ 0.937234 \left(\frac{b}{a}\right)-0.248540 \left( \frac{b}{a} \right)^2\right]$

and if $b \ge a$

$\ln(a+b)\approx\ln(b)+\left[ 0.937234 \left(\frac{a}{b}\right)-0.248540\left( \frac{a}{b}\right)^2\right]$