I'm implementing a function $f(x_1, x_2, \dots x_n)$ given by the ratio of two closed form equations. $$ f(x_1, x_2, \dots, x_n) = \frac{g(x_1, x_2, \dots, x_n)}{h(x_1, x_2, \dots, x_n)} $$

Unfortunately, when $x_1 = 0$, both $g$ and $h$ are $0$. However there is a well-defined limit $$ k (x_2, x_3, \dots x_n) = \lim_{x_1 \to 0} f(x_1, x_2, \dots x_n) $$

What is a sensible way of implementing this? I was thinking of

if |x_1| > cutoff:
   return f(x_1, ..., x_n)/g(x_1, ..., x_n)
   return k(x_2, ... x_n)

How do I determine a suitable cutoff?

I have tried to research this question and I came across Higham's book on Accuracy and Stability of Numerical Algorithms.

I found a relevant example in section 1.14.1 where he discusses $f(x) = (e^x-1)/x$. His solution to the instability is different from my naive approach above; he uses a change of coordinates $y = e^x$ that somehow reduce the inaccuracy.

I would appreciate any suggestions/references on how to tackle this problem.


Expand your function $f$ in powers of $x_1$ and compute the error term. Use the expansion when error is under your desired threshold.

Imagine you linearise the function $f$. You will have that: $$f=f_0+f'x_0+\frac{1}{2}f''(\xi)x_0^2$$ Being the last term the error $\epsilon$. Chose $\xi$ as the value that maximises the absolute value of $f''(x_0)$ which is obtained from $f'''(\xi))=0$

if epsilon<threshold

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.