4
$\begingroup$

I wrote a simple short computer program to solve a pair of equations of the format ,

  • $y = a1 * x + b1$

  • $y = a2 * x + b2$ .

But , it outputs clearly wrong answers sometimes when $abs(a1)$ or $abs(a2)$ is very small ,but not zero. I analyze the problem as follows. When it computes $a1-a2$ for very small $a2$, the floating point function computes as $a1-a2 = a1$ . Is it possible and how do mathematicians safely solve these equations in their researches?

Thank you in advance.

For details, my algorithm follows.

  • check $ a1 != a2 $ ;
  • $0 = (a1-a2)x+(b1-b2)$;
  • $x=-1*(b1-b2)/(a1-a2)$.
  • 0
    @AustinMohr,yes I did. But I thought that Number type holds only single float numbers,instead of double float ones.Thank you very much.2012-01-14

1 Answers 1

5

If you equate the $y$'s, you get $a2*x+b2=a1*x+b1$ or $x=\frac{b1-b2}{a2-a1}$. Subtraction of nearly equal quantities can lose precision. It isn't so much $a1$ or $a2$ being small compared to $1$, it is $a2-a1$ being small compared to $a1$. Think of subtracting $1.000001-.999999$ when both have only $6$ digits precision. If each one is $\pm 1 e-6$, the difference is $\pm 2 e-6$ which is a disaster if the difference is $2 e-6$. Sometimes there is no help. If these two lines have almost the same slope, the intersection is badly defined. But if you have a quadratic, there is help. Section 5.6 of Numerical Recipes and many numerical analysis texts discuss how to solve that here.