Post Reply 
Store and reuse intermediate variable
12-05-2022, 09:10 AM
Post: #1
Store and reuse intermediate variable
Hi all.
One year ago I wrote a post related to subroutine, I'm here again becasue I had a turbulent year.
Some years ago I bought a HP 50g and I'm trying to program it with litte improvement ...

My problem is using local variable in a program, in detail store an intermediate value and reuse it.
To have practice I wrote a simple program that calculate mortgage installment (even if there are financial tools that alreade did it).
I would calculate the installment and the total amount paid (simply multiplying this intermediate value for number of years and months). My problem is store this value.
A sample of program is:
Code:

<<<<"Ins C t A " {
":C:
:t:
:A:" {1 0} V} INPUT OBJ-> -> C t A 
<<'C* (t/100/n)/(1-(1+t/100/n)^(-n*A))' -> R  ;I need to store this value in a local variale R and print on display
'R*A*12' ->NUM "M" ->TAG>> 
>>
Written in this way I can calcultate total amount (tagged with M) and result is correct, but intermediare R variable is not printed.

I red and checked tons of manuals but in didn't find some example that help me.
Thanks in advance.
Find all posts by this user
Quote this message in a reply
12-05-2022, 06:36 PM
Post: #2
RE: Store and reuse intermediate variable
I make a variant of program, it use global variable ...

Code:

<<"Ins C t A " {
":C:
:t:
:A:" {1 0} V} INPUT OBJ-> -> C t A 
<<'C* (t/100/n)/(1-(1+t/100/n)^(-n*A))' -> NUM 'R' STO
'R*A*12' ->NUM 'M' STO
R "R" -> TAG
M "M" -> TAG>>
'R' PURGE
'M' PURGE 
>>

In this way I can obtain what I need, but in this way global variables are used, a practice not recommended by many.
How I can avoid use global variables?
Find all posts by this user
Quote this message in a reply
12-05-2022, 10:22 PM
Post: #3
RE: Store and reuse intermediate variable
You should init R when initializing the other local vars, then you can simply ‘R’ STO and it will not create a global (HOME) var.

Example:

Code:

« "A" { 1 } INPUT OBJ-> 0 -> A B
 « 'A*2' ->NUM ‘B’ STO B 3 * »
»

Thibault - not collector but in love with the few HP models I own - Also musician : http://walruspark.co
Find all posts by this user
Quote this message in a reply
12-06-2022, 07:13 AM
Post: #4
RE: Store and reuse intermediate variable
Hi Pinkman.
Thanks for answer.
I'm trying to avoid global variables as many people suggest.

Thanks
Find all posts by this user
Quote this message in a reply
12-07-2022, 05:48 AM
Post: #5
RE: Store and reuse intermediate variable
(12-06-2022 07:13 AM)oierpa Wrote:  Hi Pinkman.
Thanks for answer.
I'm trying to avoid global variables as many people suggest.

Thanks

So do I. Did you try my suggestion?

Thibault - not collector but in love with the few HP models I own - Also musician : http://walruspark.co
Find all posts by this user
Quote this message in a reply
12-07-2022, 02:19 PM
Post: #6
RE: Store and reuse intermediate variable
Hi Oierpa,

What Pinkman is suggesting is that you do this:

Code:
<<
  0 -> R   @ Dummy value to go into R which gets overwritten later
  <<
    "Ins C t A " {
    ":C:
     :t:
     :A:" {1 0} V} INPUT OBJ-> -> C t A 
    << 'C* (t/100/n)/(1-(1+t/100/n)^(-n*A))' ->NUM 'R' STO
        'R*A*12' ->NUM "M" ->TAG 
    >> 
  >>
>>

Note that during the INPUT, if the user recalls 'R' then they get the local zero value.
Find all posts by this user
Quote this message in a reply
12-07-2022, 08:35 PM
Post: #7
RE: Store and reuse intermediate variable
Thank you BruceH!

Yes, once a local var is declared, you can update its value with the same syntax than with global vars.

Look at line 5: adding a 0 to be stored in local var R
Look at line 6: R is appended

Code:

« "Ins C t A " {
":C:
:t:
:A:" { 1 0
} V } INPUT OBJ→ 0
→ C t A R
  « 'C*(t/100/n)/(1
-(1+t/100/n)^(-n*A)
)' →NUM 'R' STO
'R*
A*12' →NUM "M" →TAG
  »
»

Thibault - not collector but in love with the few HP models I own - Also musician : http://walruspark.co
Find all posts by this user
Quote this message in a reply
12-09-2022, 06:41 PM
Post: #8
RE: Store and reuse intermediate variable
Hi all.
Excuse me for delay, I was out for some days for my job.
I just tested program with your suggestions and it works!

Thanks a lot!
So in the future, if I have other variabiles (more than one) I've to inizialize with a dummy value, right?

I would to convert some program in RPL.

Thanks again!
Find all posts by this user
Quote this message in a reply
12-12-2022, 03:16 PM
Post: #9
RE: Store and reuse intermediate variable
Hi all.
I just make a small variant to my simple program, in order to be general, here the code:
Code:

<<"Ins C t A n " {
":C:
:t:
:A:
:n:" {1 0} V} INPUT OBJ-> 0 -> C t A n R
<<'C* (t/100/n)/(1-(1+t/100/n)^(-n*A))' -> NUM 'R' STO R
>> 
>>
Now I can manage properly local variables, but with this code, where n (number of periods of mortgage) is a variable and not a fixed value.
In this version of program, I've the problem that, even if I wrote -> NUM, I can see an algebraic result (the formula) and not the results number.
I don't understood why, so after some tests I modified in this version;
Code:

<<"Ins C t A n " {
":C:
:t:
:A:
:n:" {1 0} V} INPUT OBJ-> 0 0 -> C t A n R P
<<'n*A' -> NUM 'P' STO P 'C* (t/100/n)/(1-(1+t/100/n)^(-P))' -> NUM 'R' STO R
>> 
>>
In this way, program is ok. In the stack, at the end, I can see number of period, morgage installment, and total debt.
Why if in the exponent I keep 'n*A' indeed P (=n*A) doesn't work?
Actually I don't need to display also "P" during calculation, in other programs, if I have to calculate intermediate values, is not important see the value on the stack.
Thanks in advance.
Find all posts by this user
Quote this message in a reply
12-20-2022, 07:14 AM
Post: #10
RE: Store and reuse intermediate variable
You could also just duplicate the value with DUP before creating the local variable R:
Code:
« "Ins C t A n" {
":C:
:t:
:A:
:n:
" { 1 0 } V } INPUT OBJ→
→ C t A n
  « 'C*t/100/n/(1-(1+t/100/n)^(-n*A))'
    →NUM "R" →TAG DUP → R
    'R*A*12' →NUM "M" →TAG
  »
»

Just noted that you are now using → NUM instead of →NUM in your listing.
That could be the reason why you got an algebraic result.
Find all posts by this user
Quote this message in a reply
12-20-2022, 08:48 AM
Post: #11
RE: Store and reuse intermediate variable
Thanks Thomas.
It was my error writing "-> NUM" indeed of "->NUM".
Thanks also for "DUP" suggestion.

Merry Christmas to everyone!
Find all posts by this user
Quote this message in a reply
Post Reply 




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