1

The following expression shows significant numerical differences in a program when I compile in x86 (32 bit) versus x64 (64 bit), when $a$ is small:

$$ \left( \dfrac{1}{a} - b \right) \left( 1- \exp(-a)\right)$$

Is there a way that I can refactor this expression so that it is more robust for small $a$? It is not completely clear to me that simply expanding the expression into four terms is the best solution.

RoG
  • 113
  • Can you quantify what you consider 'small $a$'? – orlp Aug 30 '18 at 08:27
  • 1
    What have you tried? Maybe you can approximate the expression with a polynomial when $a$ is small. – Matti P. Aug 30 '18 at 08:29
  • @orlp Around $a=$1e-6 – RoG Aug 30 '18 at 08:40
  • @MattiP. I have tried expanding into four terms, and using $1-x$ for the exponential. Expanding showed some improvement, but I am not sure that it is the best approach. – RoG Aug 30 '18 at 08:42
  • 1
    The Taylor series for $1-e^{-a}$ at $a=0$ has a factor of $a$. If you expand it, and cancel with $1/a$, then your expression will be more stable. – preferred_anon Aug 30 '18 at 10:57

1 Answers1

2

You can approximate $$ e^x \approx 1+x + \frac{x^2}{2!} + \frac{x^3}{3!} \qquad \Rightarrow e^{-x} \approx 1-x + \frac{x^2}{2!} - \frac{x^3}{3!} $$ Plugging this into the equation, we get $$ \left(\frac{1}{x}-b\right) \left(1- \left[ 1-x + \frac{x^2}{2!} - \frac{x^3}{3!}\right]\right) = \left(\frac{1}{x}-b\right) \left(x - \frac{x^2}{2!} + \frac{x^3}{3!}\right) = \left(\frac{1}{x}-b\right) x\left(1 - \frac{x}{2!} + \frac{x^2}{3!}\right) $$ Then assume $x\neq 0$ ... Can you continue from here?

Matti P.
  • 6,012
  • Thanks. Can I ask why you chose to stop at $x^3$? – RoG Aug 30 '18 at 10:41
  • 1
    No particular reason. I think with that amount of terms you can already see the pattern (of pluses and minuses) and can therefore remove or add terms if necessary. – Matti P. Aug 30 '18 at 10:46
  • Note that if you want a final expression with terms up to order $n$, the series for $\exp(-x)$ must initially be up to order $n+1$ – RoG Sep 04 '18 at 11:43