]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Comparisons.hs
Add the kinda_equals comparison.
[spline3.git] / src / Comparisons.hs
1 -- | Functions for comparing 'Double' values.
2 module Comparisons
3 where
4
5 -- | epsilon is the value that will be used in all tests that require
6 -- some measure of \"closeness.\" Increasing it will make those
7 -- tests more tolerant.
8 epsilon :: Double
9 epsilon = 0.0001
10
11
12 -- | x almost equals y if x is within 'epsilon' of y.
13 almost_equals :: Double -> Double -> Bool
14 almost_equals x y = (abs (x - y)) < epsilon
15
16 infix 4 ~=
17 (~=) :: Double -> Double -> Bool
18 (~=) = almost_equals
19
20
21 -- | Like 'almost_equals', except much more tolerant. The difference
22 -- between the two arguments must be less than one percent of the sum
23 -- of their magnitudes.
24 kinda_equals :: Double -> Double -> Bool
25 kinda_equals x y =
26 (abs (x - y)) < threshold
27 where
28 threshold = ((abs x) + (abs y)) / 100.0
29
30 infix 4 ~~=
31 (~~=) :: Double -> Double -> Bool
32 (~~=) = kinda_equals
33
34
35 -- | x is very positive if it is 'epsilon' greater than zero.
36 very_positive :: Double -> Bool
37 very_positive x = x - epsilon > 0
38
39
40 -- | Takes a list of 'Double' and returns the ones which are not very
41 -- positive.
42 non_very_positive_entries :: [Double] -> [Double]
43 non_very_positive_entries = filter (not . very_positive)