Wednesday, March 21, 2012

[umvytoye] ceilDiv

ceilDiv x y = let { (d,m) = divMod x y } in if m>0 then (d+1) else d

This approximates the function composition (ceil . (/)) without going through floating point (assuming non-negative).

floorDiv is not necessary; plain div will do.

2 comments :

Twan van Laarhoven said...

Alternatively, ceilDiv x y = (x + y - 1) `div` y

Twan van Laarhoven said...

Or even nicer: ceilDiv x y = negate (negate x `div` y)