what is the best way to compute the value of the following function in a computer program, e.g., C/Fortran, in double precision?
$$ g(a,b)={{e^a/a - e^b/b}\over{a-b}} + {1\over{ab}}. $$
The above function has removable discontinuities at a=0, b=0, and a=b. I also need to code a similar three-parameter function that has removable discontinuities at a=0, b=0, c=0, a=b, b=c, c=a, and a=b=c. I am trying to avoid too many if-else in the program, and wondering if there are any mathematical tricks that can help.
As for the one-parameter function $g(x)=(e^x-1)/x$, it is currently coded as follows:
If |x| > $\epsilon$, return g(x) directly; else return $e^{x/2}{{sinh(x/2)}\over{(x/2)}}$, where $sinh(t)\over{t}$ is computed from a truncated Tayler series.
This method can easily be applied to function $f(a,b)=(e^a-e^b)/(a-b)$, as it is equal to $e^{(a+b)/2}{{sinh((a-b)/2)}\over{(a-b)/2}}$, but the best solution for $g(a,b)$ is still not obvious to me.
Thanks.
expm1, which allows the computation of $e^x-1$ without cancellation for small $x$. Your function is $f[0,a,b]$ for $f(x)=e^x$. As a second order divided difference it is harder to approximate with standard numerical functions, especially in the case where both $a$ and $b$ are small. – Lutz Lehmann Sep 10 '21 at 06:10$$\frac{\dfrac{e^a-1}a-\dfrac{e^b-1}b}{a-b}.$$
– Sep 10 '21 at 07:29