0

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).

Bernard
  • 175,478
  • 1. Are these natural logs, or logs to base 2 (or 10)? 2. Are you using standard binary64 doubles / floats for your arithmetic? – PM 2Ring Dec 31 '20 at 11:32
  • @PM2Ring I'm currently just building a table with those values using Excel formulas, but I believe that the operations are too large for any big numbers representation anyway. And the logs are base 2 – Ofir Gordon Jan 01 '21 at 08:54

4 Answers4

4
  • 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}).$$

math
  • 2,313
3

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)})$.

  • FWIW, here is an interactive Sage / Python script that shows how well $h=2^{-i}$ and $h-h^2/2$ approximate $\log(1+h)$. – PM 2Ring Dec 31 '20 at 11:51
  • 1
    @PM2Ring Thanks, this also shows that, instead of $h$, the OP can take $h-\frac{h^2}{2}$ as an even better approximation, if they want more precision. (The error term in that case is of the order of magnitude $h^3/3$.) –  Dec 31 '20 at 11:54
  • This works perfectly, thanks! – Ofir Gordon Jan 01 '21 at 10:09
3

$$\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).

1

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.)

md2perpe
  • 26,770
  • 1
    If the OP is using 64 bit doubles (which have 53 bits of precision), and their $x, y>1500$, they can ignore $2^{m-M}$ when $M-m>42$, since $M\log 2$ is already "consuming" >10 bits. – PM 2Ring Dec 31 '20 at 11:57
  • @PM2Ring. Thanks for making my estimations more precise. – md2perpe Dec 31 '20 at 16:21