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 |
Safe Haskell | Trustworthy |
Language | Haskell2010 |
Data.Fixed
Description
This module defines a "Fixed" type for fixed-precision arithmetic.
The parameter to Fixed
is any type that's an instance of HasResolution
.
HasResolution
has a single method that gives the resolution of the Fixed
type.
This module also contains generalisations of div
, mod
, and divMod
to
work with any Real
instance.
Synopsis
- div' :: (Real a, Integral b) => a -> a -> b
- mod' :: Real a => a -> a -> a
- divMod' :: (Real a, Integral b) => a -> a -> (b, a)
- newtype Fixed (a :: k) = MkFixed Integer
- class HasResolution (a :: k) where
- resolution :: p a -> Integer
- showFixed :: HasResolution a => Bool -> Fixed a -> String
- data E0
- type Uni = Fixed E0
- data E1
- type Deci = Fixed E1
- data E2
- type Centi = Fixed E2
- data E3
- type Milli = Fixed E3
- data E6
- type Micro = Fixed E6
- data E9
- type Nano = Fixed E9
- data E12
- type Pico = Fixed E12
Documentation
newtype Fixed (a :: k) Source #
The type parameter should be an instance of HasResolution
.
Instances
Instances details
Instance details
Defined in Data.Fixed
Methods
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Fixed a -> c (Fixed a) Source #
gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Fixed a) Source #
toConstr :: Fixed a -> Constr Source #
dataTypeOf :: Fixed a -> DataType Source #
dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Fixed a)) Source #
dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Fixed a)) Source #
gmapT :: (forall b. Data b => b -> b) -> Fixed a -> Fixed a Source #
gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Fixed a -> r Source #
gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Fixed a -> r Source #
gmapQ :: (forall d. Data d => d -> u) -> Fixed a -> [u] Source #
gmapQi :: Int -> (forall d. Data d => d -> u) -> Fixed a -> u Source #
gmapM :: Monad m => (forall d. Data d => d -> m d) -> Fixed a -> m (Fixed a) Source #
gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Fixed a -> m (Fixed a) Source #
gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Fixed a -> m (Fixed a) Source #
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.
Since: base-2.1
Instance details
Defined in Data.Fixed
Methods
succ :: Fixed a -> Fixed a Source #
pred :: Fixed a -> Fixed a Source #
toEnum :: Int -> Fixed a Source #
fromEnum :: Fixed a -> Int Source #
enumFrom :: Fixed a -> [Fixed a] Source #
enumFromThen :: Fixed a -> Fixed a -> [Fixed a] Source #
enumFromTo :: Fixed a -> Fixed a -> [Fixed a] Source #
enumFromThenTo :: Fixed a -> Fixed a -> Fixed a -> [Fixed a] Source #
Instance details
Defined in Data.Fixed
Instance details
Defined in Data.Fixed
class HasResolution (a :: k) where Source #
Methods
resolution :: p a -> Integer Source #
showFixed :: HasResolution a => Bool -> Fixed a -> String Source #
First arg is whether to chop off trailing zeros