Post Reply 
(42S) Quotient and Remainder
04-05-2023, 12:39 PM
Post: #2
RE: (42S) Quotient and Remainder
Hi, Werner

It had been 2 years. I provided 2 versions for floor-divide

#1: we assumed "normal" division was round-to-nearest, half-way-to-even.
The trick is to reverse engineer how it was done, and reverse the "damage".
(12-20-2020 03:22 AM)Albert Chan Wrote:  To handle signs of a, b, correction test: remainder(a,b)/b < 0 and q-1 or q

#2: more direct way, based from FMA(-q, b, a) = -q*b + a
Fixing q (for floor-divide) is similar: FMA(-q, b, a)/b < 0 and q-1 or q

The downside is that 42S does not have FMA (almost no calculator does ...)
Free42/Plus42 only had it recently (Free42 version ≥ 2.5.23, all Plus42 versions)

(01-08-2021 05:24 PM)Albert Chan Wrote:  FMA based DIV behave the same as my REM based DIV.

Click on the green arrow for floor-divide code.



Note that Free42/Plus42 (42S too?) MOD is really floor-mod. Q should be floor-quotient to match.
Example, Python use floor-divide/floor-mod: divmod(y,x) == (y//x, y%x)

>>> Y, X = 8*10**11+2, 4
>>> divmod(-Y, X)
(-200000000001L, 2L)
>>> divmod(Y, -X)
(-200000000001L, -2L)

OP code, it seems Q is truncated quotient, R is floor-mod.
Or, if you want truncated quotient, R should match that.
Either way, invariant Y = Q*X+R should hold.

(-Y) (X) "RQ" --> (-2e11) (+2)      ?
(Y) (-X) "RQ" --> (-2e11) (-2)       ?
Find all posts by this user
Quote this message in a reply
Post Reply 


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 - Werner - 04-05-2023, 03:55 PM



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