Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- type Decimal64 s = Decimal RoundHalfUp s Int64
- data RoundHalfUp
- data RoundFloor
- data Truncate
- newtype Decimal r (s :: Nat) p = Decimal p
- class Round r where
- wrapDecimal :: Integral p => p -> Decimal r s p
- unwrapDecimal :: Decimal r s p -> p
- splitDecimal :: (Integral p, KnownNat s) => Decimal r s p -> (p, p)
- getScale :: forall r s p. KnownNat s => Decimal r s p -> Int
- fromNum :: forall r s p. (Num p, KnownNat s) => p -> Decimal r s p
- parseDecimalBounded :: forall r s p. (KnownNat s, Bounded p, Integral p) => Bool -> String -> Either String (Decimal r s p)
- plusDecimal :: (MonadThrow m, Eq p, Ord p, Num p, Bounded p) => Decimal r s p -> Decimal r s p -> m (Decimal r s p)
- minusDecimal :: (MonadThrow m, Eq p, Ord p, Num p, Bounded p) => Decimal r s p -> Decimal r s p -> m (Decimal r s p)
- timesDecimal :: Decimal r s1 Integer -> Decimal r s2 Integer -> Decimal r (s1 + s2) Integer
- signumDecimal :: (Num p, KnownNat s) => Decimal r s p -> Decimal r s p
- timesDecimalBounded :: (MonadThrow m, Integral p, Bounded p) => Decimal r s1 p -> Decimal r s2 p -> m (Decimal r (s1 + s2) p)
- timesDecimalRounded :: (MonadThrow m, KnownNat s, Round r, Integral p, Bounded p) => Decimal r s p -> Decimal r s p -> m (Decimal r s p)
- divideDecimal :: (MonadThrow m, Fractional (m (Decimal r s p)), Integral p, Integral p) => Decimal r s p -> Decimal r s p -> m (Decimal r s p)
- quotRemBounded :: (MonadThrow m, Integral a, Bounded a) => a -> a -> m (a, a)
- quotRemDecimalBounded :: forall m r s p. (MonadThrow m, Integral p, Bounded p) => Decimal r s p -> Integer -> m (Decimal r s p, Decimal r s p)
- fromIntegerDecimalBounded :: forall m r s p. (MonadThrow m, Integral p, Bounded p) => Decimal r s Integer -> m (Decimal r s p)
- fromRationalDecimalRounded :: forall m r s p. (MonadThrow m, KnownNat s, Round r, Integral p) => Rational -> m (Decimal r s p)
- liftDecimal :: (p1 -> p2) -> Decimal r s p1 -> Decimal r s p2
- liftDecimal2 :: (p1 -> p2 -> p3) -> Decimal r s p1 -> Decimal r s p2 -> Decimal r s p3
- bindM2Decimal :: Monad m => (p1 -> p2 -> m p) -> m (Decimal r1 s1 p1) -> m (Decimal r2 s2 p2) -> m (Decimal r s p)
- bindM2 :: Monad m => (a -> b -> m c) -> m a -> m b -> m c
- plusBounded :: (MonadThrow m, Eq a, Ord a, Num a, Bounded a) => a -> a -> m a
- minusBounded :: (MonadThrow m, Eq a, Ord a, Num a, Bounded a) => a -> a -> m a
- timesBounded :: (MonadThrow m, Integral a, Bounded a) => a -> a -> m a
- fromIntegerBounded :: forall m a. (MonadThrow m, Integral a, Bounded a) => Integer -> m a
- fromIntegerScaleBounded :: forall m a s. (MonadThrow m, Integral a, Bounded a, KnownNat s) => Proxy s -> Integer -> m a
- divBounded :: (MonadThrow m, Integral a, Bounded a) => a -> a -> m a
- quotBounded :: (MonadThrow m, Integral a, Bounded a) => a -> a -> m a
- decimalList :: Integral p => [p] -> [Decimal r s p]
- sumDecimal :: (MonadThrow m, Foldable f, Eq p, Ord p, Num p, Bounded p) => f (Decimal r s p) -> m (Decimal r s p)
- productDecimal :: (MonadThrow m, Foldable f, KnownNat s, Round r, Integral p, Bounded p) => f (Decimal r s p) -> m (Decimal r s p)
- toScientific :: (Integral p, KnownNat s) => Decimal r s p -> Scientific
- fromScientific :: forall m r s. (MonadThrow m, KnownNat s) => Scientific -> m (Decimal r s Integer)
- fromScientificBounded :: forall m r s p. (MonadThrow m, Integral p, Bounded p, KnownNat s) => Scientific -> m (Decimal r s p)
Documentation
type Decimal64 s = Decimal RoundHalfUp s Int64 Source #
Most common Decimal type backed by Int64
and standard rounding
data RoundHalfUp Source #
Instances
Round RoundHalfUp Source # | |
Defined in Numeric.Decimal roundDecimal :: (Integral p, KnownNat k) => Decimal RoundHalfUp (n + k) p -> Decimal RoundHalfUp n p Source # |
data RoundFloor Source #
Instances
Round RoundFloor Source # | |
Defined in Numeric.Decimal roundDecimal :: (Integral p, KnownNat k) => Decimal RoundFloor (n + k) p -> Decimal RoundFloor n p Source # |
newtype Decimal r (s :: Nat) p Source #
Decimal number with custom precision (p
) and type level scaling (s
) parameter (i.e. number
of digits after the decimal point). As well as the rounding (r
) strategy to use
Decimal p |
Instances
Instances
Round Truncate Source # | |
Round RoundFloor Source # | |
Defined in Numeric.Decimal roundDecimal :: (Integral p, KnownNat k) => Decimal RoundFloor (n + k) p -> Decimal RoundFloor n p Source # | |
Round RoundHalfUp Source # | |
Defined in Numeric.Decimal roundDecimal :: (Integral p, KnownNat k) => Decimal RoundHalfUp (n + k) p -> Decimal RoundHalfUp n p Source # |
wrapDecimal :: Integral p => p -> Decimal r s p Source #
unwrapDecimal :: Decimal r s p -> p Source #
Get out the underlying representation for the decimal number. No scaling will be done.
splitDecimal :: (Integral p, KnownNat s) => Decimal r s p -> (p, p) Source #
Split the number at the decimal point, i.e. whole number and the fraction
getScale :: forall r s p. KnownNat s => Decimal r s p -> Int Source #
Get the scale of the Decimal
. Argument is not evaluated.
fromNum :: forall r s p. (Num p, KnownNat s) => p -> Decimal r s p Source #
This operation is susceptible to overflows, since it performs the scaling.
parseDecimalBounded :: forall r s p. (KnownNat s, Bounded p, Integral p) => Bool -> String -> Either String (Decimal r s p) Source #
Algebra
plusDecimal :: (MonadThrow m, Eq p, Ord p, Num p, Bounded p) => Decimal r s p -> Decimal r s p -> m (Decimal r s p) Source #
Add two decimal numbers.
minusDecimal :: (MonadThrow m, Eq p, Ord p, Num p, Bounded p) => Decimal r s p -> Decimal r s p -> m (Decimal r s p) Source #
Subtract two decimal numbers.
timesDecimal :: Decimal r s1 Integer -> Decimal r s2 Integer -> Decimal r (s1 + s2) Integer Source #
Multiply two bounded decimal numbers, adjusting their scale at the type level as well.
signumDecimal :: (Num p, KnownNat s) => Decimal r s p -> Decimal r s p Source #
Compute signum of a decimal, always one of 1, 0 or -1
timesDecimalBounded :: (MonadThrow m, Integral p, Bounded p) => Decimal r s1 p -> Decimal r s2 p -> m (Decimal r (s1 + s2) p) Source #
Multiply two bounded decimal numbers, adjusting their scale at the type level as well.
timesDecimalRounded :: (MonadThrow m, KnownNat s, Round r, Integral p, Bounded p) => Decimal r s p -> Decimal r s p -> m (Decimal r s p) Source #
Multiply two decimal numbers, while rounding the result according to the rounding strategy.
divideDecimal :: (MonadThrow m, Fractional (m (Decimal r s p)), Integral p, Integral p) => Decimal r s p -> Decimal r s p -> m (Decimal r s p) Source #
quotRemBounded :: (MonadThrow m, Integral a, Bounded a) => a -> a -> m (a, a) Source #
Divide two decimal numbers while checking for Overflow
and DivideByZero
quotRemDecimalBounded :: forall m r s p. (MonadThrow m, Integral p, Bounded p) => Decimal r s p -> Integer -> m (Decimal r s p, Decimal r s p) Source #
fromIntegerDecimalBounded :: forall m r s p. (MonadThrow m, Integral p, Bounded p) => Decimal r s Integer -> m (Decimal r s p) Source #
fromRationalDecimalRounded :: forall m r s p. (MonadThrow m, KnownNat s, Round r, Integral p) => Rational -> m (Decimal r s p) Source #
liftDecimal :: (p1 -> p2) -> Decimal r s p1 -> Decimal r s p2 Source #
bindM2Decimal :: Monad m => (p1 -> p2 -> m p) -> m (Decimal r1 s1 p1) -> m (Decimal r2 s2 p2) -> m (Decimal r s p) Source #
Bounded
plusBounded :: (MonadThrow m, Eq a, Ord a, Num a, Bounded a) => a -> a -> m a Source #
minusBounded :: (MonadThrow m, Eq a, Ord a, Num a, Bounded a) => a -> a -> m a Source #
timesBounded :: (MonadThrow m, Integral a, Bounded a) => a -> a -> m a Source #
Multiply two decimal numbers while checking for Overflow
fromIntegerBounded :: forall m a. (MonadThrow m, Integral a, Bounded a) => Integer -> m a Source #
fromIntegerScaleBounded :: forall m a s. (MonadThrow m, Integral a, Bounded a, KnownNat s) => Proxy s -> Integer -> m a Source #
divBounded :: (MonadThrow m, Integral a, Bounded a) => a -> a -> m a Source #
Divide two decimal numbers while checking for Overflow
and DivideByZero
quotBounded :: (MonadThrow m, Integral a, Bounded a) => a -> a -> m a Source #
Divide two decimal numbers while checking for Overflow
and DivideByZero
Operations
decimalList :: Integral p => [p] -> [Decimal r s p] Source #
O(1) - Conversion of a list.
Note: It doesn't do any scaling, eg:
>>>
decimalList [1,20,300] :: [Decimal RoundHalfUp 2 Int]
[0.01,0.20,3.00]
If scaling is what you need use fromIntegral
instead:
>>>
mapM fromIntegral ([1,20,300] :: [Int]) :: Either SomeException [Decimal RoundHalfUp 2 Int]
Right [1.00,20.00,300.00]
sumDecimal :: (MonadThrow m, Foldable f, Eq p, Ord p, Num p, Bounded p) => f (Decimal r s p) -> m (Decimal r s p) Source #
Sum a list of decimal numbers
productDecimal :: (MonadThrow m, Foldable f, KnownNat s, Round r, Integral p, Bounded p) => f (Decimal r s p) -> m (Decimal r s p) Source #
Multiply all decimal numbers in the list while doing rounding.
Conversion
toScientific :: (Integral p, KnownNat s) => Decimal r s p -> Scientific Source #
Convert Decimal to Scientific
fromScientific :: forall m r s. (MonadThrow m, KnownNat s) => Scientific -> m (Decimal r s Integer) Source #
Convert Scientific to Decimal without loss of precision. Will return Left
Underflow
if
Scientific
has too many decimal places, more than Decimal
scaling is capable to handle.
fromScientificBounded :: forall m r s p. (MonadThrow m, Integral p, Bounded p, KnownNat s) => Scientific -> m (Decimal r s p) Source #
Convert from Scientific to Decimal while checking for Overflow/Underflow