]> gitweb.michael.orlitzky.com - spline3.git/blobdiff - src/Comparisons.hs
Add the "zoom" function.
[spline3.git] / src / Comparisons.hs
index 506ebab8217b8c46cda4bf2b8ee7fe636c619cf2..6d8dc70d846e08159c9c4ae6ea49b11a530a25a2 100644 (file)
@@ -1,9 +1,10 @@
+-- | 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
 
@@ -17,12 +18,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)