In response to Aleksandar Donev, concerning his formulation of the
calculation of one root of a quadratic polynomial, I wrote:
> .... As abs(A*C) becomes ever smaller compared to B**2, A*C is
> subtracted from later and later digits of B**2, and eventually from
> digits that aren't represented. So B**2-A*C gets closer and closer
> to B**2....
Michael Milgram suggested writing B**2 - A*C as (B-sqrt(A*C))*(B+Sqrt(A*C)).
If A*C is negative, of course you don't want to do this.
This is worthwhile if A*C is positive (and you don't mind to pay the
price of the sqrt) because it postpones the problem to smaller values of
A*C compared to B**2. But it doesn't make the problem go away. You
still need to be careful about the formulation you use, depending on the
sign of B and which root you want.
A good rule of thumb for those not yet expert in the art of floating-
point computation is to think very carefully about what happens when
values with the same sign are subtracted, or when values with different
signs are added: If the magnitudes are similar, digits will cancel and
low-order digits will get filled with not-necessarily-correct zeros.
Once you've learned this reflexively, you start searching for alternative
formulations when you see the problem in your work.
--
Van Snyder | What fraction of Americans believe
[log in to unmask] | Wrestling is real and NASA is fake?
Any alleged opinions are my own and have not been approved or disapproved
by JPL, CalTech, NASA, Sean O'Keefe, George Bush, the Pope, or anybody else.
|