Skip to content

Instantly share code, notes, and snippets.

@edsko
Last active February 12, 2019 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edsko/83d1ac7c41c113660a6e533fbd6c4b09 to your computer and use it in GitHub Desktop.
Save edsko/83d1ac7c41c113660a6e533fbd6c4b09 to your computer and use it in GitHub Desktop.
-- | Convert 'Double' to fixed precision
--
-- For precision 'E1', we have
--
-- > 1.000 1.010 1.040 1.049 1.050 1.051 1.090 1.100
-- > floor | 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.1
-- > round | 1.0 1.0 1.0 1.0 1.0(*) 1.1 1.1 1.1
-- > ceiling | 1.0 1.1 1.1 1.1 1.1 1.1 1.1 1.1
--
-- (*): See <https://en.wikipedia.org/wiki/IEEE_754#Rounding_rules>
doubleToFixed :: forall a. HasResolution a
=> (Double -> Integer) -- ^ Rounding policy
-> Double -> Fixed a
doubleToFixed r d = MkFixed $ r (d * fromIntegral (resolution (Proxy @a)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment