I’m running into trouble with the following formula that we’re using in our software.
$$ \frac{1 - x^{-y}}{1 - x^{-(y+1)}} $$
In certain cases, the value for $y$ is a relatively large number; sometimes positive, sometimes negative. I just had a case where its value was -13729.03476
. Since I’m using ‘regular’ double-precision variables, this led to a floating point overflow error.
Looking at it, I get the distinct impression that it should be possible to simplify this formula, but I just can’t figure out how.
Alternatively, is there some other way to prevent (or predict) a floating point overflow in this situation?
Thanks in advance!
It's usually very simple. If $x > 1$ and $y$ is positive, or if $x < 1$ and $y$ is negative, then $x^y$ can blow up, so you use the original expression
$$\frac{1 - x^{-y}}{1 - x^{-(y+1)}}$$
If $x > 1$ and $y$ is negative, or if $x < 1$ and $y$ is positive, then $x^{-y}$ can blow up, so you use the expression
$$\frac{x(x^y -1)}{x^{y+1} - 1}$$
But if $x = 1$, then the expression is undefined. I don't know if this can happen in your software, but if so, you need to figure out some way of handling it!