Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79e96e4
commit b30cfb3
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
|
||
-- | Provides types and functions relating to rounding of fractional numbers. | ||
-- | ||
module Cardano.Numeric.Rounding | ||
( RoundingDirection (..) | ||
, round | ||
) where | ||
|
||
import Prelude hiding | ||
( round ) | ||
|
||
-- | Indicates a rounding direction to be used when converting from a | ||
-- fractional value to an integral value. | ||
-- | ||
-- See 'round'. | ||
-- | ||
data RoundingDirection | ||
= RoundUp | ||
-- ^ Round up to the nearest integral value. | ||
| RoundDown | ||
-- ^ Round down to the nearest integral value. | ||
deriving (Eq, Show) | ||
|
||
-- | Use the given rounding direction to round the given fractional value, | ||
-- producing an integral result. | ||
-- | ||
round :: (RealFrac a, Integral b) => RoundingDirection -> a -> b | ||
round = \case | ||
RoundUp -> ceiling | ||
RoundDown -> floor |