Post Reply 
Name that calculator - from the program listing...
11-12-2018, 10:17 AM (This post was last modified: 11-12-2018 12:13 PM by Ángel Martin.)
Post: #7
RE: Name that calculator - from the program listing...
Here's an example using the IF.ELSE.ENDIF structure to calculate the roots of a second degree equation, a.x^2 + b.x + c = 0

The program is *not* optimized for size; the purpose of the example is to show the strings more clearly in the respective branches. Feel free to improve on it, for instance by calculating the value of the discriminant beforehand and reusing it in the appropriate sections.

Code:
01     LBL “QRT”
02     “00XYZT”
03     SHFL
04     “b^2-4*a*c>=0”
05     IF
06       “#(b+Q(b^2-4*a*c”
07       “|- ))/2/a”
08       EVALY
09       “(Q(b^2-4*a*c)-b"
10        "|- )/2/a”
11     ELSE
12       "Q(4*a*c-b^2)/2/”
13       “|- a”
14       EVALY
15       “#b/2/a”
16     ENDIF
17     EVAL$
18     END

On entry the coefficients a,b,c are expected in (Z, Y, X} respectively.
On exit the roots are placed in {Y,X} if real (F4 set), or the Imaginary and real parts of the complex conjugate roots (F4 clear).

Example: try it for x^2 = 1, that is a=1, b=0, and c=-1
Result: x1= 1, x2 = -1

and to exercise the complex branch, how about x^2 + x + 1 = 0
resulting Re(z) = -0.5 ; Im(z) = 0.866025404

Cheers,
ÁM

"To live or die by your own sword one must first learn to wield it aptly."
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Name that calculator - from the program listing... - Ángel Martin - 11-12-2018 10:17 AM



User(s) browsing this thread: 1 Guest(s)