{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TemplateHaskellQuotes #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Fixed
-- Copyright   :  (c) Ashley Yakeley 2005, 2006, 2009
-- License     :  BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer  :  Ashley Yakeley <ashley@semantic.org>
-- Stability   :  stable
-- Portability :  portable
--
-- This module defines a 'Fixed' type for working with fixed-point arithmetic.
-- Fixed-point arithmetic represents fractional numbers with a fixed number of
-- digits for their fractional part. This is different to the behaviour of the floating-point
-- number types 'Float' and 'Double', because the number of digits of the
-- fractional part of 'Float' and 'Double' numbers depends on the size of the number.
-- Fixed point arithmetic is frequently used in financial mathematics, where they
-- are used for representing decimal currencies.
--
-- The type 'Fixed' is used for fixed-point fractional numbers, which are internally
-- represented as an 'Integer'. The type 'Fixed' takes one parameter, which should implement
-- the typeclass 'HasResolution', to specify the number of digits of the fractional part.
-- This module provides instances of the `HasResolution` typeclass for arbitrary typelevel
-- natural numbers, and for some canonical important fixed-point representations.
--
-- This module also contains generalisations of 'div', 'mod', and 'divMod' to
-- work with any 'Real' instance.
--
-- Automatic conversion between different 'Fixed' can be performed through
-- 'realToFrac', bear in mind that converting to a fixed with a smaller
-- resolution will truncate the number, losing information.
--
-- >>> realToFrac (0.123456 :: Pico) :: Milli
-- 0.123
--
-----------------------------------------------------------------------------

module Data.Fixed
(   -- * The Fixed Type
    Fixed(..), HasResolution(..),
    showFixed,
    -- * Resolution \/ Scaling Factors
    -- | The resolution or scaling factor determines the number of digits in the fractional part.
    --
    -- +------------+----------------------+--------------------------+--------------------------+
    -- | Resolution | Scaling Factor       | Synonym for \"Fixed EX\" | show (12345 :: Fixed EX) |
    -- +============+======================+==========================+==========================+
    -- | E0         | 1\/1                 | Uni                      | 12345.0                  |
    -- +------------+----------------------+--------------------------+--------------------------+
    -- | E1         | 1\/10                | Deci                     | 1234.5                   |
    -- +------------+----------------------+--------------------------+--------------------------+
    -- | E2         | 1\/100               | Centi                    | 123.45                   |
    -- +------------+----------------------+--------------------------+--------------------------+
    -- | E3         | 1\/1 000             | Milli                    | 12.345                   |
    -- +------------+----------------------+--------------------------+--------------------------+
    -- | E6         | 1\/1 000 000         | Micro                    | 0.012345                 |
    -- +------------+----------------------+--------------------------+--------------------------+
    -- | E9         | 1\/1 000 000 000     | Nano                     | 0.000012345              |
    -- +------------+----------------------+--------------------------+--------------------------+
    -- | E12        | 1\/1 000 000 000 000 | Pico                     | 0.000000012345           |
    -- +------------+----------------------+--------------------------+--------------------------+
    --

    -- ** 1\/1
    E0,Uni,
    -- ** 1\/10
    E1,Deci,
    -- ** 1\/100
    E2,Centi,
    -- ** 1\/1 000
    E3,Milli,
    -- ** 1\/1 000 000
    E6,Micro,
    -- ** 1\/1 000 000 000
    E9,Nano,
    -- ** 1\/1 000 000 000 000
    E12,Pico,
    -- * Generalized Functions on Real's
    div',
    mod',
    divMod'
) where

import GHC.Internal.Data.Data
import GHC.Internal.TypeLits (KnownNat, natVal)
import GHC.Internal.Read
import GHC.Internal.Text.ParserCombinators.ReadPrec
import GHC.Internal.Text.Read.Lex
import qualified GHC.Internal.TH.Syntax as TH
import qualified GHC.Internal.TH.Lift as TH
import Data.Typeable
import Prelude

-- $setup
-- >>> import Prelude

default () -- avoid any defaulting shenanigans

-- | Generalisation of 'div' to any instance of 'Real'
div' :: (Real a,Integral b) => a -> a -> b
div' :: forall a b. (Real a, Integral b) => a -> a -> b
div' a
n a
d = Rational -> b
forall b. Integral b => Rational -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor ((a -> Rational
forall a. Real a => a -> Rational
toRational a
n) Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ (a -> Rational
forall a. Real a => a -> Rational
toRational a
d))

-- | Generalisation of 'divMod' to any instance of 'Real'
divMod' :: (Real a,Integral b) => a -> a -> (b,a)
divMod' :: forall a b. (Real a, Integral b) => a -> a -> (b, a)
divMod' a
n a
d = (b
f,a
n a -> a -> a
forall a. Num a => a -> a -> a
- (b -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral b
f) a -> a -> a
forall a. Num a => a -> a -> a
* a
d) where
    f :: b
f = a -> a -> b
forall a b. (Real a, Integral b) => a -> a -> b
div' a
n a
d

-- | Generalisation of 'mod' to any instance of 'Real'
mod' :: (Real a) => a -> a -> a
mod' :: forall a. Real a => a -> a -> a
mod' a
n a
d = a
n a -> a -> a
forall a. Num a => a -> a -> a
- (Integer -> a
forall a. Num a => Integer -> a
fromInteger Integer
f) a -> a -> a
forall a. Num a => a -> a -> a
* a
d where
    f :: Integer
f = a -> a -> Integer
forall a b. (Real a, Integral b) => a -> a -> b
div' a
n a
d

-- | The type of fixed-point fractional numbers.
--   The type parameter specifies the number of digits of the fractional part and should be an instance of the 'HasResolution' typeclass.
--
-- === __Examples__
--
-- @
--  MkFixed 12345 :: Fixed E3
-- @
newtype Fixed (a :: k) = MkFixed Integer
        deriving ( Fixed a -> Fixed a -> Bool
(Fixed a -> Fixed a -> Bool)
-> (Fixed a -> Fixed a -> Bool) -> Eq (Fixed a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k (a :: k). Fixed a -> Fixed a -> Bool
$c== :: forall k (a :: k). Fixed a -> Fixed a -> Bool
== :: Fixed a -> Fixed a -> Bool
$c/= :: forall k (a :: k). Fixed a -> Fixed a -> Bool
/= :: Fixed a -> Fixed a -> Bool
Eq  -- ^ @since 2.01
                 , Eq (Fixed a)
Eq (Fixed a) =>
(Fixed a -> Fixed a -> Ordering)
-> (Fixed a -> Fixed a -> Bool)
-> (Fixed a -> Fixed a -> Bool)
-> (Fixed a -> Fixed a -> Bool)
-> (Fixed a -> Fixed a -> Bool)
-> (Fixed a -> Fixed a -> Fixed a)
-> (Fixed a -> Fixed a -> Fixed a)
-> Ord (Fixed a)
Fixed a -> Fixed a -> Bool
Fixed a -> Fixed a -> Ordering
Fixed a -> Fixed a -> Fixed a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall k (a :: k). Eq (Fixed a)
forall k (a :: k). Fixed a -> Fixed a -> Bool
forall k (a :: k). Fixed a -> Fixed a -> Ordering
forall k (a :: k). Fixed a -> Fixed a -> Fixed a
$ccompare :: forall k (a :: k). Fixed a -> Fixed a -> Ordering
compare :: Fixed a -> Fixed a -> Ordering
$c< :: forall k (a :: k). Fixed a -> Fixed a -> Bool
< :: Fixed a -> Fixed a -> Bool
$c<= :: forall k (a :: k). Fixed a -> Fixed a -> Bool
<= :: Fixed a -> Fixed a -> Bool
$c> :: forall k (a :: k). Fixed a -> Fixed a -> Bool
> :: Fixed a -> Fixed a -> Bool
$c>= :: forall k (a :: k). Fixed a -> Fixed a -> Bool
>= :: Fixed a -> Fixed a -> Bool
$cmax :: forall k (a :: k). Fixed a -> Fixed a -> Fixed a
max :: Fixed a -> Fixed a -> Fixed a
$cmin :: forall k (a :: k). Fixed a -> Fixed a -> Fixed a
min :: Fixed a -> Fixed a -> Fixed a
Ord -- ^ @since 2.01
                 )

-- We do this because the automatically derived Data instance requires (Data a) context.
-- Our manual instance has the more general (Typeable a) context.
tyFixed :: DataType
tyFixed :: DataType
tyFixed = String -> [Constr] -> DataType
mkDataType String
"Data.Fixed.Fixed" [Constr
conMkFixed]

conMkFixed :: Constr
conMkFixed :: Constr
conMkFixed = DataType -> String -> [String] -> Fixity -> Constr
mkConstr DataType
tyFixed String
"MkFixed" [] Fixity
Prefix

-- | @since 4.1.0.0
instance (Typeable k,Typeable a) => Data (Fixed (a :: k)) where
    gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Fixed a -> c (Fixed a)
gfoldl forall d b. Data d => c (d -> b) -> d -> c b
k forall g. g -> c g
z (MkFixed Integer
a) = c (Integer -> Fixed a) -> Integer -> c (Fixed a)
forall d b. Data d => c (d -> b) -> d -> c b
k ((Integer -> Fixed a) -> c (Integer -> Fixed a)
forall g. g -> c g
z Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed) Integer
a
    gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Fixed a)
gunfold forall b r. Data b => c (b -> r) -> c r
k forall r. r -> c r
z Constr
_ = c (Integer -> Fixed a) -> c (Fixed a)
forall b r. Data b => c (b -> r) -> c r
k ((Integer -> Fixed a) -> c (Integer -> Fixed a)
forall r. r -> c r
z Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed)
    dataTypeOf :: Fixed a -> DataType
dataTypeOf Fixed a
_ = DataType
tyFixed
    toConstr :: Fixed a -> Constr
toConstr Fixed a
_ = Constr
conMkFixed

-- |
-- @since template-haskell-2.19.0.0
-- @since base-4.21.0.0
instance TH.Lift (Fixed a) where
  liftTyped :: forall (m :: * -> *). Quote m => Fixed a -> Code m (Fixed a)
liftTyped Fixed a
x = m Exp -> Code m (Fixed a)
forall a (m :: * -> *). Quote m => m Exp -> Code m a
TH.unsafeCodeCoerce (Fixed a -> m Exp
forall t (m :: * -> *). (Lift t, Quote m) => t -> m Exp
forall (m :: * -> *). Quote m => Fixed a -> m Exp
TH.lift Fixed a
x)
  lift :: forall (m :: * -> *). Quote m => Fixed a -> m Exp
lift (MkFixed Integer
x) = [| MkFixed x |]

-- | Types which can be used as a resolution argument to the 'Fixed' type constructor must implement the 'HasResolution'  typeclass.
class HasResolution (a :: k) where
    -- | Provide the resolution for a fixed-point fractional number.
    resolution :: p a -> Integer

-- | For example, @Fixed 1000@ will give you a 'Fixed' with a resolution of 1000.
instance KnownNat n => HasResolution n where
    resolution :: forall (p :: Nat -> *). p n -> Integer
resolution p n
_ = Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall {k} (t :: k). Proxy t
Proxy :: Proxy n)

withType :: (Proxy a -> f a) -> f a
withType :: forall {k} (a :: k) (f :: k -> *). (Proxy a -> f a) -> f a
withType Proxy a -> f a
foo = Proxy a -> f a
foo Proxy a
forall {k} (t :: k). Proxy t
Proxy

withResolution :: (HasResolution a) => (Integer -> f a) -> f a
withResolution :: forall {k} (a :: k) (f :: k -> *).
HasResolution a =>
(Integer -> f a) -> f a
withResolution Integer -> f a
foo = (Proxy a -> f a) -> f a
forall {k} (a :: k) (f :: k -> *). (Proxy a -> f a) -> f a
withType (Integer -> f a
foo (Integer -> f a) -> (Proxy a -> Integer) -> Proxy a -> f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy a -> Integer
forall k (a :: k) (p :: k -> *). HasResolution a => p a -> Integer
forall (p :: k -> *). p a -> Integer
resolution)

-- | @since 2.01
--
-- Recall that, for numeric types, 'succ' and 'pred' typically add and subtract
-- @1@, respectively. This is not true in the case of 'Fixed', whose successor
-- and predecessor functions intuitively return the "next" and "previous" values
-- in the enumeration. The results of these functions thus depend on the
-- resolution of the 'Fixed' value. For example, when enumerating values of
-- resolution @10^-3@ of @type Milli = Fixed E3@,
--
-- >>> succ (0.000 :: Milli)
-- 0.001
--
-- and likewise
--
-- >>> pred (0.000 :: Milli)
-- -0.001
--
-- In other words, 'succ' and 'pred' increment and decrement a fixed-precision
-- value by the least amount such that the value's resolution is unchanged.
-- For example, @10^-12@ is the smallest (positive) amount that can be added to
-- a value of @type Pico = Fixed E12@ without changing its resolution, and so
--
-- >>> succ (0.000000000000 :: Pico)
-- 0.000000000001
--
-- and similarly
--
-- >>> pred (0.000000000000 :: Pico)
-- -0.000000000001
--
--
-- This is worth bearing in mind when defining 'Fixed' arithmetic sequences. In
-- particular, you may be forgiven for thinking the sequence
--
-- @
--   [1..10] :: [Pico]
-- @
--
--
-- evaluates to @[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] :: [Pico]@.
--
-- However, this is not true. On the contrary, similarly to the above
-- implementations of 'succ' and 'pred', @enumFromTo :: Pico -> Pico -> [Pico]@
-- has a "step size" of @10^-12@. Hence, the list @[1..10] :: [Pico]@ has
-- the form
--
-- @
--   [1.000000000000, 1.00000000001, 1.00000000002, ..., 10.000000000000]
-- @
--
--
-- and contains @9 * 10^12 + 1@ values.
instance Enum (Fixed a) where
    succ :: Fixed a -> Fixed a
succ (MkFixed Integer
a) = Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer -> Integer
forall a. Enum a => a -> a
succ Integer
a)
    pred :: Fixed a -> Fixed a
pred (MkFixed Integer
a) = Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer -> Integer
forall a. Enum a => a -> a
pred Integer
a)
    toEnum :: Int -> Fixed a
toEnum = Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer -> Fixed a) -> (Int -> Integer) -> Int -> Fixed a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a. Enum a => Int -> a
toEnum
    fromEnum :: Fixed a -> Int
fromEnum (MkFixed Integer
a) = Integer -> Int
forall a. Enum a => a -> Int
fromEnum Integer
a
    enumFrom :: Fixed a -> [Fixed a]
enumFrom (MkFixed Integer
a) = (Integer -> Fixed a) -> [Integer] -> [Fixed a]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer -> [Integer]
forall a. Enum a => a -> [a]
enumFrom Integer
a)
    enumFromThen :: Fixed a -> Fixed a -> [Fixed a]
enumFromThen (MkFixed Integer
a) (MkFixed Integer
b) = (Integer -> Fixed a) -> [Integer] -> [Fixed a]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer -> Integer -> [Integer]
forall a. Enum a => a -> a -> [a]
enumFromThen Integer
a Integer
b)
    enumFromTo :: Fixed a -> Fixed a -> [Fixed a]
enumFromTo (MkFixed Integer
a) (MkFixed Integer
b) = (Integer -> Fixed a) -> [Integer] -> [Fixed a]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer -> Integer -> [Integer]
forall a. Enum a => a -> a -> [a]
enumFromTo Integer
a Integer
b)
    enumFromThenTo :: Fixed a -> Fixed a -> Fixed a -> [Fixed a]
enumFromThenTo (MkFixed Integer
a) (MkFixed Integer
b) (MkFixed Integer
c) = (Integer -> Fixed a) -> [Integer] -> [Fixed a]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer -> Integer -> Integer -> [Integer]
forall a. Enum a => a -> a -> a -> [a]
enumFromThenTo Integer
a Integer
b Integer
c)

-- | @since 2.01
--
-- Multiplication is not associative or distributive:
--
-- >>> (0.2 * 0.6 :: Deci) * 0.9 == 0.2 * (0.6 * 0.9)
-- False
--
-- >>> (0.1 + 0.1 :: Deci) * 0.5 == 0.1 * 0.5 + 0.1 * 0.5
-- False
instance (HasResolution a) => Num (Fixed a) where
    (MkFixed Integer
a) + :: Fixed a -> Fixed a -> Fixed a
+ (MkFixed Integer
b) = Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer
a Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
b)
    (MkFixed Integer
a) - :: Fixed a -> Fixed a -> Fixed a
- (MkFixed Integer
b) = Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer
a Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
b)
    fa :: Fixed a
fa@(MkFixed Integer
a) * :: Fixed a -> Fixed a -> Fixed a
* (MkFixed Integer
b) = Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
div (Integer
a Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
b) (Fixed a -> Integer
forall k (a :: k) (p :: k -> *). HasResolution a => p a -> Integer
forall (p :: k -> *). p a -> Integer
resolution Fixed a
fa))
    negate :: Fixed a -> Fixed a
negate (MkFixed Integer
a) = Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer -> Integer
forall a. Num a => a -> a
negate Integer
a)
    abs :: Fixed a -> Fixed a
abs (MkFixed Integer
a) = Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer -> Integer
forall a. Num a => a -> a
abs Integer
a)
    signum :: Fixed a -> Fixed a
signum (MkFixed Integer
a) = Integer -> Fixed a
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer
forall a. Num a => a -> a
signum Integer
a)
    fromInteger :: Integer -> Fixed a
fromInteger Integer
i = (Integer -> Fixed a) -> Fixed a
forall {k} (a :: k) (f :: k -> *).
HasResolution a =>
(Integer -> f a) -> f a
withResolution (\Integer
res -> Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
res))

-- | @since 2.01
instance (HasResolution a) => Real (Fixed a) where
    toRational :: Fixed a -> Rational
toRational fa :: Fixed a
fa@(MkFixed Integer
a) = (Integer -> Rational
forall a. Real a => a -> Rational
toRational Integer
a) Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ (Integer -> Rational
forall a. Real a => a -> Rational
toRational (Fixed a -> Integer
forall k (a :: k) (p :: k -> *). HasResolution a => p a -> Integer
forall (p :: k -> *). p a -> Integer
resolution Fixed a
fa))

-- | @since 2.01
instance (HasResolution a) => Fractional (Fixed a) where
    fa :: Fixed a
fa@(MkFixed Integer
a) / :: Fixed a -> Fixed a -> Fixed a
/ (MkFixed Integer
b) = Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
div (Integer
a Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* (Fixed a -> Integer
forall k (a :: k) (p :: k -> *). HasResolution a => p a -> Integer
forall (p :: k -> *). p a -> Integer
resolution Fixed a
fa)) Integer
b)
    recip :: Fixed a -> Fixed a
recip fa :: Fixed a
fa@(MkFixed Integer
a) = Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
div (Integer
res Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
res) Integer
a) where
        res :: Integer
res = Fixed a -> Integer
forall k (a :: k) (p :: k -> *). HasResolution a => p a -> Integer
forall (p :: k -> *). p a -> Integer
resolution Fixed a
fa
    fromRational :: Rational -> Fixed a
fromRational Rational
r = (Integer -> Fixed a) -> Fixed a
forall {k} (a :: k) (f :: k -> *).
HasResolution a =>
(Integer -> f a) -> f a
withResolution (\Integer
res -> Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Rational -> Integer
forall b. Integral b => Rational -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (Rational
r Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* (Integer -> Rational
forall a. Real a => a -> Rational
toRational Integer
res))))

-- | @since 2.01
instance (HasResolution a) => RealFrac (Fixed a) where
    properFraction :: forall b. Integral b => Fixed a -> (b, Fixed a)
properFraction Fixed a
a = (b
i,Fixed a
a Fixed a -> Fixed a -> Fixed a
forall a. Num a => a -> a -> a
- (b -> Fixed a
forall a b. (Integral a, Num b) => a -> b
fromIntegral b
i)) where
        i :: b
i = Fixed a -> b
forall b. Integral b => Fixed a -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate Fixed a
a
    truncate :: forall b. Integral b => Fixed a -> b
truncate Fixed a
f = Rational -> b
forall b. Integral b => Rational -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate (Fixed a -> Rational
forall a. Real a => a -> Rational
toRational Fixed a
f)
    round :: forall b. Integral b => Fixed a -> b
round Fixed a
f = Rational -> b
forall b. Integral b => Rational -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (Fixed a -> Rational
forall a. Real a => a -> Rational
toRational Fixed a
f)
    ceiling :: forall b. Integral b => Fixed a -> b
ceiling Fixed a
f = Rational -> b
forall b. Integral b => Rational -> b
forall a b. (RealFrac a, Integral b) => a -> b
ceiling (Fixed a -> Rational
forall a. Real a => a -> Rational
toRational Fixed a
f)
    floor :: forall b. Integral b => Fixed a -> b
floor Fixed a
f = Rational -> b
forall b. Integral b => Rational -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (Fixed a -> Rational
forall a. Real a => a -> Rational
toRational Fixed a
f)

chopZeros :: Integer -> String
chopZeros :: Integer -> String
chopZeros Integer
0 = String
""
chopZeros Integer
a | Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
mod Integer
a Integer
10 Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 = Integer -> String
chopZeros (Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
div Integer
a Integer
10)
chopZeros Integer
a = Integer -> String
forall a. Show a => a -> String
show Integer
a

-- only works for positive a
showIntegerZeros :: Bool -> Int -> Integer -> String
showIntegerZeros :: Bool -> Int -> Integer -> String
showIntegerZeros Bool
True Int
_ Integer
0 = String
""
showIntegerZeros Bool
chopTrailingZeros Int
digits Integer
a = Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
digits Int -> Int -> Int
forall a. Num a => a -> a -> a
- String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s) Char
'0' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s' where
    s :: String
s = Integer -> String
forall a. Show a => a -> String
show Integer
a
    s' :: String
s' = if Bool
chopTrailingZeros then Integer -> String
chopZeros Integer
a else String
s

withDot :: String -> String
withDot :: String -> String
withDot String
"" = String
""
withDot String
s = Char
'.'Char -> String -> String
forall a. a -> [a] -> [a]
:String
s

-- | First arg is whether to chop off trailing zeros
--
-- === __Examples__
--
-- >>> showFixed True  (MkFixed 10000 :: Fixed E3)
-- "10"
--
-- >>> showFixed False (MkFixed 10000 :: Fixed E3)
-- "10.000"
--
showFixed :: (HasResolution a) => Bool -> Fixed a -> String
showFixed :: forall {k} (a :: k). HasResolution a => Bool -> Fixed a -> String
showFixed Bool
chopTrailingZeros fa :: Fixed a
fa@(MkFixed Integer
a) | Integer
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 = String
"-" String -> String -> String
forall a. [a] -> [a] -> [a]
++ (Bool -> Fixed a -> String
forall {k} (a :: k). HasResolution a => Bool -> Fixed a -> String
showFixed Bool
chopTrailingZeros (Fixed a -> Fixed a -> Fixed a
forall a. a -> a -> a
asTypeOf (Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer -> Integer
forall a. Num a => a -> a
negate Integer
a)) Fixed a
fa))
showFixed Bool
chopTrailingZeros fa :: Fixed a
fa@(MkFixed Integer
a) = (Integer -> String
forall a. Show a => a -> String
show Integer
i) String -> String -> String
forall a. [a] -> [a] -> [a]
++ (String -> String
withDot (Bool -> Int -> Integer -> String
showIntegerZeros Bool
chopTrailingZeros Int
digits Integer
fracNum)) where
    res :: Integer
res = Fixed a -> Integer
forall k (a :: k) (p :: k -> *). HasResolution a => p a -> Integer
forall (p :: k -> *). p a -> Integer
resolution Fixed a
fa
    (Integer
i,Integer
d) = Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
divMod Integer
a Integer
res
    -- enough digits to be unambiguous
    digits :: Int
digits = Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
ceiling (Double -> Double -> Double
forall a. Floating a => a -> a -> a
logBase Double
10 (Integer -> Double
forall a. Num a => Integer -> a
fromInteger Integer
res) :: Double)
    maxnum :: Integer
maxnum = Integer
10 Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
digits
    -- read floors, so show must ceil for `read . show = id` to hold. See #9240
    fracNum :: Integer
fracNum = Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
divCeil (Integer
d Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
maxnum) Integer
res
    divCeil :: a -> a -> a
divCeil a
x a
y = (a
x a -> a -> a
forall a. Num a => a -> a -> a
+ a
y a -> a -> a
forall a. Num a => a -> a -> a
- a
1) a -> a -> a
forall a. Integral a => a -> a -> a
`div` a
y

-- | @since 2.01
instance (HasResolution a) => Show (Fixed a) where
    showsPrec :: Int -> Fixed a -> String -> String
showsPrec Int
p Fixed a
n = Bool -> (String -> String) -> String -> String
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
6 Bool -> Bool -> Bool
&& Fixed a
n Fixed a -> Fixed a -> Bool
forall a. Ord a => a -> a -> Bool
< Fixed a
0) ((String -> String) -> String -> String)
-> (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String -> String -> String
showString (String -> String -> String) -> String -> String -> String
forall a b. (a -> b) -> a -> b
$ Bool -> Fixed a -> String
forall {k} (a :: k). HasResolution a => Bool -> Fixed a -> String
showFixed Bool
False Fixed a
n

-- | @since 4.3.0.0
instance (HasResolution a) => Read (Fixed a) where
    readPrec :: ReadPrec (Fixed a)
readPrec     = (Lexeme -> ReadPrec (Fixed a)) -> ReadPrec (Fixed a)
forall a. Num a => (Lexeme -> ReadPrec a) -> ReadPrec a
readNumber Lexeme -> ReadPrec (Fixed a)
forall {k} (a :: k).
HasResolution a =>
Lexeme -> ReadPrec (Fixed a)
convertFixed
    readListPrec :: ReadPrec [Fixed a]
readListPrec = ReadPrec [Fixed a]
forall a. Read a => ReadPrec [a]
readListPrecDefault
    readList :: ReadS [Fixed a]
readList     = ReadS [Fixed a]
forall a. Read a => ReadS [a]
readListDefault

convertFixed :: forall a . HasResolution a => Lexeme -> ReadPrec (Fixed a)
convertFixed :: forall {k} (a :: k).
HasResolution a =>
Lexeme -> ReadPrec (Fixed a)
convertFixed (Number Number
n)
 | Just (Integer
i, Integer
f) <- Integer -> Number -> Maybe (Integer, Integer)
numberToFixed Integer
e Number
n =
    Fixed a -> ReadPrec (Fixed a)
forall a. a -> ReadPrec a
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> Fixed a
forall a. Num a => Integer -> a
fromInteger Integer
i Fixed a -> Fixed a -> Fixed a
forall a. Num a => a -> a -> a
+ (Integer -> Fixed a
forall a. Num a => Integer -> a
fromInteger Integer
f Fixed a -> Fixed a -> Fixed a
forall a. Fractional a => a -> a -> a
/ (Fixed a
10 Fixed a -> Integer -> Fixed a
forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
e)))
    where r :: Integer
r = Proxy a -> Integer
forall k (a :: k) (p :: k -> *). HasResolution a => p a -> Integer
forall (p :: k -> *). p a -> Integer
resolution (Proxy a
forall {k} (t :: k). Proxy t
Proxy :: Proxy a)
          -- round 'e' up to help make the 'read . show == id' property
          -- possible also for cases where 'resolution' is not a
          -- power-of-10, such as e.g. when 'resolution = 128'
          e :: Integer
e = Double -> Integer
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
ceiling (Double -> Double -> Double
forall a. Floating a => a -> a -> a
logBase Double
10 (Integer -> Double
forall a. Num a => Integer -> a
fromInteger Integer
r) :: Double)
convertFixed Lexeme
_ = ReadPrec (Fixed a)
forall a. ReadPrec a
pfail

-- | Resolution of 1, this works the same as Integer.
data E0

-- | @since 4.1.0.0
instance HasResolution E0 where
    resolution :: forall (p :: * -> *). p E0 -> Integer
resolution p E0
_ = Integer
1

-- | Resolution of 1, this works the same as Integer.
--
-- === __Examples__
--
-- >>> show (MkFixed 12345 :: Fixed E0)
-- "12345.0"
--
-- >>> show (MkFixed 12345 :: Uni)
-- "12345.0"
--
type Uni = Fixed E0

-- | Resolution of 10^-1 = .1
data E1

-- | @since 4.1.0.0
instance HasResolution E1 where
    resolution :: forall (p :: * -> *). p E1 -> Integer
resolution p E1
_ = Integer
10

-- | Resolution of 10^-1 = .1
--
-- === __Examples__
--
-- >>> show (MkFixed 12345 :: Fixed E1)
-- "1234.5"
--
-- >>> show (MkFixed 12345 :: Deci)
-- "1234.5"
--
type Deci = Fixed E1

-- | Resolution of 10^-2 = .01, useful for many monetary currencies
data E2

-- | @since 4.1.0.0
instance HasResolution E2 where
    resolution :: forall (p :: * -> *). p E2 -> Integer
resolution p E2
_ = Integer
100

-- | Resolution of 10^-2 = .01, useful for many monetary currencies
--
-- === __Examples__
--
-- >>> show (MkFixed 12345 :: Fixed E2)
-- "123.45"
--
-- >>> show (MkFixed 12345 :: Centi)
-- "123.45"
--
type Centi = Fixed E2

-- | Resolution of 10^-3 = .001
data E3

-- | @since 4.1.0.0
instance HasResolution E3 where
    resolution :: forall (p :: * -> *). p E3 -> Integer
resolution p E3
_ = Integer
1000

-- | Resolution of 10^-3 = .001
--
-- === __Examples__
--
-- >>> show (MkFixed 12345 :: Fixed E3)
-- "12.345"
--
-- >>> show (MkFixed 12345 :: Milli)
-- "12.345"
--
type Milli = Fixed E3

-- | Resolution of 10^-6 = .000001
data E6

-- | @since 2.01
instance HasResolution E6 where
    resolution :: forall (p :: * -> *). p E6 -> Integer
resolution p E6
_ = Integer
1000000

-- | Resolution of 10^-6 = .000001
--
-- === __Examples__
--
-- >>> show (MkFixed 12345 :: Fixed E6)
-- "0.012345"
--
-- >>> show (MkFixed 12345 :: Micro)
-- "0.012345"
--
type Micro = Fixed E6

-- | Resolution of 10^-9 = .000000001
data E9

-- | @since 4.1.0.0
instance HasResolution E9 where
    resolution :: forall (p :: * -> *). p E9 -> Integer
resolution p E9
_ = Integer
1000000000

-- | Resolution of 10^-9 = .000000001
--
-- === __Examples__
--
-- >>> show (MkFixed 12345 :: Fixed E9)
-- "0.000012345"
--
-- >>> show (MkFixed 12345 :: Nano)
-- "0.000012345"
--
type Nano = Fixed E9

-- | Resolution of 10^-12 = .000000000001
data E12

-- | @since 2.01
instance HasResolution E12 where
    resolution :: forall (p :: * -> *). p E12 -> Integer
resolution p E12
_ = Integer
1000000000000

-- | Resolution of 10^-12 = .000000000001
--
-- === __Examples__
--
-- >>> show (MkFixed 12345 :: Fixed E12)
-- "0.000000012345"
--
-- >>> show (MkFixed 12345 :: Pico)
-- "0.000000012345"
--
type Pico = Fixed E12