]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Comparisons.hs
d033159f23f827fa3776ca2c6dd2ac775780af91
[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 -- | A tiny margin of error.
12 theta :: Double
13 theta = 0.0000000000001
14
15 -- | x almost equals y if x is within 'theta' of y.
16 nearly_equals :: Double -> Double -> Bool
17 nearly_equals x y = (abs (x - y)) < theta
18
19 -- | Nearly greater-than or equal-to.
20 nearly_ge :: Double -> Double -> Bool
21 x `nearly_ge` y = (x > y) || (x `nearly_equals` y)
22
23 -- | x almost equals y if x is within 'epsilon' of y.
24 almost_equals :: Double -> Double -> Bool
25 almost_equals x y = (abs (x - y)) < epsilon
26
27 infix 4 ~=
28 (~=) :: Double -> Double -> Bool
29 (~=) = almost_equals
30
31
32 -- | Like 'almost_equals', except much more tolerant. The difference
33 -- between the two arguments must be less than one percent of the sum
34 -- of their magnitudes.
35 kinda_equals :: Double -> Double -> Bool
36 kinda_equals x y =
37 (abs (x - y)) < threshold
38 where
39 threshold = ((abs x) + (abs y)) / 100.0
40
41 infix 4 ~~=
42 (~~=) :: Double -> Double -> Bool
43 (~~=) = kinda_equals
44
45
46 -- | x is very positive if it is 'epsilon' greater than zero.
47 very_positive :: Double -> Bool
48 very_positive x = x - epsilon > 0
49
50
51 -- | Takes a list of 'Double' and returns the ones which are not very
52 -- positive.
53 non_very_positive_entries :: [Double] -> [Double]
54 non_very_positive_entries = filter (not . very_positive)