X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FComparisons.hs;h=d033159f23f827fa3776ca2c6dd2ac775780af91;hb=02a34bf79286cd63edcc62e8610fe045feb1dd68;hp=b5d76d5b11d6da85ca386021be918041d83abd53;hpb=d60de7d5ab06d6c3261826b843ee89cf12ab9667;p=spline3.git diff --git a/src/Comparisons.hs b/src/Comparisons.hs index b5d76d5..d033159 100644 --- a/src/Comparisons.hs +++ b/src/Comparisons.hs @@ -8,6 +8,17 @@ where epsilon :: Double epsilon = 0.0001 +-- | A tiny margin of error. +theta :: Double +theta = 0.0000000000001 + +-- | x almost equals y if x is within 'theta' of y. +nearly_equals :: Double -> Double -> Bool +nearly_equals x y = (abs (x - y)) < theta + +-- | Nearly greater-than or equal-to. +nearly_ge :: Double -> Double -> Bool +x `nearly_ge` y = (x > y) || (x `nearly_equals` y) -- | x almost equals y if x is within 'epsilon' of y. almost_equals :: Double -> Double -> Bool @@ -18,6 +29,20 @@ infix 4 ~= (~=) = almost_equals +-- | Like 'almost_equals', except much more tolerant. The difference +-- between the two arguments must be less than one percent of the sum +-- of their magnitudes. +kinda_equals :: Double -> Double -> Bool +kinda_equals x y = + (abs (x - y)) < threshold + where + threshold = ((abs x) + (abs y)) / 100.0 + +infix 4 ~~= +(~~=) :: Double -> Double -> Bool +(~~=) = kinda_equals + + -- | x is very positive if it is 'epsilon' greater than zero. very_positive :: Double -> Bool very_positive x = x - epsilon > 0