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
Instances10Enum, Eq, Fractional, Data, Num, Ord, …
Enum (Fixed a)Defined in base-4.20.2.0 · Data.FixedRecall 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 resolution10^-3oftype Milli = Fixed E3,Example1 expression succ (0.000 :: Milli)0.001
and likewise
Example1 expression 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^-12is the smallest (positive) amount that can be added to a value oftype Pico = Fixed E12without changing its resolution, and soExample1 expression succ (0.000000000000 :: Pico)0.000000000001
and similarly
Example1 expression 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" of10^-12. Hence, the list[1..10] :: [Pico]has the form[1.000000000000, 1.00000000001, 1.00000000002, ..., 10.000000000000]and contains
9 * 10^12 + 1values.Eq (Fixed a)Defined in base-4.20.2.0 · Data.FixedHasResolution a => Fractional (Fixed a)Defined in base-4.20.2.0 · Data.Fixed(Typeable k, Typeable a) => Data (Fixed a)Defined in base-4.20.2.0 · Data.FixedHasResolution a => Num (Fixed a)Defined in base-4.20.2.0 · Data.FixedMultiplication is not associative or distributive:
Example1 expression (0.2 * 0.6 :: Deci) * 0.9 == 0.2 * (0.6 * 0.9)False
Example1 expression (0.1 + 0.1 :: Deci) * 0.5 == 0.1 * 0.5 + 0.1 * 0.5False
Ord (Fixed a)Defined in base-4.20.2.0 · Data.FixedHasResolution a => Read (Fixed a)Defined in base-4.20.2.0 · Data.FixedHasResolution a => Real (Fixed a)Defined in base-4.20.2.0 · Data.FixedHasResolution a => RealFrac (Fixed a)Defined in base-4.20.2.0 · Data.FixedHasResolution a => Show (Fixed a)Defined in base-4.20.2.0 · Data.Fixed