Skip to content

Rounding of non-floating-point values #15

Description

@dmcclean

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions