Proposal: add conversion functions to Data.Fixed.

Ashley Yakeley ashley at semantic.org
Sat Dec 1 10:37:10 CET 2012


On 28/11/12 09:07, Jeff Shaw wrote:
> - | Get a Fixed value's Integer representation and resolution.
> fromFixed :: (HasResolution a) => Fixed a -> (Integer,Integer)
> fromFixed f@(MkFixed i) = (i, resolution f)
>
> -- | Given an Integer representation and resolution of a Fixed value,
> -- create a new Fixed value with an arbitrary resolution.
> toFixed :: (HasResolution a) => (Integer,Integer) -> Fixed a
> toFixed (i,r0) = withResolution (\r1 -> MkFixed $ (i * r1) `div` r0)

How about...

fromFixed f = let r = resolution f in (truncate (f * r),r)

toFixed (i,r) = fromRational (i % r)

-- Ashley



More information about the Libraries mailing list