Post Reply 
(42, all flavours) Integer Division - how?
12-13-2020, 08:51 PM
Post: #19
RE: (42, all flavours) Integer Division - how?
(12-13-2020 07:19 PM)Werner Wrote:  
(12-13-2020 04:21 PM)Albert Chan Wrote:  Assuming q is correctly rounded (mode round-to-nearest), r = 0 or -1
Only for a and b positive?
For a negative, it doesn’t work, eg
(12 digits calc)
a=-4e11-6
b=4
DIV returns -1e11-2

I defined IDIV matching MOD behavior: a = b * IDIV(a,b) + MOD(a,b)

Free42, binary and decimal, uses floor-mod: (a MOD b) has sign of b
To match it, IDIV(a,b) = floor(a/b)

So, above DIV is correct: floor((-4e11 - 6)/4) = floor(-1e11 - 1.5) = -1e11 - 2

Python also define it this way, see Why Python Integer Division Floors

>>> a, b = -4*10**11-6, 4
>>> q, r = a//b, a%b
>>> print q, r, q*b+r
-100000000002      2      -400000000006
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: (42, all flavours) Integer Division - how? - Albert Chan - 12-13-2020 08:51 PM



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