module Numeric.Eps
( Epsilon(..), isNz, roundZero, roundOne, roundZeroOne,
nearOne
) where
import Data.Complex
import Foreign.C.Types (CFloat, CDouble)
class (Floating a, Num a) => Epsilon a where
nearZero :: a -> Bool
instance Epsilon Float where
nearZero a = abs a <= 1e-6
instance Epsilon Double where
nearZero a = abs a <= 1e-12
instance Epsilon CFloat where
nearZero a = abs a <= 1e-6
instance Epsilon CDouble where
nearZero a = abs a <= 1e-12
instance Epsilon (Complex Float) where
nearZero a = magnitude a <= 1e-6
instance Epsilon (Complex Double) where
nearZero a = magnitude a <= 1e-12
instance Epsilon (Complex CFloat) where
nearZero a = magnitude a <= 1e-6
instance Epsilon (Complex CDouble) where
nearZero a = magnitude a <= 1e-12
nearOne :: Epsilon a => a -> Bool
nearOne x = nearZero (1 x)
isNz :: Epsilon a => a -> Bool
isNz x = not (nearZero x)
withDefault :: (t -> Bool) -> t -> t -> t
withDefault q d x | q x = d
| otherwise = x
roundZero, roundOne, roundZeroOne :: Epsilon a => a -> a
roundZero = withDefault nearZero (fromIntegral (0 :: Int))
roundOne = withDefault nearOne (fromIntegral (1 :: Int))
with2Defaults :: (t -> Bool) -> (t -> Bool) -> t -> t -> t -> t
with2Defaults q1 q2 d1 d2 x | q1 x = d1
| q2 x = d2
| otherwise = x
roundZeroOne = with2Defaults nearZero nearOne (fromIntegral (0 :: Int)) (fromIntegral (1 :: Int))