Post Reply 
HP48 programming: use values from stack in expression
12-01-2018, 05:05 AM (This post was last modified: 12-06-2018 02:10 AM by Giuseppe Donnini.)
Post: #4
RE: HP48 programming: use values from stack in expression
SUBST is not an HP-48 command.

Thomas Klemm's solution works, but you would have to rewrite the program for every single function you want to apply.

Here's a more general solution:

1. For single-argument functions (like SIN, square root, etc.):
Code:
@ F1 : ( x {f} --> f(x) )
@ Given x and f on the stack, where x is any argument, and f is a function of
@ one argument, F1 returns 'f(x)'.  Note: f has to be embedded in a list to
@ prevent its immediate execution.
@ Example: ( 12 {SIN} --> 'SIN(12)' )

\<< SWAP \-> a                        @ Save argument into local variable a
    \<< 'a' SWAP EVAL                 @ Calculate f(a) symbolically
        'a' a 2 \->LIST \|^MATCH DROP @ Substitute x for a
    \>>
\>>

2. For two-argument functions (like +, -, etc.):
Code:
@ F2 : ( x y {f} --> f(x,y) )
@ Given x, y, and f on the stack, where x and y are any arguments, and f is a
@ function of two arguments, F2 returns 'f(x,y)'.  Note: f has to be embedded in
@ a list to prevent its immediate execution.
@ Example: ( 1 6 {+} --> '1+6' )

\<< 3 ROLLD \-> a b                   @ Save arguments into local vars a and b
    \<< 'a' 'b' ROT EVAL              @ Calculate f(a,b) symbolically
        'a' a 2 \->LIST \|^MATCH DROP @ Substitute x for a
        'b' b 2 \->LIST \|^MATCH DROP @ Substitute y for b
    \>>
\>>

Following the same pattern, programs for three or more argument functions can easily be added.

Note that these programs respect the symbolic result flag -3: if clear (default), they return symbolic results; if set, they return numeric results.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: HP48 programming: use values from stack in expression - Giuseppe Donnini - 12-01-2018 05:05 AM



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