The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

[wp34s] E24 macro
Message #1 Posted by Nobby K. on 14 June 2013, 11:26 p.m.

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
      
Re: [wp34s] E24 macro
Message #2 Posted by Paul Dale on 14 June 2013, 11:41 p.m.,
in response to message #1 by Nobby K.

Nice program. Good to see some of the more unique features of the 34S are being used.

Pauli

      
Re: [wp34s] E24 macro
Message #3 Posted by Didier Lachieze on 15 June 2013, 2:11 a.m.,
in response to message #1 by Nobby K.

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
            
Re: [wp34s] E24 macro
Message #4 Posted by Paul Dale on 15 June 2013, 2:47 a.m.,
in response to message #3 by Didier Lachieze

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

                  
Re: [wp34s] E24 macro
Message #5 Posted by Nobby K. on 17 June 2013, 10:23 a.m.,
in response to message #4 by Paul Dale

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.

                        
Re: [wp34s] E24 macro
Message #6 Posted by Didier Lachieze on 20 June 2013, 7:51 a.m.,
in response to message #5 by Nobby K.

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

                              
Re: [wp34s] E24 macro
Message #7 Posted by Paul Dale on 20 June 2013, 7:47 p.m.,
in response to message #6 by Didier Lachieze

Would it be okay to add this version of the program to the 34S library?

- Pauli

                                    
Re: [wp34s] E24 macro
Message #8 Posted by Didier Lachieze on 21 June 2013, 2:42 a.m.,
in response to message #7 by Paul Dale

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

                                          
Re: [wp34s] E24 macro
Message #9 Posted by Paul Dale on 21 June 2013, 2:47 a.m.,
in response to message #8 by Didier Lachieze

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.

                                                
Re: [wp34s] E24 macro
Message #10 Posted by Didier Lachieze on 21 June 2013, 2:52 a.m.,
in response to message #9 by Paul Dale

Yes, I was editing my post as you were answering...

                                                      
Re: [wp34s] E24 macro
Message #11 Posted by Paul Dale on 21 June 2013, 2:55 a.m.,
in response to message #10 by Didier Lachieze

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

                                                            
Re: [wp34s] E24 macro
Message #12 Posted by Didier Lachieze on 21 June 2013, 3:34 a.m.,
in response to message #11 by Paul Dale

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.

                                                                  
Re: [wp34s] E24 macro
Message #13 Posted by Didier Lachieze on 21 June 2013, 6:57 a.m.,
in response to message #12 by Didier Lachieze

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.

                                                                        
Re: [wp34s] E24 macro
Message #14 Posted by Paul Dale on 22 June 2013, 12:54 a.m.,
in response to message #13 by Didier Lachieze

I mentioned the clearing of local registers above :-)

- Pauli

                                                                              
Re: [wp34s] E24 macro
Message #15 Posted by Didier Lachieze on 22 June 2013, 5:15 a.m.,
in response to message #14 by Paul Dale

Yes, I don't know how I missed it ...


[ Return to Index | Top of Index ]

Go back to the main exhibit hall