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
operations. This type is also polymorphic on the underlying
representation, so you can use whatever size and signedness you
- 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
- 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
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.
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.
Losslessly cast to a supertype.
Cast to a subtype. Information may be lost.
Instances of this class are useful as the first parameter of
Given a fixed-point number, give the number of bits used to represent its fractional part.
Fast conversion between fixed-point numbers with the same fractional size.
Fast conversion between fixed-point numbers with the same representation size.
Fast conversion from floating-point to fixed-point.
Multiplication without throwing away fractional information.
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).
Division while removing unnecessary bits in the result's fractional part.
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.