Posts: 20
Threads: 4
Joined: Nov 2011
Hi, all.
I make new macro which round number to E24 series, such as 47k[ohm].
It is seems to be useful in electronic design.
You can download from my github page.
https://github.com/nkon/wp34s-E24/blob/master/E24.wp34s
Please send your advice or pull-request to refactoring the macro.
I want to use local table. Lateral and branch is used in current version.
Thank you.
/*
Input: X reg
Output: X reg <- E24(Input)
E24: 1.0 1.1 1.2 1.3 1.5 1.6 1.8 2.0 2.2 2.4 2.7 3.0
3.3 3.6 3.9 4.3 4.7 5.1 5.6 6.2 6.8 7.5 8.2 9.1
*/
LBL'E24'
SSIZE8
EXPT
RCL L
MANT
// X = MANT(orig X), Y=EXPT(orig X)
# 010 // begin
# 011
XEQ 00
RTN // end
# 011 // begin
# 012
XEQ 00
RTN // end
# 012 // begin
# 013
XEQ 00
RTN // end
# 013 // begin
# 015
XEQ 00
RTN // end
# 015 // begin
# 016
XEQ 00
RTN // end
# 016 // begin
# 018
XEQ 00
RTN // end
# 018 // begin
# 020
XEQ 00
RTN // end
# 020 // begin
# 022
XEQ 00
RTN // end
# 020 // begin
# 024
XEQ 00
RTN // end
# 024 // begin
# 027
XEQ 00
RTN // end
# 027 // begin
# 030
XEQ 00
RTN // end
# 030 // begin
# 033
XEQ 00
RTN // end
# 033 // begin
# 036
XEQ 00
RTN // end
# 036 // begin
# 039
XEQ 00
RTN // end
# 039 // begin
# 043
XEQ 00
RTN // end
# 043 // begin
# 047
XEQ 00
RTN // end
# 047 // begin
# 051
XEQ 00
RTN // end
# 051 // begin
# 056
XEQ 00
RTN // end
# 056 // begin
# 062
XEQ 00
RTN // end
# 062 // begin
# 068
XEQ 00
RTN // end
# 068 // begin
# 075
XEQ 00
RTN // end
# 075 // begin
# 082
XEQ 00
RTN // end
# 082 // begin
# 091
XEQ 00
RTN // end
# 091 // begin
# 100
XEQ 00
RTN // end
# 100 // begin
# 110
XEQ 00
RTN // end
LBL 00 // ROUND
LocR 001
x[<->] Y
STO .00
+
# 020
/
x<=? Y
SKIP 008
DROP // Drop threshold
DROP // Drop mantissa
1
-
10[^x]
RCL .00 // Recall rounded mantissa
[times]
RTN // Good Return
DROP // SKIP 008 come here.Drop Threashold
RTN+1 // Next parameter
END // Program Separator
Posts: 3,229
Threads: 42
Joined: Jul 2006
Nice program. Good to see some of the more unique features of the 34S are being used.
Pauli
Posts: 248
Threads: 5
Joined: Feb 2008
Interesting usage of some of the WP 34S specific functions.
Any reason for the SSIZE8 at the beginning?
It can be slightly optimized to avoid duplicated entry of each E24 value:
/*
Input: X reg
Output: X reg <- E24(Input)
E24: 1.0 1.1 1.2 1.3 1.5 1.6 1.8 2.0 2.2 2.4 2.7 3.0
3.3 3.6 3.9 4.3 4.7 5.1 5.6 6.2 6.8 7.5 8.2 9.1
*/
LBL'E24'
EXPT
RCL L
MANT
// X = MANT(orig X), Y=EXPT(orig X)
# 010 // begin
# 011
XEQ 00
RTN // end
# 012 // begin
XEQ 00
RTN // end
# 013 // begin
XEQ 00
RTN // end
# 015 // begin
XEQ 00
RTN // end
# 016 // begin
XEQ 00
RTN // end
# 018 // begin
XEQ 00
RTN // end
# 020 // begin
XEQ 00
RTN // end
# 022 // begin
XEQ 00
RTN // end
# 024 // begin
XEQ 00
RTN // end
# 027 // begin
XEQ 00
RTN // end
# 030 // begin
XEQ 00
RTN // end
# 033 // begin
XEQ 00
RTN // end
# 036 // begin
XEQ 00
RTN // end
# 039 // begin
XEQ 00
RTN // end
# 043 // begin
XEQ 00
RTN // end
# 047 // begin
XEQ 00
RTN // end
# 051 // begin
XEQ 00
RTN // end
# 056 // begin
XEQ 00
RTN // end
# 062 // begin
XEQ 00
RTN // end
# 068 // begin
XEQ 00
RTN // end
# 075 // begin
XEQ 00
RTN // end
# 082 // begin
XEQ 00
RTN // end
# 091 // begin
XEQ 00
RTN // end
# 100 // begin
XEQ 00
RTN // end
# 110 // begin
XEQ 00
RTN // end
LBL 00 // ROUND
LocR 002
STO .01
x[<->] Y
STO .00
+
# 020
/
x<=? Y
SKIP 008
DROP // Drop threshold
DROP // Drop mantissa
1
-
10[^x]
RCL .00 // Recall rounded mantissa
[times]
RTN // Good Return
DROP // SKIP 008 come here.Drop Threashold
RCL .01 // Recall Upper value
RTN+1 // Next parameter
END // Program Separator
Posts: 3,229
Threads: 42
Joined: Jul 2006
Another possibility would be to get rid of the:
# nnn
XEQ 00
RTN
sequences and use the CASE command instead:
LBL 01
CASE .00
# nnn
GTO 00
# nnn
GTO 00
Where LBL 00 increments .00 by 2 and a local frame of one register is allocated at the start of the routine and LBL 00 goes back to LBL 01 instead of using RTN+1.
- Pauli
Posts: 20
Threads: 4
Joined: Nov 2011
Thanks, Didier.
That is simple and good idea.
Pauli's idea using CASE command is quite difficult for me.
I'm trying that, but take a little time.
Posts: 248
Threads: 5
Joined: Feb 2008
Here is Pauli's idea using CASE:
/*
Input: X reg
Output: X reg <- E24(Input)
E24: 1.0 1.1 1.2 1.3 1.5 1.6 1.8 2.0 2.2 2.4 2.7 3.0
3.3 3.6 3.9 4.3 4.7 5.1 5.6 6.2 6.8 7.5 8.2 9.1
*/
LBL'E24'
LocR 003
0
STO .00
DROP
EXPT
RCL L
MANT
// X = MANT(orig X), Y=EXPT(orig X)
# 010 // begin
LBL 01
CASE .00
# 011
GTO 00
# 012
GTO 00
# 013
GTO 00
# 015
GTO 00
# 016
GTO 00
# 018
GTO 00
# 020
GTO 00
# 022
GTO 00
# 024
GTO 00
# 027
GTO 00
# 030
GTO 00
# 033
GTO 00
# 036
GTO 00
# 039
GTO 00
# 043
GTO 00
# 047
GTO 00
# 051
GTO 00
# 056
GTO 00
# 062
GTO 00
# 068
GTO 00
# 075
GTO 00
# 082
GTO 00
# 091
GTO 00
# 100
GTO 00
# 110
LBL 00 // ROUND
STO .02
x[<->] Y
STO .01
+
# 020
/
x<=? Y
SKIP 008
DROP // Drop threshold
DROP // Drop mantissa
1
-
10[^x]
RCL .01 // Recall rounded mantissa
[times]
RTN // Good Return
DROP // SKIP 008 come here.Drop Threashold
RCL .02 // Recall Upper value
INC .00
INC .00
GTO 01 // Next parameter
END // Program Separator
Posts: 3,229
Threads: 42
Joined: Jul 2006
Would it be okay to add this version of the program to the 34S library?
- Pauli
Posts: 248
Threads: 5
Joined: Feb 2008
Yes, and to save a few steps you can replace the sequence:
SKIP 008
DROP // Drop threshold
DROP // Drop mantissa
1
-
by:
SKIP 006
[<->] ZTTT // Drop threshold & mantissa
DEC X
Edited: 21 June 2013, 2:48 a.m. after one or more responses were posted
Posts: 3,229
Threads: 42
Joined: Jul 2006
And the 1 - by DEC X after those lines.
Also the 0 STO .00 DROP at the start -- local registers are initialised to zero automatically.
- Pauli
Edited: 21 June 2013, 2:48 a.m.
Posts: 248
Threads: 5
Joined: Feb 2008
Yes, I was editing my post as you were answering...
Posts: 3,229
Threads: 42
Joined: Jul 2006
An extra step can also be saved by reversing the last two blocks of code and relying on the END to act as a RTN.
- Pauli
Posts: 248
Threads: 5
Joined: Feb 2008
Yes and with a few other optimizations this gives:
x>? Y
SKIP 004
x[<->] .02 // Recall Upper value & Drop Threshold
INC .00
INC .00
GTO 01 // Next parameter
[<->] ZTTT // Drop threshold & mantissa
DEC X
10[^x]
RCL[times] .01 // Recall rounded mantissa & multiply
END // Program Separator
(5 steps less than initial code)
And by moving the STO .01 just after the LBL 01 we can save another step:
LBL 01
STO .01
CASE .00
[...]
LBL 00 // ROUND
STO .02
+
# 020
/
Edited: 21 June 2013, 3:56 a.m.
Posts: 248
Threads: 5
Joined: Feb 2008
Another step saved at the beginning:
LBL'E24'
LocR 003
STO .00
STO- .00 // Clear local register 00
EXPT
EDIT: I just checked the E24.wp34s on sourceforge and there is nothing to clear register .00
After checking the manual I found the confirmation: "Newly allocated registers are cleared." , so no need for these steps between LocR & EXPT. But still one step could be saved in the sourceforge version by moving the STO .01
Edited: 21 June 2013, 7:11 a.m.
Posts: 3,229
Threads: 42
Joined: Jul 2006
I mentioned the clearing of local registers above :-)
- Pauli
Posts: 248
Threads: 5
Joined: Feb 2008
Yes, I don't know how I missed it ...