(42, all flavours) Integer Division - how?
|
12-20-2020, 03:22 AM
(This post was last modified: 01-08-2021 03:05 PM by Albert Chan.)
Post: #47
|
|||
|
|||
RE: (42, all flavours) Integer Division - how?
DIV code, using REM. It work with signed arguments, both a and b.
Code: 00 { 42-Byte Prgm } To handle signs of a, b, correction test: remainder(a,b)/b < 0 and q-1 or q idiv(a, b) = idiv(sign(b)*a, |b|) -- previously, code required positive divisor remainder(a,b)/b = remainder(a,|b|) / (sign(b)*|b|) = sign(b) * remainder(a, |b|) / |b| = remainder(sign(b)*a, |b|) / |b| The code gives the same answer, as if we manually made b positive. Note: remainder(a,b)/b = fractional part of quotient, if integer part is closest to a/b, not floored. Example: 4e33 39 + STO "u" 40 STO "v" XEQ "DIV" → X = 1e32, Y = -0.025 With Y < 0, this meant closest integer for u/v is X+1 (-u)/(-v) gives exactly the same result of u/v, as expected. u/v = X+1 - Y = 1e32 + 0.975 (-u)/v gives X = -1e32 - 1, Y = 0.025 With Y >= 0, closest integer for (-u)/v = X u/(-v) gives exactly the same result of (-u)/v, as expected. (-u)/v = X + Y = -1e32 - 0.975 Note: if u/v != floor(u/v), the code skipped REM, and return floor(u/v), u/v 100 16 XEQ "DIV" → X = 6, Y = 6.25 Update: removed the unneeded NOP after DSE |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)