04-05-2023, 09:12 AM

[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:

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

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