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 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src/Trivmix.hs') 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) -- cgit v1.2.3