ekmett / ekmett/rounded

Rounding of non-floating-point values

Open
#15 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
35
Forks
13
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.