Re: final message
- Posted by DerekParnell (admin) May 25, 2012
- 1445 views
mattlewis said...
I can see the value in different rounding methods, but the sign handling here seems wrong, as I would expect:
intdiv(-8, 3, 0) --> -2
Ok, I'm not wedded to abs() in this case. So how about ...
public function intdiv(object a, object b, integer behave = 1) object t t = abs(a) / abs(b) switch behave do case -1 then t = floor(t) -- Move towards zero. case 0 then t = round(t) -- Move either way to round case 1 then t = ceil(t) -- Move away from zero case else fail("Unknown behaviour requested") end switch return t * sign(a) * sign(b) -- Adjust sign for each element in arguments. end function ? intdiv({-8,8}, -3, 1) --> {3,-3} ? intdiv({-8,8}, -3, 0) --> {3,-3} ? intdiv({-8,8}, -3,-1) --> {2,-2}