[Haskell-cafe] Division: Is there a way to simultaneously find the quotient and remainder?
daniel.is.fischer at googlemail.com
Sat May 14 18:00:56 CEST 2011
On Saturday 14 May 2011 15:55:15, Henning Thielemann wrote:
> Chris Smith schrieb:
> > Sure... see quotRem in the prelude.
No, don't, just know when you want which.
quot and rem are what you get from the hardware (perhaps not all, but
most), thus they are faster. If speed is important and the semantics
coincide (e.g. when all operands are positive), use quot and rem.
If the semantics of quot or rem are better for your goal than those of div
or mod (example from GHC.Float.floatToDigits:
-- Using quot instead of div is a little faster and requires
-- fewer fixup steps for negative lx.
let lx = p - 1 + e0
k1 = (lx * 8651) `quot` 28738
), use quot or rem.
div and mod have the better semantics for most use-cases, so they should be
what one normally uses, but there are cases where you want quot and rem.
For those cases, knowing them causes happiness.
More information about the Haskell-Cafe