The Museum of HP Calculators

HP Forum Archive 17

 Re: Which quadratic solution should we use?Message #2 Posted by allen on 4 Mar 2007, 1:24 a.m.,in response to message #1 by Palmer O. Hanson, Jr. I have been reading through Wickes's book "HP 48 insights" that another forum member sent. It is truly beautiful to see in practice what Karl mentioned a few days ago: Quote: ...use equation-based programming when the formula is simple and straightforward, and use keystroke programming when the formula or calculation is lengthy... Is not the quadratic equation a worthy example? I did not appreciate the simplicity of the equation until I saw it in this form: There are really only two terms here: c/a and b/2a, the second of which comes up twice! So, a very straightforward RPN-equation would be a two-fold process: [a b c] -> [c/a b/2a] -> [root1 root2] I can't find my original program from 1993, so here is a rough hack for one at 52.5 bytes. I know someone can get one below 50 bytes. << SWAP -2 / ->V2 SWAP / V-> 'sets up [c/a,-b/2a] using vector DUP SQ ROT - (SQRROOT) 'finds SQRROOT(b^2/4a^2 -c/a) DUP2 + 3 ROLLD - 'gets both Y+X and Y-X on the stack. >> Then again you could just type in: [pre] << ->V3 PROOT >> But I think that would defeat the purpose.... Edited: 4 Mar 2007, 1:53 a.m.

 Re: Which quadratic solution should we use?Message #3 Posted by Les Wright on 4 Mar 2007, 2:04 a.m.,in response to message #2 by allen This is very elegant but it still has destructive cancellation and gives wrong answers for all of the tough examples Palmer gives. Les

 Re: Which quadratic solution should we use?Message #4 Posted by Karl Schneider on 4 Mar 2007, 4:01 a.m.,in response to message #1 by Palmer O. Hanson, Jr. Hi, Palmer -- Quote: A more thorough treatment of the subject with a similar solution method appears in the appendix "Accuracy of Numerical Calculations" to the HP-15C Advanced Functions Handbook also published in 1982. The discussion on page 191 presents the textbook formula and an algebraically equvalent calculation sequence which avoids "destructive cancellation". (Readers of that reference should note that for some reason the authors chose to define the quadratic as c - 2bz + az2. The more common definition is used here.) The discussion states "... Such a program will be listed later (page 205) and must be used in those instances, common in engineering, when the smaller root y is needed accurately despite the fact that the quadratic's other unwanted root is relatively large." One advantage of defining the quadratic that way is that the determinant becomes b2 - ac instead of b2 - 4ac, and the magnitude of those terms is reduced by a factor of 4. I haven't fully examined the program to determine exactly how it works. Both the b2 and a*c terms overflow a 10-digit register in cases 4, 5, and 6. "b2 - ac" nonethless is calculated and tested against zero in several ways. -- KS Quote: A demonstration problem is provided. For a quadratic equation of the form ax2 + bx + c and a = 1E-13, b = -2 and c = 1 the exact solution is r1 = 2E13 and r2 = 0.5. Implementation of the textbook formula on most HP and TI machines will yield r1 = 2E13 and r2 = 0. . I get r1 = 19999999999999.4999999999999875 = 2E13 - 0.5 - 1.25E-14 r2 = 0.50000000000001249999999996826535 ~= 0.5 + 1.25E-14 on Windows XP Calculator, with its 80-bit extended double-precision variables. These results are quite close to exact, with about 1E-25 calculation roundoff error when re-inserted into the original equation. x = 0.5 would be the only (and exact) solution if "a" were zero. x = 2E13 yields ax2 + bx = zero exactly, so ax2 + bx + c = 1. In the original problem, calculating the square root of the determinant "b2 - 4ac" as sqrt(abs(b + 2*sqrt(a)*sqrt(c)) * sqrt(abs(b - 2*sqrt(a)*sqrt(c)) roundoff error was reduced, but 12 significant digits isn't enough without using special methods. -- KS Edited: 4 Mar 2007, 3:03 p.m.

 Re: Which quadratic solution should we use?Message #5 Posted by Palmer O. Hanson, Jr. on 4 Mar 2007, 9:22 p.m.,in response to message #4 by Karl Schneider Quote: I get r1 = 19999999999999.4999999999999875 = 2E13 - 0.5 - 1.25E-14 r2 = 0.50000000000001249999999996826535 ~= 0.5 + 1.25E-14 on Windows XP Calculator, with its 80-bit extended double-precision variables. These results are quite close to exact, with about 1E-25 calculation roundoff error when re-inserted into the original equation. You can, of course, simply do the arithmetic with pencil and paper and find that r1 will be a little less than 2E13 and r2 will be a little more than 0.5. Furthermore, for a = 1E-nn, b = -2 and c = 1 the pencil and paper answers will be r1 a little less than 2Enn and r2 a little more than 0.5 . Some tests with a number of machines show that if nn is less than the number of digits carried by the machine (but in some cases only if nn is two less than the number of digits carried by the machine) then the machine will get the 0.5 answer using the textbook solution. This means that getting the 0.5 answer for r2 from unknown routines on fifteen digit machines like the TI graphic calculators, for example, does not mean that the unknown routines use the more accurate formulation. I have tested the routines in the TI-85, TI-86 and TI-89 to verify that they will get the 0.5 answer when nn is greater than 15.

 Re: Which quadratic solution should we use?Message #6 Posted by John Keith on 4 Mar 2007, 1:42 p.m.,in response to message #1 by Palmer O. Hanson, Jr. Thank you, Palmer, for this thorough and enlightening post. To answer your original question, I am guessing HP's logic goes something like this: Professional engineers etc. who are paying big \$\$ for the higher-end calcs and reading the advanced-function manuals are likely to run into unusual problems for which they need accurate solutions. Purchasers of simpler, less expensive machines are likely to be students for whom learning to use and program the calculator to solve textbook problems is more useful. HP may have thought that the more accurate but less obvious program would confuse and intimidate beginners and students but be worth the consideration (and extra keystrokes) of more advanced users John (Post edited for additional note) Though it may be defeating some purposes, the PROOT function on the 50g returns the correct result for all 4 of Allen's examples, and does so in less than half a second. Presumably it uses the better algorithms that Palmer describes. Edited: 4 Mar 2007, 5:48 p.m. after one or more responses were posted

 Re: Which quadratic solution should we use?Message #7 Posted by Palmer O. Hanson, Jr. on 4 Mar 2007, 9:35 p.m.,in response to message #6 by John Keith Quote: I am guessing HP's logic goes something like this: Professional engineers etc. who are paying big \$\$ for the higher-end calcs and reading the advanced-function manuals are likely to run into unusual problems for which they need accurate solutions. Purchasers of simpler, less expensive machines are likely to be students for whom learning to use and program the calculator to solve textbook problems is more useful. HP may have thought that the more accurate but less obvious program would confuse and intimidate beginners and students but be worth the consideration (and extra keystrokes) of more advanced users You may be correct. I find it interesting that H-P chose to give the more accurate formulation for the HP-67 and the HP-15C, albeit not in the baseline documentation but in extended documentation. This may be consistent with the opinion expressed with some frequency in this forum that H-P isn't providing documentation as carefully as they used to. One concern about the "more accurate" method arises from my background in aerospace and weapons systems where we always worried a little (maybe a lot) when we changed software to fix one problem for fear that we may have inadvertently caused another problem.

Go back to the main exhibit hall