Add approximate floating point comparison

This commit is contained in:
Kiana Sheibani 2022-10-21 17:59:22 -04:00
parent 8e58fc8a1a
commit be119f5116
Signed by: toki
GPG key ID: 6CB106C25E86A9F7

View file

@ -28,10 +28,22 @@ interface (Eq a, Neg a, Fractional a) => FieldCmp a where
export
(Ord a, Abs a, Neg a, Fractional a) => FieldCmp a where
abslt x y = abs x < abs y
FieldCmp Double where
abslt = (<) `on` abs
-- Alternative implementations of `Eq` and `FieldCmp` that compare floating
-- point numbers approximately, useful when working with transforms
namespace Eq
export
WithEpsilon : Double -> Eq Double
WithEpsilon ep = MkEq (\x,y => x - y < ep) (\x,y => x - y >= ep)
namespace FieldCmp
export
WithEpsilon : Double -> FieldCmp Double
WithEpsilon ep = MkFieldCmp @{WithEpsilon ep} ((<) `on` abs)
--------------------------------------------------------------------------------
-- Multiplication
--------------------------------------------------------------------------------
@ -82,8 +94,8 @@ interface MultMonoid a => MultGroup a where
constructor MkMultGroup
||| Calculate the inverse of the matrix or transformation.
||| WARNING: This function will not check if an inverse exists for the given
||| input, and will happily return results containing NaN values. To avoid
||| this, use `tryInverse` instead.
||| input, and will happily divide by zero or return results containing NaN.
||| To avoid this, use `tryInverse` instead.
inverse : a -> a