# How to safely solve a pair of elementary equations in a floating point computing system?

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)$.
-
If by "very small $a_2$" you mean $a_2$ is close to $0$, then it sounds like your language doesn't carry enough digits of precision and is rounding $a_2$ down to $0$. What language/software are you using? – Austin Mohr Jan 14 '12 at 6:06
@AustinMohr,the language is flash action script3, I admit it is not for solving mathematics. – seven_swodniw Jan 14 '12 at 6:12
You may already be using it, but there is a Number data type that seems to allow for a fair amount of precision. You can read about it here. – Austin Mohr Jan 14 '12 at 7:35
@AustinMohr,yes I did. But I thought that Number type holds only single float numbers,instead of double float ones.Thank you very much. – seven_swodniw Jan 14 '12 at 10:07

## 1 Answer

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.

-