0

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?

AliOsm
  • 103

2 Answers2

1

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

C Monsour
  • 8,228
  • If the difference between $b\log(a)$ and $a\log(b)$ is greater than $\log (10)$ the larger is over $10$ times the smaller and the subtraction will not bring the computation into range. It is $b \log (a)$ that should be tested for being so large that exponentiating it will cause an overflow. – Ross Millikan May 13 '18 at 15:19
  • Your first statement would be true if you said greater than $10$, but as I said if exponentiating is large enough to cause overflow, then $a^b-b^a$ is functionally $a^b$ and the answer is basically $\log(x)$=$b\log(a)$. As for the second, I said if $b^a$ is not too large...that's called testing. I give a way to handle that case to get $\log(x)$. – C Monsour May 13 '18 at 15:46
0

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.

Ross Millikan
  • 374,822