The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

local variables
Message #1 Posted by brooky on 19 Sept 2012, 7:04 a.m.

hi I need some help with local variables. I want create local variables within a program for example;

<< -> A B << b a - 5 / 'c' ->tag b c + 'd' ->tag >> >>

what i get on the stack after exacution is (a=5 b=6)

2: 0.2 1: '6+c'

instead of 1: 6.2

Thanks

      
Re: local variables
Message #2 Posted by Neil Hamilton (Ottawa) on 19 Sept 2012, 7:14 a.m.,
in response to message #1 by brooky

Hi,

I am pretty sure that this is case sensitive so you "A B" is not the same as "a b".

Hope that helps.

      
Re: local variables
Message #3 Posted by Marcus von Cube, Germany on 19 Sept 2012, 7:35 a.m.,
in response to message #1 by brooky

I can't see a definition for the variable c. The ->tag command just labels entries on the stack. It does not store values or create variables.

      
Re: local variables
Message #4 Posted by Namir on 19 Sept 2012, 8:24 a.m.,
in response to message #1 by brooky

Perhaps

<< -> A B << b a - 5 / 'c' ->tag b c + 'd' ->tag >> >>

Should be:

<< 0 -> c A B << b a - 5 / 'c' STO 'c' ->tag b c + 'd' ->tag >> >>

Yes?

Namir

My RPL is very rusty .. :-(

            
Re: local variables
Message #5 Posted by Gilles Carpentier on 19 Sept 2012, 9:24 a.m.,
in response to message #4 by Namir

Hi Namir

 << 0 -> c A B << b a - 5 / 'c' STO 'c' ->tag b c + 'd' ->tag >> >>
Yes?

No ;) Could be :

<< 0 -> a b c << b a - 5 / 'c' STO b c + 'd' ->tag >> >>

But this is unnecessarely complex (the c varaible has no interest here)

      
Re: local variables
Message #6 Posted by Gilles Carpentier on 19 Sept 2012, 9:14 a.m.,
in response to message #1 by brooky

Hi,

as Marcus wrote, the ->TAG function is just for labels data on the stack.

There are several way for your calculation :

<< -> a b '((b-a)/5)+b' >>
<< -> a b << b a - 5 / b + >> >>
or just 
<< DUP ROT - 5 / + >>

Not that, in all cases, if you name your program for example 'f', you can use it in RPN or Algrabraic :

5 6 f
'f(5,6)' EVAL
or just
`f(5,6)`

The result will be 6.2

I recommand you to debug each program to see what happens

PS : My RPL is for 49 and 50G.

Edited: 19 Sept 2012, 1:40 p.m. after one or more responses were posted

            
Re: local variables
Message #7 Posted by C.Ret on 19 Sept 2012, 10:17 a.m.,
in response to message #6 by Gilles Carpentier

 -> a b '((b-a)/5)+b'            is pure User Function Format
 -> a b  b a - 5 / b +         is pure User Programm Format
 DUP ROT - 5 / +                 is pure User Reverse Polish Language

Again Gilles you make it right !

Translate into RPN this will give (may vary depending of exact calculator)

Lbl F - Lastx x:y 5 / + R/S

If as brooky you persist in using a third variable, you just have to embed the local variable structure :

 -> a b  b a - 5 / -> c  c b +   

But, as Gilles already say it, it is unnecessarily complex since the intermediate c variable has no interest here.

Edited: 19 Sept 2012, 10:20 a.m.

      
Re: local variables
Message #8 Posted by David Hayden on 20 Sept 2012, 3:53 p.m.,
in response to message #1 by brooky

This does what you seem to want from your example, but why are you storing into the local variable 'd' when it disappears at the end of the program?

 0. 0.  @ put two dummy values on the stack for locals c & d
   A B c d
  
   B A - 5 / 'c' STO
   B 'c' EVAL +         @ locals need EVAL
   DUP 'd' STO
  

            
Re: local variables
Message #9 Posted by brooky on 24 Sept 2012, 7:08 a.m.,
in response to message #8 by David Hayden

The simple fromula was only example sorry for the confussion. Can you create local variables during a program or do they have to be stored as global ones. ie STO

                  
Re: local variables
Message #10 Posted by David Hayden on 25 Sept 2012, 12:25 a.m.,
in response to message #9 by brooky

You create local variables with the -> << >> construction. Once creat4ed you can store to them with STO just like with global variables. In other words 'XYZ' STO will store into the local variable XYZ if it exists, and otherwise it will store into the global variable. Does this help?

Dave


[ Return to Index | Top of Index ]

Go back to the main exhibit hall