fixed-point-0.2.0.0: Binary fixed-point arithmetic

Data.Fixed.Binary

Description

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.

Synopsis

Documentation

div' :: (Real a, Integral b) => a -> a -> b

generalisation of div to any instance of Real

mod' :: Real a => a -> a -> a

generalisation of mod to any instance of Real

divMod' :: (Real a, Integral b) => a -> a -> (b, a)

generalisation of divMod to any instance of Real

data Fixed r a Source

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.

Instances

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.

Methods

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

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

data E0 Source

Instances

type E1 = S E0Source

type E2 = E1 :+ E1Source

type E4 = E2 :+ E2Source

type E8 = E4 :+ E4Source

type E10 = S (S E8)Source

type E16 = E8 :+ E8Source

data S n Source

Increment a resolution

Instances

type family P a Source

Decrement a resolution

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.

type family a :+ b Source

Add resolutions

(*.) :: 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.

type family a :- b Source

Subtract resolutions

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

Division without throwing away fractional information. Same caveats apply as with '(*.)'.