# approx: Easy-to-use emulation of approximate, ranges and tolerances in Haskell

[ library, mit, numeric, program ] [ Propose Tags ]

## Modules

[Index] [Quick Jump]

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

#### Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

• No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1 ChangeLog.md approx, base (>=4.7 && <5), containers (<=0.6.4.1), hashable (<=1.3.0.0), text (<=1.2.4.1), time (<=1.11.1.1), unordered-containers (<=0.2.13.0), vector (<=0.12.2.0) [details] MIT 2020 Kishaloy Neogi Kishaloy Neogi nkishaloy@yahoo.com Revision 1 made by kishaloy at 2021-02-15T19:02:22Z Numeric https://github.com/n-kishaloy/approx#readme https://github.com/n-kishaloy/approx/issues by kishaloy at 2021-02-15T18:48:12Z approx-exe 331 total (3 in the last 30 days) 2.0 (votes: 1) [estimated by Bayesian average] λ λ λ Docs available Last success reported on 2021-02-15

## Readme for approx-0.1.0.1

[back to package description]

# approx

## Motivation

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 feel in writing mathematical 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.

## Features

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.

## Code examples

The following all expressions evaluate as True

(1.0e+7 :: Double) =~ 10000000.05

((1.2, 3.4) :: (Double, Double)) =~ (1.20000008, 3.399999999)
((1.2, 3.5) :: (Double, Double)) /~ (1.20000008, 3.399999999)

([1.2, 3.4, 5.6] :: [Double]) /~ [1.2, 3.4, 5.65]

("star"::Text) =~ ("star"::Text)
("star"::Text) /~ ("start"::Text)

Just (10.00000000000007 :: Double) =~ Just 10.0
Just (10 :: Double) /~ Just 1.0



## Installation

Add the library in build-depends and import Data.Approx.

Kishaloy Neogi