Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rounding of non-floating-point values #15

Open
dmcclean opened this issue Apr 17, 2014 · 0 comments
Open

Rounding of non-floating-point values #15

dmcclean opened this issue Apr 17, 2014 · 0 comments

Comments

@dmcclean
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant