ichbin
12-01-2010, 03:55 AM
There is a slight problem with equation 5.6.4. I know it's hard to believe, this being just the quadratic formula, but bear with me!
When b=0, sgn(b)=0, so the formula gives q=0, when it should give q = -sqrt(-ac). For example, x^2 - 1 = 0 should have solutions +1 and -1, but since sgn(b)=sgn(0)=0, the formula gives q=0 and thus the solutions 0 and -1/0=NaN.
Presumably the authors assumed sgn(0)=1, which would make the problem go away. But officially sgn(0)=0. See, for example, http://en.wikipedia.org/wiki/Sign_function.
If one codes the formula as "if (b >=0) { ... } else { ... }", i.e. wrongly assuming sgn(0)=1, then the problem goes away. But if one codes the formula by calling a correctly implemented signum function, e.g. the Math.Sign function offered by the .NET framework, then results go wrong when b=0.
Yeah, I know it sounds like a minor quibble, but it cost me 15 minutes to track down what was going wrong in a routine where everything had gone right many times before.
When b=0, sgn(b)=0, so the formula gives q=0, when it should give q = -sqrt(-ac). For example, x^2 - 1 = 0 should have solutions +1 and -1, but since sgn(b)=sgn(0)=0, the formula gives q=0 and thus the solutions 0 and -1/0=NaN.
Presumably the authors assumed sgn(0)=1, which would make the problem go away. But officially sgn(0)=0. See, for example, http://en.wikipedia.org/wiki/Sign_function.
If one codes the formula as "if (b >=0) { ... } else { ... }", i.e. wrongly assuming sgn(0)=1, then the problem goes away. But if one codes the formula by calling a correctly implemented signum function, e.g. the Math.Sign function offered by the .NET framework, then results go wrong when b=0.
Yeah, I know it sounds like a minor quibble, but it cost me 15 minutes to track down what was going wrong in a routine where everything had gone right many times before.