packages feed

mini-1.6.5.0: src/Mini/Linear/Approx.hs

-- | Checking for approximate equality
module Mini.Linear.Approx (
  -- * Class
  Approx (
    (~=)
  ),
) where

import Prelude (
  Bool,
  Double,
  Float,
  abs,
  and,
  zipWith,
  ($),
  (-),
  (<=),
 )

-- Class

-- | The class of approximative types
class Approx a where
  infix 4 ~=

  -- | Approximately equal to
  (~=) :: a -> a -> Bool

-- | Absolute difference maximum of @1e-6@
instance Approx Float where
  a ~= b = abs (a - b) <= 1e-6

-- | Absolute difference maximum of @1e-12@
instance Approx Double where
  a ~= b = abs (a - b) <= 1e-12

instance (Approx a) => Approx [a] where
  a ~= b = and $ zipWith (~=) a b