]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Comparisons.hs
58a57473fac441f5c7ff305a565c5e6b0bc26cee
[spline3.git] / src / Comparisons.hs
1 -- | Functions for comparing 'Double' values.
2 module Comparisons (
3 (~=),
4 (~~=),
5 almost_equals,
6 kinda_equals,
7 nearly_equals,
8 nearly_ge,
9 non_very_positive_entries,
10 very_positive,
11 )
12 where
13
14 -- | epsilon is the value that will be used in all tests that require
15 -- some measure of \"closeness.\" Increasing it will make those
16 -- tests more tolerant.
17 epsilon :: Double
18 epsilon = 0.0001
19
20 -- | A tiny margin of error.
21 theta :: Double
22 theta = 0.0000000000001
23
24
25 -- | x almost equals y if x is within 'theta' of y.
26 --
27 -- Only used in tests.
28 --
29 nearly_equals :: Double -> Double -> Bool
30 nearly_equals x y =
31 (abs (x - y)) < theta
32
33
34 -- | Nearly greater-than or equal-to.
35 --
36 -- Only used in tests.
37 --
38 nearly_ge :: Double -> Double -> Bool
39 x `nearly_ge` y =
40 (x > y) || (x `nearly_equals` y)
41
42
43 -- | x almost equals y if x is within 'epsilon' of y.
44 --
45 -- Only used in tests.
46 --
47 almost_equals :: Double -> Double -> Bool
48 almost_equals x y =
49 (abs (x - y)) < epsilon
50
51 infix 4 ~=
52 (~=) :: Double -> Double -> Bool
53 (~=) = almost_equals
54
55
56 -- | Like 'almost_equals', except much more tolerant. The difference
57 -- between the two arguments must be less than one percent of the sum
58 -- of their magnitudes.
59 --
60 -- Only used in tests.
61 --
62 kinda_equals :: Double -> Double -> Bool
63 kinda_equals x y =
64 (abs (x - y)) < threshold
65 where
66 threshold = ((abs x) + (abs y)) / 100.0
67
68 infix 4 ~~=
69 (~~=) :: Double -> Double -> Bool
70 (~~=) = kinda_equals
71
72
73 -- | x is very positive if it is 'epsilon' greater than zero.
74 --
75 -- Only used in tests.
76 --
77 very_positive :: Double -> Bool
78 very_positive x =
79 x - epsilon > 0
80
81
82 -- | Takes a list of 'Double' and returns the ones which are not very
83 -- positive.
84 non_very_positive_entries :: [Double] -> [Double]
85 non_very_positive_entries = filter (not . very_positive)