From a386491241d272f2bb569d33ee9aa7d073461c62 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 21 Jun 2015 18:51:59 +0200 Subject: Better fixed precision --- src/Trivmix.hs | 16 +++++++++++----- trivmix.cabal | 2 +- trivmix.nix | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Trivmix.hs b/src/Trivmix.hs index d95ea46..37ecec6 100644 --- a/src/Trivmix.hs +++ b/src/Trivmix.hs @@ -34,6 +34,8 @@ import Data.Char import Data.Function import Control.Monad + +import Data.Fixed import Data.CaseInsensitive ( CI ) import qualified Data.CaseInsensitive as CI @@ -48,14 +50,18 @@ data Options = Options data Level = Lin Float | DB Float -fixedPrecision :: (RealFrac a, Num a) => a -> a -> a -fixedPrecision p x = (fromInteger $ round $ p' * x) / p' - where - p' = 1 / p +withPrec :: (Num a, RealFrac a, HasResolution b) => a -> Fixed b +withPrec x = withResolution $ \p' -> MkFixed $ toInteger $ round $ x * (fromInteger p') + +withType :: (p a -> f a) -> f a +withType f = f undefined + +withResolution :: (HasResolution a) => (Integer -> f a) -> f a +withResolution f = withType (f . resolution) instance Show Level where show (Lin x) = show x - show (DB x) = (show $ fixedPrecision 1e-3 x') ++ "dB" + show (DB x) = (show $ (withPrec x' :: Milli)) ++ "dB" where x' = 20 * (logBase 10 x) diff --git a/trivmix.cabal b/trivmix.cabal index a5af14d..40aa9a8 100644 --- a/trivmix.cabal +++ b/trivmix.cabal @@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: trivmix -version: 2.4.5 +version: 2.4.6 -- synopsis: -- description: license: PublicDomain diff --git a/trivmix.nix b/trivmix.nix index 7d97c01..fae88fc 100644 --- a/trivmix.nix +++ b/trivmix.nix @@ -6,7 +6,7 @@ cabal.mkDerivation (self: { pname = "trivmix"; - version = "2.4.5"; + version = "2.4.6"; src = ./.; isLibrary = false; isExecutable = true; -- cgit v1.2.3