Re: final message

new topic     » goto parent     » topic index » view thread      » older message » newer message
DerekParnell said...
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} 

Doing the abs() and then multiplying by signs offends my sense of efficiency. smile

Matt

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu