what's wrong with this program? / STO error



#13

So I purchased a 50g a couple of months ago partly based on this forum's advice, (thank you), and really like it, especially rpn. So I've finally gotten around to trying to program it some. I've written a lot of simple programs for various purposes on my old ti-84, and I wanted to see how much faster rpl is than ti basic by writing a simple prime number finding program where you input a number and the program finds all primes up to that number. This is what I've written:
<< -> X
<< 3 X
FOR B B 1 - S STO 5 M STO 2 S
FOR C
IF B C MOD 0 ==
THEN 1 M STO
END
NEXT
IF M 0 &#8800 [does not = symbol];
THEN 0 M STO B
END
NEXT
>>
>>
(I tried to keep some formatting and stuff so it's easier to read but it just has spaces on the calc on the stack, and the stuff in brackets isn't in the program).

So every time I run this I get the message "STO error: bad argument type." I can't figure out what is causing this, even though I've tried changing a lot of the STO arguments around and stuff I still get the same error. I was thinking that maybe the debug function could pinpoint what is causing this but I can't figure out how to make that work. I would really appreciate if someone would point out what I'm doing wrong.

BTW I'm aware that this really is not an efficient algorithim at all, but right now I'm just trying to learn enough rpl so that I can do all the stuff I knew how to do on my ti 84 on my 50g.

Thanks again


#14

To get the STO working, you might need to include 'M' instead of M.
I'm not sure what the M in the below program is for. it looks like it might not be necessary at all. Can you share a little bit about what the program is used for or provide a pseudocode explanation for these loops?

<< -> X 
<< 3 X FOR B
B 1 - S STO 5 M STO
2 S FOR C
IF B C MOD 0 == THEN 1 M STO END
NEXT
IF M 0 <> THEN 0 M STO B END
NEXT
>>
>>

In many cases it is not necessary to use the STO function on a 50g (or other RPL calculator) to keep intermediate values. Try writing the routine to only use the stack- it will make the program much faster and likely more elegant.


#15

Thanks Allen for the help. I didn't realize that inserting quotation marks could make the difference it did - after putting quotation marks around whatever variable names I wanted to be treated as symbolic and not putting them around variable names that I wanted to be treated as the number they represented I got the program to work in short order.

To try to clarify what I was asking (though I already got it working) the program is supposed to find all prime numbers up to the first value on the stack. Since I couldn't figure out how to insert an early exit command for loops in rpl, I used 'M' as a value which basically represents whether or not the number 'B' has any factors. If it does not, the number is copied to the stack.

Thanks again for the help and now I guess I'll do some more reading and try to figure out how to rewrite this using the stack.

Just as a side-note, is there a way to exit a loop early in rpl? Because I just realized that this program is going to crawl as the numbers get bigger because, even if it's divisible by 2, the program is still going to check for every number up to the number being checked. (Actually I need to change it so it stops looking for factors at half the number being checked.)


Edited: 30 Aug 2009, 3:49 p.m.


#16

Quote:
it stops looking for factors at half the number being checked

Not half, square root.

#17

The quotes give you a reference to the variable. Unquoted, you get the variable's value placed on the stack.


#18

Unfortunately, it is more complicated than that.

If M is unquoted, then:

  • If M is already defined (i.e., contains a value), then M means the number.
  • If M is not already defined, then M means the symbol.
If M is quoted, as in 'M', it always means the symbol.

This means that the sequence "3 M STO" does different things depending on whether M already contains a value or not: if M does not contain a value, it puts 3 into M; otherwise, it causes an error.

There is this kind of craziness all around the 50g; in general, it is NOT true that the outcome of an operation depends on its operands only. There was a thread some time ago on the way the result of CAS functions depended on which variables were accessible! I wish the designers took good programming language design in mind when coming up with the 50g. To me, it feels that its software has been hastily put together.

Luca


#19

Quote:
if M is already defined (i.e., contains a value), then M means the number
Really? And which "number" exactly it means? What if M consisted of a string, matrix, vector, list, library etc. etc. ?

Quote:
I wish the designers took good programming language design in mind when coming up with the 50g.
??? What does that suppose to mean? That RPL is not good enough for you? If that's the case there are SysRPL and C. Still not happy?

#20

Quote:

??? What does that suppose to mean? That RPL is not good enough for you? If that's the case there are SysRPL and C. Still not happy?


Yes, that's exactly right. RPL is clearly not good enough for me; I think it is poorly designed. It has two faults.

  • The large fault is that a postfix notation does not make much sense in a structured language setting. As others have remarked, there is an obfuscated C context, but with RPL, you don't need the motivation of a context -- the code comes out naturally obfuscated.
  • The smaller, more technical fault is that the effect of too many commands, such as CAS commands and STO, depends on the context. Properly designed stack languages, such as Forth, avoid such side dependencies.
SysRPL and C are even worse - I mean, who wants to bother with such low-level stuff when writing code on a programmable calculator?
RPN programming is easy enough for very small tasks. For bigger tasks, if I want a pocketable device, I am waiting till I can simply write Python, Ruby, on my Android cellphone; I guess it will not be long. Those languages are in another universe, regarding usability.


#21

You keep messing up RPL and CAS. CAS has nothing to do with RPL which is a unique language specially designed for HP series of calculators like HP28 and HP48. For many years it has proven it's usefulness. The creators of RPL were nothing short but geniuses. The fact, that nothing better was designed later and RPL platform was used for (to my view) running school kids' stuff like CAS etc. is not a reason to be criticized by people with limited knowledge and abilities in this area. If you know what's best and how to do it, what are you waiting for? Go ahead develop new calculator and proper language for it. HP are waiting for you.

#22

Quote:
I wish the designers took good programming language design in mind when coming up with the 50g.

It would be just as helpful if the users used good programming practice.

I.e.
(1) ensure your variables are defined before calling/manipulating them (this was hammered into me as one of the most important practices of good programming, in C if you attemp to use an undefined variable you also get an error).
(2) be _consistent_ when calling your variable, thus always use quotes when referring to the symbol and no quotes when when referring to it's value (making sure you have defined the value!).
#23

Luca,

I you wish to continue RPL programming on the 50G, may I point you to the 50G Manuals page (linked for convenience) and suggest you refer to the "HP 49g+ & 48gII_Advanced User's Reference Manual" listed under General Reference. This is an RPL programming reference.

On p1_1 there is a table that clearly states the impact of quoted and unquoted names.

Bart

#24

Quote:
Just as a side-note, is there a way to exit a loop early in rpl?

KILL

#25

Quote:
Just as a side-note, is there a way to exit a loop early in rpl?
Not in a way elegant as the "break" statement in the C language, unfortunately.
#26

Quote:
Just as a side-note, is there a way to exit a loop early in rpl?

The short answer is no.

The longer answer is to rethink the way you program so that you never need to. In this case, a loop structure that has a continue condition, like do-while, should be used.

The other answer is yes, you can write bad FORTRAN in any language, even one as lovely as RPL, so there's a horrible kludgey way to leave a loop. Enclose the loop in an IFERR <your loop> THEN <optional stuff> END block, then inside your loop you can do IF <cond> THEN 0 DOERR END, and the program will jump to whatever is after the THEN (or END if there is nothing after THEN). Try not to do this, please.

#27

It is easy to exit early from a FOR loop on the 50g, for example

1 10 FOR j
(do something)
(if finished before 10 cycles then)
20 'j' STO
NEXT


#28

Well, as others have said, there is no formal way of breaking out of a for loop and with good reason - doing so is bad programming because it means you are using the wrong structure. There is also no need for it because the designers kindly built in condition based structures for exactly this type of reason: DO...UNTIL and WHILE...REPEAT depending on whether you want the condition tested at the start or end of the loop.

Having said all that, a BREAK statement or equivalent could be useful at times and even though using this is bad practise, will get frowned upon etc., I don't consider it to be such a huge crime as is sometimes said. If you are going to break rules, as long as you do it elegantly and with good reason, it can give you more flexibility and even simplify the code.

Mark


#29

Hi Mark. I'm not so sure that the formal requirements inherent in structured programming apply--or should apply--to calculator programming. Good grief, if it's a mainframe program and other programmers may have to make changes to it in the future, then structured programming is appropriate, along with verbose and meaningful comments. But in calculator programming, or programs we write ourselves for ourselves, an emphasis on structured programming may be a bit misplaced.

I once wrote an article on the ultimate in un-structured programming for the 17b solver, in which I described how to exit a loop early by doing a divide by 0. For calculators, just do what works for you.

After all, how could one expect structured programming on a calculator like the 12c, with one of the keys labelled "GOTO"?

: )


#30

Hi Don,

I may not have put my point over as clearly as I wanted!

My first paragraph was a purist view - thou shalt not do this.

My second paragraph was being more realistic and admitting that it is done.

However, I still think it is very important to write "elegant" code. All my comments were in relation to UserRPL and not regular step programming where anything goes (the thread being about a 48 program).

I consider step programming to be a bit like doing 8 bit assembler with a fancy CPU and quite low level. In those circumstances, you do whatever you need to do to get the job done - there are no rules.

UserRPL on the other hand is a distinct high-level language which like it or not, imposes structures on the program that you have to follow.

Anyway, as UserRPL doesn't allow loop-breaking by keyword, this is all a bit moot but I still maintain that using the correct loop structure is very important (but not mandatory!).

Mark

Quote:
Hi Mark. I'm not so sure that the formal requirements inherent in structured programming apply--or should apply--to calculator programming. Good grief, if it's a mainframe program and other programmers may have to make changes to it in the future, then structured programming is appropriate, along with verbose and meaningful comments. But in calculator programming, or programs we write ourselves for ourselves, an emphasis on structured programming may be a bit misplaced.

I once wrote an article on the ultimate in un-structured programming for the 17b solver, in which I described how to exit a loop early by doing a divide by 0. For calculators, just do what works for you.

After all, how could one expect structured programming on a calculator like the 12c, with one of the keys labelled "GOTO"?

: )



#31

Thanks Mark. Yes, I was really talking about pure RPN keystroke programming. I had a 49g+ for awhile a few years ago, but its RPL programming was something I could not get used to. I know RPL is a powerful programming language, but for me there are just too many darned commands! I like the simplicity of RPN.

Katie Wasserman and I have been discussing the joys of RPN programming using as few registers and lines of code as possible. I normally just use what I need without paying a lot of attention to the stack, but I recently wrote a small program on the 12c (15 lines) that used two registers, and when I started analyzing the stack usage I realized I could get away with using no registers. I admit, that felt very good!

#32

Quote:
...there is no formal way of breaking out of a for loop and with good reason - doing so is bad programming because it means you are using the wrong structure. There is also no need for it because the designers kindly built in condition based structures for exactly this type of reason: DO...UNTIL and WHILE...REPEAT

Hmm -- what if the loop-breaking condition is determined by a non-straightforward calculation using a datum that is read-in upon each executed step of the loop? There is no predictable pattern to the iterations of the loop, and to express the condition as a one-liner would be cumbersome.

Quote:
Having said all that, a BREAK statement or equivalent could be useful at times and even though using this is bad practise, will get frowned upon etc., I don't consider it to be such a huge crime as is sometimes said. If you are going to break rules, as long as you do it elegantly and with good reason, it can give you more flexibility and even simplify the code.

Perhaps this is one such instance...

-- KS

#33

Error correction, need an END for the IF:-

1 10 FOR j (do something) IF (finished before 10 cycles) THEN 20 'j' STO END NEXT

Might as well get it right.


#34

Hey thanks everyone for the help. As you probably noticed, I am definitely a beginner at programming and stuff, so thank you. Especially P.Hart - your tip sped up my program by a ton :-). Yeah so this is what I have now and it works fine...

<< -> V X << V X FOR B B [square root symbol] 1 + 'S' STO 0. 'M' STO 2. S FOR C IF B C MOD 0. == THEN 1. 'M' STO S 1 + 'C' STO END NEXT IF M 1 = THEN 0. 'M' STO B END NEXT >> >>

I don't know why the = sign after M 1 = has to be a = instead of a ==, but the double equal signs don't work on my calc. Also, I think the values which you input on the stack as the range of numbers within which to search have to have a period (.) after them.

BTW just in case for anybody who doesn't have the hlp49 library on their 50gs or 49g+s, it's been really helpful to me and contains syntax and explanations for something around 600 non CAS commands - similar to the built in help system for the CAS... so you don't have to lug around a manual everywhere! (I know this is random... just trying to help anyone if I can... :-))


Possibly Related Threads...
Thread Author Replies Views Last Post
  MS advert shows spreadsheet with obvious error BruceH 3 1,103 11-14-2013, 09:50 AM
Last Post: Bill (Smithville, NJ)
  HP Prime: run a program in another program Davi Ribeiro de Oliveira 6 1,240 11-11-2013, 08:28 PM
Last Post: Davi Ribeiro de Oliveira
  [HP-Prime - Geometry App] What is wrong with this ? Jean-Michel 2 726 10-27-2013, 06:26 PM
Last Post: Jean-Michel
  HP Prime: Rounding error in determinant Stephan Matthys 3 826 10-25-2013, 09:29 PM
Last Post: Walter B
  Prime Error or Mine? toml_12953 12 1,875 10-22-2013, 10:35 AM
Last Post: toml_12953
  HP Prime: What's wrong with this program? BruceTTT 6 997 10-16-2013, 11:14 AM
Last Post: Eddie W. Shore
  Explaination on How to Reset Caculator in Users guie: error Harold A Climer 5 1,199 10-15-2013, 02:11 AM
Last Post: cyrille de Brébisson
  Repair of HP-34C - Error 0 and Error 9 Jeff Kearns 3 854 10-11-2013, 12:29 PM
Last Post: Randy
  Do You Think a Listing Error Was Made? Jim Johnson 13 1,893 09-04-2013, 09:23 AM
Last Post: Eddie W. Shore
  HP Prime - How to STO in RPN mode? Javier Goizueta 10 1,316 08-18-2013, 10:24 PM
Last Post: Xavier A. (Brazil)

Forum Jump: