HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulebase-4.20.2.0Haskell2010

Data.Fixed

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.

Example1 expression
realToFrac (0.123456 :: Pico) :: Milli0.123
  • 15 types
  • 1 class
  • 4 values
  • Packagebase-4.20.2.0
  • Exports20
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceFixed.hs

The Fixed Type

3 declarations
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

Instances10Enum, Eq, Fractional, Data, Num, Ord, …
  • 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
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.

Instances8HasResolution, …
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"

Resolution / Scaling Factors

0 declarations

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

datadata E0
#

Resolution of 1, this works the same as Integer.

Instances1HasResolution
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"

1/10

datadata E1
#

Resolution of 10^-1 = .1

Instances1HasResolution
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"

1/100

datadata E2
#

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

Instances1HasResolution
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"

1/1 000

datadata E3
#

Resolution of 10^-3 = .001

Instances1HasResolution
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"

1/1 000 000

datadata E6
#

Resolution of 10^-6 = .000001

Instances1HasResolution
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"

1/1 000 000 000

datadata E9
#

Resolution of 10^-9 = .000000001

Instances1HasResolution
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"

1/1 000 000 000 000

datadata E12
#

Resolution of 10^-12 = .000000000001

Instances1HasResolution
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"

Generalized Functions on Real's

3 declarations
valuemod' :: Real a => a -> a -> a
#

Generalisation of mod to any instance of Real