]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Comparisons.hs
Clean up imports/exports.
[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 -- | x almost equals y if x is within 'theta' of y.
25 nearly_equals :: Double -> Double -> Bool
26 nearly_equals x y = (abs (x - y)) < theta
27
28 -- | Nearly greater-than or equal-to.
29 nearly_ge :: Double -> Double -> Bool
30 x `nearly_ge` y = (x > y) || (x `nearly_equals` y)
31
32 -- | x almost equals y if x is within 'epsilon' of y.
33 almost_equals :: Double -> Double -> Bool
34 almost_equals x y = (abs (x - y)) < epsilon
35
36 infix 4 ~=
37 (~=) :: Double -> Double -> Bool
38 (~=) = almost_equals
39
40
41 -- | Like 'almost_equals', except much more tolerant. The difference
42 -- between the two arguments must be less than one percent of the sum
43 -- of their magnitudes.
44 kinda_equals :: Double -> Double -> Bool
45 kinda_equals x y =
46 (abs (x - y)) < threshold
47 where
48 threshold = ((abs x) + (abs y)) / 100.0
49
50 infix 4 ~~=
51 (~~=) :: Double -> Double -> Bool
52 (~~=) = kinda_equals
53
54
55 -- | x is very positive if it is 'epsilon' greater than zero.
56 very_positive :: Double -> Bool
57 very_positive x = x - epsilon > 0
58
59
60 -- | Takes a list of 'Double' and returns the ones which are not very
61 -- positive.
62 non_very_positive_entries :: [Double] -> [Double]
63 non_very_positive_entries = filter (not . very_positive)