Skip to content

Commit

Permalink
Add type RoundingDirection.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed Jan 14, 2021
1 parent 79e96e4 commit b30cfb3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/numeric/cardano-numeric.cabal
Expand Up @@ -33,6 +33,7 @@ library
src
exposed-modules:
Cardano.Numeric.PositiveNatural
Cardano.Numeric.Rounding

test-suite unit
default-language:
Expand Down
31 changes: 31 additions & 0 deletions lib/numeric/src/Cardano/Numeric/Rounding.hs
@@ -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

0 comments on commit b30cfb3

Please sign in to comment.