HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulererebase-1.21.2Haskell2010

Data.Fixed

  • 15 types
  • 1 class
  • 4 values
newtypenewtype Fixed (a :: k)
#

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

Constructors

Instances16Lift, NFData1, Enum, Eq, Fractional, Data, …
  • Lift (Fixed a)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • NFData1 FixedDefined in deepseq-1.5.0.0 · Control.DeepSeq
  • Enum (Fixed a)Defined in base-4.20.2.0 · Data.Fixed

    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,

    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^-12 is the smallest (positive) amount that can be added to a value of type Pico = Fixed E12 without changing its resolution, and so

    Example1 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" 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.

  • Eq (Fixed a)Defined in base-4.20.2.0 · Data.Fixed
  • HasResolution 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.Fixed
  • HasResolution a => Num (Fixed a)Defined in base-4.20.2.0 · Data.Fixed

    Multiplication 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.Fixed
  • HasResolution a => Read (Fixed a)Defined in base-4.20.2.0 · Data.Fixed
  • HasResolution a => Real (Fixed a)Defined in base-4.20.2.0 · Data.Fixed
  • HasResolution a => RealFrac (Fixed a)Defined in base-4.20.2.0 · Data.Fixed
  • HasResolution a => Show (Fixed a)Defined in base-4.20.2.0 · Data.Fixed
  • NFData (Fixed a)Defined in deepseq-1.5.0.0 · Control.DeepSeq
  • Binary (Fixed a)Defined in binary-0.8.9.3 · Data.Binary.Class
  • Hashable (Fixed a)Defined in hashable-1.4.7.0 · Data.Hashable.Class
  • HasResolution a => Default (Fixed a)Defined in data-default-0.8.0.1 · Data.Default.Internal
valuemod' :: Real a => a -> a -> a
#

Generalisation of mod to any instance of Real

valueshowFixed :: HasResolution a => Bool -> Fixed a -> String
#

First arg is whether to chop off trailing zeros

Examples
Example1 expression
showFixed True  (MkFixed 10000 :: Fixed E3)"10"
Example1 expression
showFixed False (MkFixed 10000 :: Fixed E3)"10.000"
typetype Centi = Fixed E2
#

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

Examples
Example1 expression
show (MkFixed 12345 :: Fixed E2)"123.45"
Example1 expression
show (MkFixed 12345 :: Centi)"123.45"
typetype Deci = Fixed E1
#

Resolution of 10^-1 = .1

Examples
Example1 expression
show (MkFixed 12345 :: Fixed E1)"1234.5"
Example1 expression
show (MkFixed 12345 :: Deci)"1234.5"
datadata E0
#

Resolution of 1, this works the same as Integer.

Instances1HasResolution
datadata E1
#

Resolution of 10^-1 = .1

Instances1HasResolution
datadata E12
#

Resolution of 10^-12 = .000000000001

Instances1HasResolution
datadata E2
#

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

Instances1HasResolution
datadata E3
#

Resolution of 10^-3 = .001

Instances1HasResolution
datadata E6
#

Resolution of 10^-6 = .000001

Instances1HasResolution
datadata E9
#

Resolution of 10^-9 = .000000001

Instances1HasResolution
classclass HasResolution (a :: k) where
#

Types which can be used as a resolution argument to the Fixed type constructor must implement the HasResolution typeclass.

Methods

  • resolution :: p a -> Integer

    Provide the resolution for a fixed-point fractional number.

Instances10HasResolution, …
typetype Micro = Fixed E6
#

Resolution of 10^-6 = .000001

Examples
Example1 expression
show (MkFixed 12345 :: Fixed E6)"0.012345"
Example1 expression
show (MkFixed 12345 :: Micro)"0.012345"
typetype Milli = Fixed E3
#

Resolution of 10^-3 = .001

Examples
Example1 expression
show (MkFixed 12345 :: Fixed E3)"12.345"
Example1 expression
show (MkFixed 12345 :: Milli)"12.345"
typetype Nano = Fixed E9
#

Resolution of 10^-9 = .000000001

Examples
Example1 expression
show (MkFixed 12345 :: Fixed E9)"0.000012345"
Example1 expression
show (MkFixed 12345 :: Nano)"0.000012345"
typetype Pico = Fixed E12
#

Resolution of 10^-12 = .000000000001

Examples
Example1 expression
show (MkFixed 12345 :: Fixed E12)"0.000000012345"
Example1 expression
show (MkFixed 12345 :: Pico)"0.000000012345"
typetype Uni = Fixed E0
#

Resolution of 1, this works the same as Integer.

Examples
Example1 expression
show (MkFixed 12345 :: Fixed E0)"12345.0"
Example1 expression
show (MkFixed 12345 :: Uni)"12345.0"