Simplify the expression $\log(2^x+2^y)$ for large values of $x,y$

115 Views Asked by At

Given two numbers $x,y$, I need to calculate $\log(2^x+2^y)$.

Is there a way to simplify this function, in a way that would allow to calculate the complete expression without going through calculating $2^x$ and $2^y$?

Assume that $x,y$ are larger than 1500 and I'm trying to calculate the value using a computer, so it can't really handle $2^{1500}$, but eventually I want to stay in the logarithmic scale so I don't really want to calculate $2^x$.

A (good) approximation is also fine (if exact simplification doesn't exist).

4

There are 4 best solutions below

3
On BEST ANSWER

Assume $x\ge y$. Because:

$$\log(2^x+2^y)=\log(2^x(1+2^{-(x-y)}))=x\log(2)+\log(1+2^{-(x-y)})$$

and, as you may know, $\log(1+h)\approx h$ for $h$ small, then:

  • If $2^{-(x-y)}$ is small, then take $\log(2^x+2^y)\approx x\log(2)+2^{-(x-y)}$
  • If $2^{-(x-y)}$ is large, leave it as is: $\log(2^x+2^y)=x\log(2)+\log(1+2^{-(x-y)})$.

The point here is that the error you get when you replace $\log(1+h)$ with $h$ is given by the error in Taylor expansion of $\log(1+h)$ around $h=0$, which is:

$$\log(1+h)=h-\frac{h^2}{2(1+\xi)^2}$$

for some $\xi, 0\lt\xi\lt h$, and so the error is bounded by $h^2/2$. Thus, I suggest you use that estimate of an error to choose for how small $h=2^{-(x-y)}$, i.e. for how big difference $x-y$ you are happy with the above approximation.

Example: Suppose you want the result to be correct up to two decimal points, i.e. the error to be smaller than $0.005$. By solving $h^2/2\le 0.005$ you get $h\le 0.1$, i.e. the approximation above is good whenever $x-y\ge\log_2(0.1)\approx 3.32$.

Altogether, luckily, $2^{-(x-y)}$ converges very quickly (exponentially) to zero as $x-y$ grows. This means you will probably accept the approximation for all but a very few differences $x-y$, for which you will need to use the exact formula with $\log(1+2^{-(x-y)})$. For those small $x-y$, if $x$ and $y$ are integers, you can even pre-calculate a table of the values of $\log(1+2^{-(x-y)})$.

4
On
  • If $x\simeq y$, then we have $\log (2^x + 2^y) \simeq \log (2^{x+1}) = (x+1)\log 2$ (which is equal to $(x+1)$ if we are in base $2$).
  • If $x\gg y$, then we have $\log (2^x + 2^y) \simeq \log(2^x) = x\log 2$ (equal to $x$ if we are in base $2$).

More precisely , if $h=x-y >0$, then$$\ln(2^x+2^y)=x\ln2+\ln(1+2^{-h})\in x\ln2+2^{-h}-O(4^{-h}).$$

0
On

$$\log_2(2^x+2^y)=\max(x,y)+\log_2(2^{x-\max(x,y)}+2^{y-\max(x,y)}) \\=\max(x,y)+\log_2(1+2^{-|x-y|}).$$

The second term is a gentle function close to zero and not exceeding $1$. You can compute it very accurately if the function log1p is available in your environment (for large values of $|x-y|$, say $>50$, just ignore it).

2
On

Let $M=\max(x,y)$ and $m=\min(x,y)$. Then $$ \ln(2^x+2^y) = \ln(2^M+2^m) = \ln(2^M(1+2^{-(M-m)}) = \ln(2^M) + \ln(1+2^{-(M-m)}) \\= \ln(2) M + \ln(1+2^{-(M-m)}) . $$ Now, even if the computer cannot handle $2^M,$ it can handle the above expression.

If you want to reduce the number of calculations of logarithms (hardly needed nowadays) then you can split the calculations into cases: If $M-m$ is bigger than $\sim 10$ then the second term can be approximated with just $2^{-(M-m)}$, and if $M-m$ is bigger than $\sim 100$ then it can be skipped completely. (The limits are qualified guesses; do some tests to see if you need to change them.)