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