Assign local variable inside for/start loop
03-17-2021, 08:27 AM
Post: #1
 BINUBALL Member Posts: 133 Joined: Mar 2021
Assign local variable inside for/start loop
I tried to calculate pi with Gauss - Legendre algorithm. So this program input repetition count. there is program code below.
Code:
<< 1 2 √ INV 4 INV -> N A B T << 0 N FOR X A B + 2 / A B * √ T 2 X ^ A A B + 2 / - SQ * - -> A B T NEXT >> >>
It error at "-> A B T NEXT". However I have to assign new a, b, t values for calculation.
Of course, I can assign global variable like 'A' STO, but I want to use local variable. Can't I assign a local variable inside a loop?

S.Korean / HP-50G | fx-570EX | fx-570CW | HP-200LX
03-17-2021, 09:20 AM
Post: #2
 MNH Member Posts: 226 Joined: May 2014
RE: Assign local variable inside for/start loop
How about using a compiled local variable?
03-17-2021, 10:19 AM
Post: #3
 BINUBALL Member Posts: 133 Joined: Mar 2021
RE: Assign local variable inside for/start loop
(03-17-2021 09:20 AM)MNH Wrote:  How about using a compiled local variable?
What is EXACTLY compiled local variable? I don't know it at all.

S.Korean / HP-50G | fx-570EX | fx-570CW | HP-200LX
03-17-2021, 01:35 PM (This post was last modified: 03-17-2021 01:36 PM by Joe Horn.)
Post: #4
 Joe Horn Senior Member Posts: 1,998 Joined: Dec 2013
RE: Assign local variable inside for/start loop
Since A, B and T already exist as local variables, don't create them again; just use STO as usual to store the new values into them. Replace the second -> A B T with something like 'A' STO 'B' STO 'T' STO (or in the opposite order, depending on the order that they are in the stack). This will store the desired values into the local variables, not into global ones. Also, note that after the inner program ends, those variables will cease to exist, so access them if needed immediately after the NEXT command.

<0|ɸ|0>
-Joe-
03-18-2021, 02:58 AM
Post: #5
 BINUBALL Member Posts: 133 Joined: Mar 2021
RE: Assign local variable inside for/start loop
(03-17-2021 01:35 PM)Joe Horn Wrote:  Since A, B and T already exist as local variables, don't create them again; just use STO as usual to store the new values into them. Replace the second -> A B T with something like 'A' STO 'B' STO 'T' STO (or in the opposite order, depending on the order that they are in the stack). This will store the desired values into the local variables, not into global ones. Also, note that after the inner program ends, those variables will cease to exist, so access them if needed immediately after the NEXT command.
It works. Thanks Joe.

S.Korean / HP-50G | fx-570EX | fx-570CW | HP-200LX
 « Next Oldest | Next Newest »

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