Copyright | (c) 2020 Kishaloy Neogi |
---|---|
License | MIT |
Maintainer | Kishaloy Neogi |
Safe Haskell | None |
Language | Haskell2010 |
The library is created to allow for a easy-to-use reasonable way of emulating approx in Haskell. The codes are all in pure Haskell. The idea is to have a natural mathematical feel in writing code, with operators which just works when working with Double and Float and their composite types like lists, vectors etc.
The Approx module defines 2 operators =~
and /~
, which are for checking nearly equal to and not nearly equal to respectively.
Both the operators =~ and /~ are put under the class Approx.
At least one of the operators have to be defined and the other gets automatically defined.
The library already defines the functions for some of the basic / common types.
For types where Eq is defined like Char, Bool, Int, Day, Text the approx is simply replaced with ==.
For Float and Double, the following formula is used,
if max ( |x|, |y| ) < epsilon_Zero then True else if |x - y| / max ( |x|, |y| ) < epsilon_Eq then True else False
The motivation for defining Approx for classes for which Eq is also defined is to allow for composite types where both Eq and Approx would be present. For example, the following code evaluates to True
, even though the tuple is of type (Int,Double,Text,[Int],[[Char]],[Double])
.
((2,5.35,"happ",[1,2],["ret","we"],[6.78,3.5]) :: (Int,Double,Text,[Int],[[Char]],[Double])) =~ (2,5.35,"happ",[1,2],["ret","we"],[6.78,3.5])
For UTCTime, the approx operator checks for equality to the nearest minute. The following expression evaluates to True
.
(parseTimeM True defaultTimeLocale "%Y-%m-%d %H:%M:%S" "2020-01-15 15:02:15" :: Maybe UTCTime) =~ parseTimeM True defaultTimeLocale "%Y-%m-%d %H:%M:%S" "2020-01-15 15:01:50"
The library also provides approx for Complex and common structures like List, Boxed and Unboxed Vector, Hashmap, Tuples and Maybe. For all lists, tuples, hashmaps and vectors, the approximation is checked right down to the elements and the order for lists and vectors are important.
For lists, only finite lists are supported. Any use of infinite lists would cause a runtime error.
There are addtional functions inRange, safeInRange and inTol, which checks for values within Ranges either explictily defined as in inRange and safeInRange or through tolerances as in inTol.
You may see the github repository at https://github.com/n-kishaloy/approx
How to use this library
Add approx
to build-depends and import Data.Approx
Documentation
The class Approx
defines 2 operators =~
and /~
, which are for checking nearly equal to and not nearly equal to respectively.
Instances
inRange :: Ord a => (a, a) -> a -> Bool infix 4 Source #
inRange (u,v) x = check if x is inside the range (u,v)
Note: The function assumes u < v
. This is done to ensure speed of operations. Use safeInRange, otherwise.
safeInRange :: Ord a => (a, a) -> a -> Bool infix 4 Source #
safeInRange (u,v) x = check if x is inside the range (u,v)
Note: The function works even if u>v. However, it has addtional checks and is more expensive. Use only if you are not sure that u < v for your use-case.