How to evaluate $a^b - b^a = x$

41 Views Asked by At

I know the values of $a$ and $b$ and I want some other form of this equation to solve it using C++ programming language without overflow, because $a$ and $b$ can reach $100$.

I was thinking about logarithm but I could not complete the idea if we take the logarithm of the two sides:

$\log (a^b - b^a)$ = $\log x$

any idea?

2

There are 2 best solutions below

7
On BEST ANSWER

Unless $a$ and $b$ are very close, one of these will be much larger than the other. In that case if either one overflows the difference will, too, so you might as well just do the straightforward computation. Contrast $$10^{12}=1,000,000,000,000\\12^{10}=61,917,364,224$$ Taking the difference doesn't reduce $10^{12}$ much at all. If $a,b$ are integers just test for equality. If they are equal return $0$, otherwise compute it as written and let it overflow.

2
On

Evaluate $b \log(a)$ and $a \log(b)$. Suppose, for example, that $a \log(b)$ is smaller. $b \log(a) - a \log(b)=\log(\frac{a^b}{b^a})$. If this is so large that exponentiating causes overflow (or even gives you a 20-digit number), then for all practical purposes $x=a^b-b^a$ is just $a^b$ for purposes of the number of significant digits you have, and you are done (i.e., $\log(x)=b \log(a)$ to avoid overflow). If $b \log(a) - a \log(b)$ is less than $\log(10^{20})$ or so, exponentiate it and subtract $1$ to get $\frac{x}{b^a}$. If $b^a$ is not too large, solve directly. If $b^a$ is very large, take the log again to get $\log(x)-a\log(b)$, and then add $a\log(b)$ to get $\log(x)$.

The key thing is to realize that is $b \log(a)$ is sufficiently larger than $a \log(b)$, then $a^b-b^a$ will just be $a^b$. And of course (which I think you already know) to realize that if $x$ would overflow, you would still like to return $\log (x)$.