My function is $E(v)=mc^2(\frac{1}{\sqrt{1-v^2/c^2}} - 1)$, (c=3e8, m=1) and I have to calculate it for values of v between 1e-6 and 2.99e8. The point of this problem is floating point precision.
For very small values of v the result is E=0. I figured that's because of the root: $\sqrt{1-v^2/c^2}=1$.
I tried using Taylor: $mv^2/2 + 3mv^4/8c^2 + 5mv^6/16c^4 + 35mv^8/128c^6$, and that works well for small v but gives me wrong results for very big values of v.
How do I transform E(v) to a better formula that works for all values of v?
I'm assuming you don't have a square root routine available. Something you could try...
For simplicity I'll assume $m=c=1$, and call the answer $u$, so we have
$$E(v)=u=\frac{1}{\sqrt{1-v^2}}-1$$
We can rearrange this to get:
$$(u+1)^2(1-v^2)=1$$
And we can write it as a root finding problem in $u$:
$$(1-v^2) u^2 + 2(1-v^2)u + (v^2 - 2v) = 0$$
Or, perhaps it's clearer to let $s=1-v^2$ and rearrange,
$$s(u^2 + 2u + 1)-1 = 0$$
You could use any root finding algorithm, to find $f(u)=0$, Newton's method iterates:
$$u_{n+1} = u_n - \frac{f(u_n)}{f'(u_n)} = u_n - \frac{s (u_n^2 + 2u_n + 1)-1}{2s(u_n + 1)}$$
With a few iterations that should give you the answer to a decent accuracy, at least.