The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

My First Prime Program
Message #1 Posted by Jeff O. on 30 Oct 2013, 1:29 p.m.

No, it's not "Hello World" :-)

Inspired by one of Eddie's blogs (thanks Eddie!), I tried to write a program to take values from the stack and return values to the stack when executed from the command line in RPN home mode. The goal is the ever-popular rectangular to polar and polar to rectangular conversions. Figuring I'd better try to crawl before walking, I started by just trying to take two values from the stack and return a result based on those values:

EXPORT POL(X,Y)
BEGIN
RETURN ATAN(X/Y);
END;

I typed the above in, went to the home screen in RPN mode, keyed in 3 ENTER 4 ENTER POL ENTER. Hallelujah! 36.8699 was returned to the stack on level 1! Excited with my success, I edited my program as follows:

EXPORT POL(X,Y)
BEGIN
RETURN ATAN(X/Y);
RETURN SQROOT(X2+Y2);
END;

There were no syntax errors, so I hoped the first return statement would put the angle on the stack as before, and the second would push the magnitude. Unfortunately, 3 ENTER 4 ENTER POL ENTER yielded only the 36.8699 result as before. If I comment out the first RETURN statement, I get the correct answer of 5 for the magnitude. So, is there any way to have the program calculate and push both values onto the stack?

      
Re: My First Prime Program
Message #2 Posted by Patrice on 30 Oct 2013, 1:37 p.m.,
in response to message #1 by Jeff O.

RETURN is quiting the sub-program when encountered. So no second RETURN

            
Re: My First Prime Program
Message #3 Posted by Jeff O. on 30 Oct 2013, 1:57 p.m.,
in response to message #2 by Patrice

I guess I thought or hoped that RETURN meant "return this value to somewhere", in this case the stack if run from home in RPN mode.

      
Re: My First Prime Program
Message #4 Posted by Tim Wessman on 30 Oct 2013, 1:40 p.m.,
in response to message #1 by Jeff O.

Unfortunately not at this time. Since the programming language is function based, like essentially all function based programming languages it only allows a single return item. Exploring ways to allow you to push multiple results...

TW

            
Re: My First Prime Program
Message #5 Posted by Jeff O. on 30 Oct 2013, 1:56 p.m.,
in response to message #4 by Tim Wessman

Thanks, looking forward to it.

            
Re: My First Prime Program
Message #6 Posted by Patrice on 30 Oct 2013, 2:16 p.m.,
in response to message #4 by Tim Wessman

a
RETURN ATAN(X/Y), SQROOT(X2+Y2);
would have been very convenient :)

along with
A,B:= POL(X,Y);
for sure.

Edited: 30 Oct 2013, 2:16 p.m.

                  
Re: My First Prime Program
Message #7 Posted by Han on 30 Oct 2013, 2:42 p.m.,
in response to message #6 by Patrice

Quote:
a
RETURN ATAN(X/Y), SQROOT(X2+Y2);
would have been very convenient :)

Just use

RETURN { ATAN(X,Y), (X^2+Y^2)^(1/2) };

You can also use local variables ore even lists to store values temporarily until you are ready to RETURN all the results in a single list. So you can even do something like:

L1:={}; // initialize to empty list
L1(1):=ATAN(X/Y);
L1(2):=(X^2+Y^2)^(1/2);
RETURN L1;

You can even refer to the results using L1(1) from the command line. That said, I do agree that it would be nice to be able to PUSH() results to the stack.

Edited: 30 Oct 2013, 2:43 p.m.

                        
Re: My First Prime Program
Message #8 Posted by Patrice on 30 Oct 2013, 3:03 p.m.,
in response to message #7 by Han

My point is that some languages use this syntax to allow to return more than 1 value from a function.

                        
Re: My First Prime Program
Message #9 Posted by Eddie W. Shore on 30 Oct 2013, 3:28 p.m.,
in response to message #7 by Han

Quote:

Just use

RETURN { ATAN(X,Y), (X^2+Y^2)^(1/2) };

You can also use local variables ore even lists to store values temporarily until you are ready to RETURN all the results in a single list. So you can even do something like:

L1:={}; // initialize to empty list
L1(1):=ATAN(X/Y);
L1(2):=(X^2+Y^2)^(1/2);
RETURN L1;

You can even refer to the results using L1(1) from the command line. That said, I do agree that it would be nice to be able to PUSH() results to the stack.


I recommend what Han suggests, use a list with RETURN to return multiple results.

And thanks for the compliment Jeff O. :)

Edited: 30 Oct 2013, 3:30 p.m.

                              
Re: My First Prime Program
Message #10 Posted by Jeff O. on 30 Oct 2013, 4:09 p.m.,
in response to message #9 by Eddie W. Shore

Is there an easy (or any) way to decompose the list into separate values on stack levels 1 and 2? (If I key L1(1) into the command line with a list of {1.0000,2.0000} on level 1 of the stack, I get an "X L1({1.0000,2.0000})" pop-up message. I probably don't know what "using L1(1) from the command line" means.)

You are welcome Eddie. Your blogs have been quite helpful. Give me some hope that I might someday be able to do something useful with a Prime.

            
Re: My First Prime Program
Message #11 Posted by Pal G. on 30 Oct 2013, 5:07 p.m.,
in response to message #4 by Tim Wessman

Lua?


[ Return to Index | Top of Index ]

Go back to the main exhibit hall