(42S) Quotient and Remainder
|
04-05-2023, 09:38 AM
(This post was last modified: 04-06-2023 11:54 AM by Werner.)
Post: #1
|
|||
|
|||
(42S) Quotient and Remainder
[Updated to use floor-divide. X and/or Y may be negative, and Y = Q*X+R holds when possible]
The 41C version was easy, but here we have to solve the halfway problem, eg: Y: 8e11+2 X: 4 must return Q: 2E11 R: 2 but Y: 8e11+6 X: 4 must return Q: 2E11+1 R: 2 In the first case, the division rounds down, in the second it rounds up. Thanks to Albert Chan for the halfway correcting algorithm. (which I adapted so I could keep on using IP instead of FLOOR) split Y = Q*X + R t := X/Y; Q := INT(t); R := MOD(Y,X); if t#Q then return(R,Q); t := X - R; if t<R then Q:= Q - 1; if t=R then Q:= Q - MOD(FLOOR(MOD(Y,X*10)/X),2); /* equals IP(MOD(MOD(Y,X*10)/X,2)) */ return(R,Q); Code: @ given X and Y, determine Quotient DIV(Y,X) and Remainder MOD(Y,X) will work for X and Y integers, abs(X)<1e12 and abs(Y)<=X*1e12 I confess I haven't tried it with fractional inputs, but that should also work. Counterexamples welcome, I think ;-) As with the 41 code, this routine will work when Q can be represented with 12 digits eg Y=1e40 and X=6 will not work. Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
(42S) Quotient and Remainder - Werner - 04-05-2023 09:38 AM
RE: (42S) Quotient and Remainder - Albert Chan - 04-05-2023, 12:39 PM
RE: (42S) Quotient and Remainder - Werner - 04-05-2023, 01:11 PM
RE: (42S) Quotient and Remainder - Albert Chan - 04-05-2023, 01:48 PM
RE: (42S) Quotient and Remainder - Werner - 04-05-2023, 03:55 PM
RE: (42S) Quotient and Remainder - Albert Chan - 04-06-2023, 12:03 AM
RE: (42S) Quotient and Remainder - Albert Chan - 04-06-2023, 02:55 PM
|
User(s) browsing this thread: 1 Guest(s)