-
Notifications
You must be signed in to change notification settings - Fork 1
/
epsilon.go
60 lines (49 loc) · 1.3 KB
/
epsilon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package epsilon
import (
"math"
)
var (
// minNormal is the minimum delta between two float64 values. In
// float64 format, 53 bits are reserved for precision (the significand).
// See
// https://en.wikipedia.org/wiki/Double-precision_floating-point_format
// for more information.
minNormal = math.Float64frombits(1 << 52) // 0x0010000000000000
DefaultE = Normal(128)
)
// Normal calculates if two float64 values are very close to one another. This
// is based on the principle that if a ~ b, (a - b) / (a + b) ~ 0.
//
// See https://stackoverflow.com/a/32334103/873865.
func Normal(i float64) E {
return *New(
func(a, b float64) float64 {
n := math.Min(math.Abs(a)+math.Abs(b), math.MaxFloat64)
return i * math.Abs(a-math.Nextafter(a, b)) * n
},
)
}
func Absolute(e float64) E {
return *New(func(a, b float64) float64 { return e })
}
func Relative(t float64) E {
return *New(
func(a, b float64) float64 {
n := math.Min(math.Abs(a)+math.Abs(b), math.MaxFloat64)
return t / n
},
)
}
type f func(a, b float64) float64
type E f
func New(epsilon f) *E {
e := E(epsilon)
return &e
}
func (e E) Within(a float64, b float64) bool {
if a == b {
return true
}
return math.Abs(a-b) < math.Max(minNormal, f(e)(a, b))
}
func Within(a float64, b float64) bool { return DefaultE.Within(a, b) }