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. You just have to be mindful of overflows if you use a fixed-size
representation, especially with operations like multiplication.
- 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 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
- type family a :+ b
- (*.) :: Num a => Fixed r a -> Fixed s a -> Fixed (r :+ s) a
- type family a :- b
- (/.) :: Integral a => Fixed r a -> Fixed s a -> Fixed (r :- s) 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 | |
Enum a => Enum (Fixed r a) | |
Eq a => Eq (Fixed r a) | |
(HasResolution r, Bits a, Integral a) => Fractional (Fixed r a) | |
(HasResolution r, Bits a, Integral a) => Num (Fixed r a) | |
Ord a => Ord (Fixed r a) | |
(HasResolution r, Bits a, Integral a) => Read (Fixed r a) | |
(HasResolution r, Bits a, Integral a) => Real (Fixed r a) | |
(HasResolution r, Bits a, Integral a) => RealFrac (Fixed r a) | |
(HasResolution r, Bits a, Integral a) => Show (Fixed r a) |
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.
(*.) :: Num a => Fixed r a -> Fixed s a -> Fixed (r :+ s) aSource
Multiplication without throwing away fractional information. Note that this doesn't help against losing significant information from the integer component. If you are concerned about preventing overflow then convert to a larger representation first.