(12C) Solve a system of linear equations of 2 Unknown
|
03-23-2018, 09:40 AM
Post: #2
|
|||
|
|||
RE: (12C) Solve a system of linear equations of 2 Unknown
(03-23-2018 04:24 AM)Gamo Wrote: Solve a system of linear equations for two unknown variables. Gamo, this is a nice and functional program. It works correctly and solves a common problem. And it also is a nice example where applying some basic rules of good programming style may make it much more reliable and robust. So I hope you do not mind when I take the opportunity to remind us all of a basic rule for better programming: When a program stops with R/S for display or data entry, it is always a good idea not to rely on a certain stack content when the program continues. The user may have performed a calculation before he entered a value, or some other key may have been pressed that disturbs the stack. This would cause the program to continue with wrong data and thus wrong results. Take a look at the R/S in line 12. The next steps rely on the fact that the T-register remains unchanged. The user only has to press a number key, and T is lost. Here the 3x Rv is not required at all, simply replace this with "RCL 4" and the problem cannot occur. The program will safely continue with the correct data. Then take a look at line 28-30. The determinant is calculated and R/S displays it. But it has not been stored at this point. It is stored after the R/S. Imagine the user inadvertedly presses a key here: then the number that happens to be in X is stored in the next step as the (wrong) determinant and the complete calculation is void. As a general rule you should always first store a result, then display it. And, if possible, always write the program in a way that does not rely on previous stack contents when the user continues with R/S. Always remember: the common user is a strange beast. He may press keys that you don't want him to press, so you better play safe and make sure that this does not cause unexpected problems. All this can be done easily here: - After all six coefficients have been entered, calculate the determinant, then store it, and finally, once it has been saved, display it. - Then calculate X. Do not use previous stack content – all required data is available in the storage registers from where it can be safely recalled. - Finally calculate Y in the same way. Code: 01 STO 1 // store coefficients of equation 1 Again, I hope you do not mind when I take this opportunity to discuss a basic rule of RPN programming that makes programs more reliable and safer to use. Dieter |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
(12C) Solve a system of linear equations of 2 Unknown - Gamo - 03-23-2018, 04:24 AM
RE: (12C) Solve a system of linear equations of 2 Unknown - Dieter - 03-23-2018 09:40 AM
RE: (12C) Solve a system of linear equations of 2 Unknown - Gamo - 03-23-2018, 12:02 PM
RE: (12C) Solve a system of linear equations of 2 Unknown - Dieter - 03-23-2018, 12:27 PM
RE: (12C) Solve a system of linear equations of 2 Unknown - Gamo - 03-24-2018, 03:22 AM
RE: (12C) Solve a system of linear equations of 2 Unknown - Dieter - 03-24-2018, 08:21 AM
|
User(s) browsing this thread: 1 Guest(s)