approx-0.1.0.1: Easy-to-use reasonable way of emulating approximate in Haskell.
Copyright(c) 2020 Kishaloy Neogi
LicenseMIT
MaintainerKishaloy Neogi
Safe HaskellNone
LanguageHaskell2010

Data.Approx

Description

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

Synopsis

How to use this library

Add approx to build-depends and import Data.Approx

Documentation

class Approx a where Source #

The class Approx defines 2 operators =~ and /~, which are for checking nearly equal to and not nearly equal to respectively.

Minimal complete definition

(=~) | (/~)

Methods

(=~) :: a -> a -> Bool infix 4 Source #

(/~) :: a -> a -> Bool infix 4 Source #

Instances

Instances details
Approx Bool Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: Bool -> Bool -> Bool Source #

(/~) :: Bool -> Bool -> Bool Source #

Approx Char Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: Char -> Char -> Bool Source #

(/~) :: Char -> Char -> Bool Source #

Approx Double Source # 
Instance details

Defined in Data.Approx

Approx Float Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: Float -> Float -> Bool Source #

(/~) :: Float -> Float -> Bool Source #

Approx Int Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: Int -> Int -> Bool Source #

(/~) :: Int -> Int -> Bool Source #

Approx Integer Source # 
Instance details

Defined in Data.Approx

Approx Text Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: Text -> Text -> Bool Source #

(/~) :: Text -> Text -> Bool Source #

Approx UTCTime Source # 
Instance details

Defined in Data.Approx

Approx Day Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: Day -> Day -> Bool Source #

(/~) :: Day -> Day -> Bool Source #

Approx a => Approx [a] Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: [a] -> [a] -> Bool Source #

(/~) :: [a] -> [a] -> Bool Source #

Approx a => Approx (Maybe a) Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: Maybe a -> Maybe a -> Bool Source #

(/~) :: Maybe a -> Maybe a -> Bool Source #

Approx a => Approx (Complex a) Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: Complex a -> Complex a -> Bool Source #

(/~) :: Complex a -> Complex a -> Bool Source #

(Unbox a, Approx a) => Approx (Vector a) Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: Vector a -> Vector a -> Bool Source #

(/~) :: Vector a -> Vector a -> Bool Source #

Approx a => Approx (Vector a) Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: Vector a -> Vector a -> Bool Source #

(/~) :: Vector a -> Vector a -> Bool Source #

(Approx a, Approx b) => Approx (a, b) Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: (a, b) -> (a, b) -> Bool Source #

(/~) :: (a, b) -> (a, b) -> Bool Source #

(Eq a, Hashable a, Approx b) => Approx (HashMap a b) Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: HashMap a b -> HashMap a b -> Bool Source #

(/~) :: HashMap a b -> HashMap a b -> Bool Source #

(Approx a, Approx b, Approx c) => Approx (a, b, c) Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: (a, b, c) -> (a, b, c) -> Bool Source #

(/~) :: (a, b, c) -> (a, b, c) -> Bool Source #

(Approx a, Approx b, Approx c, Approx d) => Approx (a, b, c, d) Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: (a, b, c, d) -> (a, b, c, d) -> Bool Source #

(/~) :: (a, b, c, d) -> (a, b, c, d) -> Bool Source #

(Approx a, Approx b, Approx c, Approx d, Approx e) => Approx (a, b, c, d, e) Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: (a, b, c, d, e) -> (a, b, c, d, e) -> Bool Source #

(/~) :: (a, b, c, d, e) -> (a, b, c, d, e) -> Bool Source #

(Approx a, Approx b, Approx c, Approx d, Approx e, Approx f) => Approx (a, b, c, d, e, f) Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: (a, b, c, d, e, f) -> (a, b, c, d, e, f) -> Bool Source #

(/~) :: (a, b, c, d, e, f) -> (a, b, c, d, e, f) -> Bool Source #

(Approx a, Approx b, Approx c, Approx d, Approx e, Approx f, Approx g) => Approx (a, b, c, d, e, f, g) Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: (a, b, c, d, e, f, g) -> (a, b, c, d, e, f, g) -> Bool Source #

(/~) :: (a, b, c, d, e, f, g) -> (a, b, c, d, e, f, g) -> Bool Source #

(Approx a, Approx b, Approx c, Approx d, Approx e, Approx f, Approx g, Approx h) => Approx (a, b, c, d, e, f, g, h) Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: (a, b, c, d, e, f, g, h) -> (a, b, c, d, e, f, g, h) -> Bool Source #

(/~) :: (a, b, c, d, e, f, g, h) -> (a, b, c, d, e, f, g, h) -> Bool Source #

(Approx a, Approx b, Approx c, Approx d, Approx e, Approx f, Approx g, Approx h, Approx i) => Approx (a, b, c, d, e, f, g, h, i) Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: (a, b, c, d, e, f, g, h, i) -> (a, b, c, d, e, f, g, h, i) -> Bool Source #

(/~) :: (a, b, c, d, e, f, g, h, i) -> (a, b, c, d, e, f, g, h, i) -> Bool Source #

(Approx a, Approx b, Approx c, Approx d, Approx e, Approx f, Approx g, Approx h, Approx i, Approx j) => Approx (a, b, c, d, e, f, g, h, i, j) Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: (a, b, c, d, e, f, g, h, i, j) -> (a, b, c, d, e, f, g, h, i, j) -> Bool Source #

(/~) :: (a, b, c, d, e, f, g, h, i, j) -> (a, b, c, d, e, f, g, h, i, j) -> Bool Source #

(Approx a, Approx b, Approx c, Approx d, Approx e, Approx f, Approx g, Approx h, Approx i, Approx j, Approx k) => Approx (a, b, c, d, e, f, g, h, i, j, k) Source # 
Instance details

Defined in Data.Approx

Methods

(=~) :: (a, b, c, d, e, f, g, h, i, j, k) -> (a, b, c, d, e, f, g, h, i, j, k) -> Bool Source #

(/~) :: (a, b, c, d, e, f, g, h, i, j, k) -> (a, b, c, d, e, f, g, h, i, j, k) -> Bool Source #

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.

inTol :: (Num a, Ord a) => a -> a -> a -> Bool infix 4 Source #

inTol t p x = inRange (p - t, p + t) x

The Function checks if x is close to p within a tolerance band t. Please ensure t is positive or there would be incorrect results.