Skip to content

Commit 7e1de8a

Browse files
committed
Remove Approximate in favor of an approximately-equals relation
1 parent e396a9f commit 7e1de8a

File tree

4 files changed

+51
-77
lines changed

4 files changed

+51
-77
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ Functions for working with PureScript's builtin `Number` type.
44
## Example
55

66
``` purs
7-
> 0.1 + 0.2 == 0.3
7+
> 0.1 + 0.2 == 0.3
88
false
99
10-
> import Data.Number.Approximate
11-
12-
> Approximate 0.1 + Approximate 0.2 == Approximate 0.3
10+
> import Data.Number
11+
> 0.1 + 0.2 ≅ 0.3
1312
true
1413
```

src/Data/Number.purs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
module Data.Number
33
( Fraction
44
, eqRelative
5+
, eqApproximate
6+
, (~=)
7+
, (≅)
8+
, neqApproximate
9+
, (≇)
510
, Precision
611
, eqAbsolute
712
) where
@@ -20,19 +25,55 @@ type Fraction = Number
2025
-- | `eqRelative frac (s * x) (s * y)` for a given scale factor `s > 0.0`
2126
-- | (unless one of x, y is exactly `0.0`).
2227
-- |
28+
-- | Note that the relation that `eqRelative frac` induces on `Number` is
29+
-- | not an equivalence relation. It is reflexive and symmetric, but not
30+
-- | transitive.
31+
-- |
2332
-- | Example:
2433
-- | ``` purs
2534
-- | > (eqRelative 0.01) 133.7 133.0
2635
-- | true
2736
-- |
2837
-- | > (eqRelative 0.001) 133.7 133.0
2938
-- | false
39+
-- |
40+
-- | > (eqRelative 0.01) (0.1 + 0.2) 0.3
41+
-- | true
3042
-- | ```
3143
eqRelative Fraction Number Number Boolean
3244
eqRelative fraction 0.0 y = abs y <= fraction
3345
eqRelative fraction x 0.0 = abs x <= fraction
3446
eqRelative fraction x y = abs (x - y) <= fraction * abs (x + y) / 2.0
3547

48+
-- | Test if two numbers are approximately equal, up to a relative difference
49+
-- | of one part in a million:
50+
-- | ``` purs
51+
-- | eqApproximate = eqRelative 1.0e-6
52+
-- | ```
53+
-- |
54+
-- | Example
55+
-- | ``` purs
56+
-- | > 0.1 + 0.2 == 0.3
57+
-- | false
58+
-- |
59+
-- | > 0.1 + 0.2 ≅ 0.3
60+
-- | true
61+
-- | ```
62+
eqApproximate Number Number Boolean
63+
eqApproximate = eqRelative onePPM
64+
where
65+
onePPM Fraction
66+
onePPM = 1.0e-6
67+
68+
infix 4 eqApproximate as ~=
69+
infix 4 eqApproximate as
70+
71+
-- | The complement of `eqApproximate`.
72+
neqApproximate Number Number Boolean
73+
neqApproximate x y = not (x ≅ y)
74+
75+
infix 4 neqApproximate as
76+
3677
-- | A(nother) type alias for (small) numbers.
3778
type Precision = Number
3879

src/Data/Number/Approximate.purs

Lines changed: 0 additions & 56 deletions
This file was deleted.

test/Main.purs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ module Test.Main where
22

33
import Prelude
44

5-
import Data.Number (eqRelative, eqAbsolute)
6-
import Data.Number.Approximate (Approximate(..))
5+
import Data.Number (eqRelative, eqAbsolute, (≅), (≇))
76

87
import Control.Monad.Aff.AVar (AVAR)
98
import Control.Monad.Eff (Eff)
@@ -111,23 +110,14 @@ main = runTest do
111110
eqRelative 3.0 10.0 29.5
112111

113112

114-
suite "Approximate" do
115-
let x = Approximate 0.3
116-
y = Approximate 0.2999999999
117-
a = Approximate 0.1
118-
b = Approximate 0.2
113+
suite "eqApproximate" do
114+
test "0.1 + 0.2 ≅ 0.3" do
115+
assert "0.1 + 0.2 should be approximately equal to 0.3" $
116+
0.1 + 0.20.3
119117

120-
test "Eq instance" do
121-
assert "should treat them as equal" $
122-
x == y
118+
assert "0.1 + 0.200001 should not be approximately equal to 0.3" $
119+
0.1 + 0.2000010.3
123120

124-
test "Ord instance" do
125-
assert "should also treat them as equal" $
126-
x >= y && y >= x
127-
128-
test "Addition" do
129-
assert "0.1 + 0.2 should be equal to 0.3 (with Approximate)" $
130-
a + b == x
131121

132122
suite "eqAbsolute" do
133123
test "eqAbsolute" do

0 commit comments

Comments
 (0)