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 export
(Ord a, Abs a, Neg a, Fractional a) => FieldCmp a where FieldCmp Double where
abslt x y = abs x < abs y 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 -- Multiplication
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -82,8 +94,8 @@ interface MultMonoid a => MultGroup a where
constructor MkMultGroup constructor MkMultGroup
||| Calculate the inverse of the matrix or transformation. ||| Calculate the inverse of the matrix or transformation.
||| WARNING: This function will not check if an inverse exists for the given ||| WARNING: This function will not check if an inverse exists for the given
||| input, and will happily return results containing NaN values. To avoid ||| input, and will happily divide by zero or return results containing NaN.
||| this, use `tryInverse` instead. ||| To avoid this, use `tryInverse` instead.
inverse : a -> a inverse : a -> a