X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FComparisons.hs;h=d033159f23f827fa3776ca2c6dd2ac775780af91;hb=638f544b522157f83b075e07a70b2f106e01ec68;hp=506ebab8217b8c46cda4bf2b8ee7fe636c619cf2;hpb=28710758dac0a1241911c6ba296b0685b877b5e3;p=spline3.git diff --git a/src/Comparisons.hs b/src/Comparisons.hs index 506ebab..d033159 100644 --- a/src/Comparisons.hs +++ b/src/Comparisons.hs @@ -1,12 +1,24 @@ +-- | Functions for comparing 'Double' values. module Comparisons where -- | epsilon is the value that will be used in all tests that require --- some measure of "closeness." Increasing it will make those tests --- more tolerant. +-- some measure of \"closeness.\" Increasing it will make those +-- tests more tolerant. 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 @@ -17,12 +29,26 @@ 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 --- | Takes a list of Doubles and returns the ones which are not very +-- | Takes a list of 'Double' and returns the ones which are not very -- positive. non_very_positive_entries :: [Double] -> [Double] non_very_positive_entries = filter (not . very_positive)