```-----------------------------------------------------------------------------
-- |
-- Copyright   :  (C) 2012-2015 Edward Kmett
-- Maintainer  :  Edward Kmett <ekmett@gmail.com>
-- Stability   :  provisional
-- Portability :  portable
--
-- Testing for values "near" zero
-----------------------------------------------------------------------------
module Linear.Epsilon
( Epsilon(..)
) where
import Data.Complex (Complex, magnitude)
import Foreign.C.Types (CFloat, CDouble)

-- | Provides a fairly subjective test to see if a quantity is near zero.
--
-- >>> nearZero (1e-11 :: Double)
-- False
--
-- >>> nearZero (1e-17 :: Double)
-- True
--
-- >>> nearZero (1e-5 :: Float)
-- False
--
-- >>> nearZero (1e-7 :: Float)
-- True
class Num a => Epsilon a where
-- | Determine if a quantity is near zero.
nearZero :: a -> Bool

-- | @'abs' a '<=' 1e-6@
instance Epsilon Float where
nearZero :: Float -> Bool
nearZero Float
a = Float -> Float
forall a. Num a => a -> a
abs Float
a Float -> Float -> Bool
forall a. Ord a => a -> a -> Bool
<= Float
1e-6

-- | @'abs' a '<=' 1e-12@
instance Epsilon Double where
nearZero :: Double -> Bool
nearZero Double
a = Double -> Double
forall a. Num a => a -> a
abs Double
a Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
1e-12

-- | @'abs' a '<=' 1e-6@
instance Epsilon CFloat where
nearZero :: CFloat -> Bool
nearZero CFloat
a = CFloat -> CFloat
forall a. Num a => a -> a
abs CFloat
a CFloat -> CFloat -> Bool
forall a. Ord a => a -> a -> Bool
<= CFloat
1e-6

-- | @'abs' a '<=' 1e-12@
instance Epsilon CDouble where
nearZero :: CDouble -> Bool
nearZero CDouble
a = CDouble -> CDouble
forall a. Num a => a -> a
abs CDouble
a CDouble -> CDouble -> Bool
forall a. Ord a => a -> a -> Bool
<= CDouble
1e-12

instance (Epsilon a, RealFloat a) => Epsilon (Complex a) where
nearZero :: Complex a -> Bool
nearZero = a -> Bool
forall a. Epsilon a => a -> Bool
nearZero (a -> Bool) -> (Complex a -> a) -> Complex a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Complex a -> a
forall a. RealFloat a => Complex a -> a
magnitude
```