Post Reply 
[41,42] Perfect functions
05-13-2020, 01:07 PM (This post was last modified: 05-14-2020 06:17 AM by Werner.)
Post: #1
[41,42] Perfect functions
A 'perfect' function is one that mimicks the built-in functions:
  • save X in LastX
  • take the n arguments from the stack, compute the result in X
  • roll down the stack and replicate reg T n-1 times

Here, I present a few routines that pass these requirements.

1. Parallel Resistance:
In: X Y
Out: X*Y/(X+Y)
Code:
01 LBL"PAR"
02 STO L
03 X<>Y
04 ST+ Y
05 X<>Y
06 X!=0?
07 ST/ Y
08 X<> L
09 *
10 END

For the 42S, that can be shortened to:

Code:
00 { 17-Byte Prgm }
01▸LBL "PAR"
02 RCL+ ST Y
03 X≠0?
04 STO÷ ST Y
05 X<> ST L
06 ×
07 END

2. CEIL and FLOOR
Code:
01*LBL"CEIL"
02 INT
03 X<>Y
04 X<> L
05 X>Y?
06 ISG Y
07 X<=Y?
08 GTO 00
09*LBL"FLOOR"
10 INT
11 X<>Y
12 X<> L
13 X<Y?
14 DSE Y
15*LBL 00
16 X<> L
17 X<>Y
18 END

3. combine two percentages
1+r%/100 = (1+x%/100)*(1+y%/100) -> r%=x% +y% + x%*y%/100
In: x% y%
Out: r%
Code:
01*LBL"%*"
02 %
03 ST+ Y
04 X<> L
05 +
06 END

4. SINC
Sinc(x) = Sin(x)/x, or 1 if x=0
Code:
01 LBL"SINC"
02 SIN
03 X<> L
04 X!=0?
05 ST/ L
06 X<> L
07 X=0?
08 SIGN
09 END

Do you have more?

Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
05-13-2020, 03:32 PM (This post was last modified: 05-13-2020 03:33 PM by rprosperi.)
Post: #2
RE: [41,42] Perfect functions
Here's the first program I put into any new RPN machine I play with:

%T - Return the percent that X is of Y

Code:
01 LBL "%T"
02 1/X
03 %
04 1/X
05 RTN

Derived with Joe Horn one late Friday night at a PPC meeting, around 1982-3? Cliff Stern may have been involved too. We celebrated by going to get 'breakfast' at Denny's at around 2am when the meeting ended, a long tradition after PPC meetings at the time.

Edit: Forgot to say thanks Werner, for a handy new tool for the toolbox! Smile

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
05-13-2020, 04:32 PM
Post: #3
RE: [41,42] Perfect functions
While short and concise, it is not a perfect function, Bob!
Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
05-13-2020, 10:31 PM
Post: #4
RE: [41,42] Perfect functions
Oops. LastX is broken.... And to think I've been happy with it all these years for preserving the stack. Dang. On the other hand, while nice, I never thought of it as perfect.

No simple way I can see to fix that. I'll have to live with it, imperfect as it is.

Thanks.

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
05-13-2020, 11:02 PM (This post was last modified: 05-13-2020 11:10 PM by Sylvain Cote.)
Post: #5
RE: [41,42] Perfect functions
(05-13-2020 04:32 PM)Werner Wrote:  While short and concise, it is not a perfect function, Bob!
I do not know if you want to keep P2 in the stack or not, so I assume not

(05-13-2020 10:31 PM)rprosperi Wrote:  No simple way I can see to fix that.
Not that hard either. Wink

Code:
STK :  L  X  Y  Z  T
IN  :  l P1 P2  z  t
OUT : P1  R  t  t  t

Formula: R = (P1 / P2) × 100%

Code:
01 LBL "%T"
02 STO Z 
03 1/X
04 %
05 1/X
06 *
07 RUP
08 STO Y
09 X<> Z
10 X<> L
11 END
Find all posts by this user
Quote this message in a reply
05-13-2020, 11:49 PM
Post: #6
RE: [41,42] Perfect functions
Close, but no cigar...

This saves X into L, but you've lost the original Z. Sad

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
05-14-2020, 05:30 AM
Post: #7
RE: [41,42] Perfect functions
(05-13-2020 11:49 PM)rprosperi Wrote:  Close, but no cigar...
This saves X into L, but you've lost the original Z. Sad
You're tough ... Wink

2nd version
Code:
STK :  L  X  Y  Z  T
IN  :  l P1 P2  z  t
OUT : P1  R  z  t  t

Formula: R = (P1 / P2) × 100%

Code:
FOCAL___  >  _L_  _X_  _Y_  _Z_  _T_
LBL "%T"  >    l   12   60    z    t
/         >   12    5    z    t    t
Last X    >   12   12    5    z    t
X<>Y      >   12    5   12    z    t
1/X       >    5   .2   12    z    t
X<>Y      >    5   12   .2    z    t
Code:
STO L     >   12   12   .2    z    t
RDN       >   12   .2    z    t   12
100       >   12  100   .2    z    t
ST* Y     >   12  100   20    z    t
RDN       >   12   20    z    t  100
RCL Z     >   12    t   20    z    t
RDN       >   12   20    z    t    t
END       >   12   20    z    t    t
Find all posts by this user
Quote this message in a reply
05-14-2020, 06:20 AM (This post was last modified: 05-14-2020 06:54 AM by Werner.)
Post: #8
RE: [41,42] Perfect functions
In analogy to % and %CH, %T should also leave Y intact.
This comes close:

Code:
>LBL"%T"
 X<>Y
 ST+ Y
 X<>Y
 %CH
 %
 X<> L
 END
but LASTX may suffer from roundoff.
So that leaves

Code:
>LBL"%T"
 ABS
 CLX
 100
 X<>Y
 ST/ Y
 X<>Y
 X<> L
 ST* L
 X<> L
 END
In this case, I'd go for the imperfect, elegant solution though ;-)

Cheers, Werner
PS. Added SINC to the original post

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
05-14-2020, 12:36 PM
Post: #9
RE: [41,42] Perfect functions
(05-13-2020 11:02 PM)Sylvain Cote Wrote:  I do not know if you want to keep P2 in the stack or not, so I assume not

I missed this comment the first time, sorry. As Werner noted, keeping P2 (Y) is required to mimic the other % functions, but also because often you are still operating on the total, which is likely why HP chose to keep Y in the other % functions.

I need to check out the stackrobatics in the last one, but I'll keep the short one too.

Thank you both! Smile

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
Post Reply 




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