ekmett / rounded

MPFR bindings for Haskell
http://hackage.haskell.org/package/rounded
BSD 3-Clause "New" or "Revised" License
33 stars 13 forks source link

Rounding of non-floating-point values #15

Open dmcclean opened 10 years ago

dmcclean commented 10 years ago

It seems like all the concepts in Numeric.Rounded.Rounding apply at some non-floating-point types like Rational or binary or decimal fixed-point types.

It would be nice if there was a corresponding type class with instances for Rounded r p, Double, Rational, etc.

The tricky thing is representing the precision to round to. It isn't like the situation for the Rounded type where the task is to keep the result of a numerical operation within the type at which it is called, instead the task is to explicitly round to a lower precision. Perhaps data Precision' = Binary Int | Decimal Int? Or data Precision' = Precision' { base :: Int, power :: Int }? Or the type class could have separate methods for binary, decimal, and arbitrary base rounding which might be desirable if some types really can't sensibly support all options. Note that the digits here are counted with respect to 1, not with respect to whatever exponent (if any) is contained in the value itself.

class (RealFrac a) => RealFrac' a where
  roundTo :: RoundingMode -> Precision' -> a -> a
  round' :: RoundingMode -> a -> Integer
  -- some laws:
  -- round' TowardZero = truncate
  -- round' TowardInf = ceiling
  -- round' TowardNegInf = floor
  roundToRational :: RoundingMode -> Integer -> a -> Rational -- where second argument is desired denominator

RealFrac's round uses bankers rounding, which I don't think currently has an encoding in the RoundingMode type.