This module defines a type for binary fixed-point arithmetic. The main
advantage this provides over decimal fixed-point arithmetic is that
the point is maintained using fast bit shifts instead of slow `div`

operations. This type is also polymorphic on the underlying
representation, so you can use whatever size and signedness you
want.

- div' :: (Real a, Integral b) => a -> a -> b
- mod' :: Real a => a -> a -> a
- divMod' :: (Real a, Integral b) => a -> a -> (b, a)
- data Fixed r a
- class SuperTypeable a where
- class HasResolution r where
- resolution :: Num a => Fixed r a -> Int

- data E0
- type E1 = S E0
- type E2 = E1 :+ E1
- type E4 = E2 :+ E2
- type E8 = E4 :+ E4
- type E10 = S (S E8)
- type E16 = E8 :+ E8
- type E20 = E10 :+ E10
- type E30 = E20 :+ E10
- type E32 = E16 :+ E16
- type E64 = E32 :+ E32
- data S n
- type family P a
- fixedRadix :: (Integral a, Num b) => Fixed r a -> Fixed r b
- fixedSize :: (HasResolution r, HasResolution s, Bits a) => Fixed r a -> Fixed s a
- fromRealFloat :: (RealFloat a, HasResolution r, Num b) => a -> Fixed r b
- type family a :+ b
- (*.) :: (Num (Super a), SuperTypeable a) => Fixed r a -> Fixed s a -> Fixed (r :+ s) a
- (*!) :: (HasResolution r, Bits a, Num a) => Fixed r a -> Fixed r a -> Fixed r a
- type family a :- b
- (/.) :: Integral a => Fixed r a -> Fixed s a -> Fixed (r :- s) a
- (/!) :: (HasResolution r, Bits a, Integral a) => Fixed r a -> Fixed r a -> Fixed r a

# Documentation

The first type parameter represents the number of bits to devote
to the fractional part of the number. The second type parameter is
the underlying representation. For example, `Fixed E8 Int16`

uses
eight bits for the integer component (of which one bit is used for
the sign) and eight bits for the fractional component.

Typeable2 Fixed | |

Bounded a => Bounded (Fixed r a) | |

Enum a => Enum (Fixed r a) | |

Eq a => Eq (Fixed r a) | |

(HasResolution r, Bits a, Bits (Super a), Integral a, Integral (Super a), SuperTypeable a) => Fractional (Fixed r a) | |

(HasResolution r, Bits a, Bits (Super a), Integral a, Num (Super a), Integral (Super a), SuperTypeable a) => Num (Fixed r a) | |

Ord a => Ord (Fixed r a) | |

(HasResolution r, Bits a, Bits (Super a), Integral a, Integral (Super a), SuperTypeable a) => Read (Fixed r a) | |

(HasResolution r, Bits a, Bits (Super a), Integral a, Integral (Super a), SuperTypeable a) => Real (Fixed r a) | |

(HasResolution r, Bits a, Bits (Super a), Integral a, Integral (Super a), SuperTypeable a) => RealFrac (Fixed r a) | |

(HasResolution r, Bits a, Bits (Super a), Integral a, Integral (Super a), SuperTypeable a) => Show (Fixed r a) | |

(SuperTypeable a, Num a, Num (Super a), Integral a, Integral (Super a)) => SuperTypeable (Fixed r a) |

class SuperTypeable a whereSource

Instances of `SuperTypeable`

can be cast up to and down from a
supertype. If the type is bounded, the supertype must be able to
hold at least twice as much information to be a valid instance.

superCast :: a -> Super aSource

Losslessly cast to a supertype.

Cast to a subtype. Information may be lost.

class HasResolution r whereSource

Instances of this class are useful as the first parameter of
`Fixed`

.

resolution :: Num a => Fixed r a -> IntSource

Given a fixed-point number, give the number of bits used to represent its fractional part.

HasResolution E0 | |

HasResolution n => HasResolution (S n) |

fixedRadix :: (Integral a, Num b) => Fixed r a -> Fixed r bSource

Fast conversion between fixed-point numbers with the same fractional size.

fixedSize :: (HasResolution r, HasResolution s, Bits a) => Fixed r a -> Fixed s aSource

Fast conversion between fixed-point numbers with the same representation size.

fromRealFloat :: (RealFloat a, HasResolution r, Num b) => a -> Fixed r bSource

Fast conversion from floating-point to fixed-point.

(*.) :: (Num (Super a), SuperTypeable a) => Fixed r a -> Fixed s a -> Fixed (r :+ s) aSource

Multiplication without throwing away fractional information.

(*!) :: (HasResolution r, Bits a, Num a) => Fixed r a -> Fixed r a -> Fixed r aSource

Perform a multiplication without adding any extra bits for the
intermediate steps. This may be faster (especially when you are
already working with native-sized integer data), but it's only safe
to use if you are sure that the multiplication won't
overflow. Normal multiplication is equivalent to ```
x y -> subCast
(superCast x *! superCast y)
```

.

(/.) :: Integral a => Fixed r a -> Fixed s a -> Fixed (r :- s) aSource

Division while removing unnecessary bits in the result's fractional part.

(/!) :: (HasResolution r, Bits a, Integral a) => Fixed r a -> Fixed r a -> Fixed r aSource

Perform a division without adding any extra bits for the intermediate steps. This may be faster if supercasting brings it up to a non-native size, but you need to be sure that the shifting before the division won't cause an overflow.