Post Reply 
(42, all flavours) Integer Division - how?
12-19-2020, 04:42 PM (This post was last modified: 12-19-2020 08:14 PM by Werner.)
Post: #43
RE: (42, all flavours) Integer Division - how?
I always use IP.
41/42 doesn't have native FLOOR or CEIL.

So, this would be the 42 version:
In: X Y
Out: Q R, X in LASTX
Code:
00 { 70-Byte Prgm } @     X       Y       Z       T
01▸LBL "RQ" @             X       Y
02 RCL ST Y @             Y       X       Y
03 RCL÷ ST Y @           Y/X      X       Y
04 ENTER
05 IP @                  [Q]      Q       X       Y
06 X=Y?
07 GTO 00
08 R↓
09 R↓ @                   X       Y      [Q]
10 MOD
11 X<>Y @         X      [Q]      R
12 RTN
13▸LBL 00 @               Q       Q       X       Y
14 CLX
15 + @                    Q       X       Y       Y
16 R↓ @                   X       Y       Y       Q
17 MOD @          X       R       Y       Q       Q
18 LASTX @                X       R       Y       Q
19 RCL- ST Y @    X      X-R      R       Y       Q
20 X>Y?
21 GTO 00
22 X=Y?
23 GTO 01
24 DSE ST T
25▸LBL 00
26 X<> ST T @     X       Q       R
27 RTN
28▸LBL 01 @       X       R       R       Y       Q
29 R↓
30 X<>Y @         X       Y       R       Q
31 1
32 RCL+ ST L @            C       Y       R       Q
33 MOD @          C      Y%C      R       Q       Q
34 LASTX
35 - @            C     Y%C-C     R       Q       Q
36 R^
37 STO+ ST Y
38 X<> ST L @             C       T       R       Q
39 MOD @          C      T%C      R       Q       Q
40 X≠Y?
41 DSE ST T
42▸LBL 00
43 CLX
44 1
45 STO- ST L
46 X<> ST T @     X       Q       R
47 END

A version for the 41 would be at the same time simpler and harder: the 41 doesn't do round-to even (rounds up the halfway case), but doesn't have recall arithmetic.

Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: (42, all flavours) Integer Division - how? - Werner - 12-19-2020 04:42 PM



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