I am having trouble implementing a function numerically. It suffers from the fact that at large input values the result is a very large number times a very small number. I am not sure if catastrophic cancellation is the correct term so please correct me if it is. Evidence of something going awry: How can I avoid the oscillations and the assigment of 0.0 for larger inputs of 6?Here is my function: import numpy as npdef func(x):    t = np.exp(-np.pi*x)    return 1/t*(1-np.sqrt(1-t**2))
How to avoid catastrophic cancellation in python function?
python;numerical analysis;floating point
This is indeed called catastrophic cancellation. In fact, this particular case is very easy: rewrite the function using the equivalent, numerically stable expression $$ \frac{t}{1+\sqrt{1-t^2}}. $$Since you probably need a reference, this is discussed in most numerical methods textbooks in relation to the formula for solving quadratic equations (that formula in its standard form is numerically unstable for some parameter values). The way to get rid of $1-\sqrt{1-t^2}$ is to multiply and divide by $1+\sqrt{1-t^2}$. Here is a comparison: