Post Reply 
(41C) Quotient and Remainder
04-05-2023, 09:12 AM (This post was last modified: 04-06-2023 05:40 AM by Werner.)
Post: #1
(41C) Quotient and Remainder
[Update: for now, only for positive X and Y]
You would think this is a no-brainer, but, as has been touched upon a couple of times, you can't just calculate the Quotient with INT(Y/X), as that will fail when rounding occurs in the division.
The following routine gets it right, and even saves X in LASTX:

Code:
@ given X and Y, determine Quotient=DIV(Y,X)  and Remainder=MOD(Y,X)
@               L       X       Y       Z       T
@ In:                   X       Y
@ Out:          X       R       Q
01 LBL"RQ"  @           X       Y
02 RCL Y
03 RCL Y
04 /                   Y/X      X       Y
05 ENTER
06 INT @                Q      Q..      X       Y
07 X=Y?
08 GTO 00
09 RDN
10 RDN @                X       Y       Q
11 MOD @    X    R    Q
12 RTN
13 LBL 00
14 RDN
15 RDN @                X       Y       Q       Q
16 STO Z
17 MOD @        X       R       X       Q       Q
18 ST- Y @      X       R      X-R      Q       Q
19 X<>Y
20 X<=Y?
21 DSE Z @ Q=1 rounded up is not possible
22 RDN @        X       R       Q
23 END

Try for instance:
Y: 4 000 000 001, 4 000 000 002, 4 000 000 003
X: 4
and get the correct
Q: 1E9
R: 1,2,3

or

Y: 2.000000003E19
X: 4E9
to get
Q: 5 000 000 007
R: 2 000 000 000

Getting it right on machines that do round-to-even is somewhat harder. Stay tuned for my 42S solution, in another section of this forum.

Cheers, Werner
as an afterthought: this RQ split will only work when it makes sense, eg not for Y=1e40 and X=6 because then Q carries too many digits

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
(41C) Quotient and Remainder - Werner - 04-05-2023 09:12 AM
RE: (41C) Quotient and Remainder - Werner - 04-05-2023, 01:08 PM
RE: (41C) Quotient and Remainder - Werner - 04-10-2023, 07:25 AM
RE: (41C) Quotient and Remainder - Werner - 04-18-2023, 02:57 PM



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