X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FComparisons.hs;h=3bf4e246bcfba473634b255fb1befb8187a84b8f;hb=cc42825a4df8226f217e59e852950c36c0ec67ba;hp=4bf4cebb69ece2947eb1deccf0dfa9cfda20f266;hpb=58cf11569acb270995d2de924dda03ef526647e2;p=spline3.git diff --git a/src/Comparisons.hs b/src/Comparisons.hs index 4bf4ceb..3bf4e24 100644 --- a/src/Comparisons.hs +++ b/src/Comparisons.hs @@ -1,13 +1,33 @@ -- | Functions for comparing 'Double' values. -module Comparisons +module Comparisons ( + (~=), + (~~=), + almost_equals, + kinda_equals, + nearly_equals, + nearly_ge, + non_very_positive_entries, + very_positive, + ) 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 @@ -18,6 +38,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