Epsilon definitions for Go
Epsilon definitions for Go.
func Equal(f1, f2, threshold float64) bool {
f3 := math.Abs(f1 - f2)
return mathu.Zero(f3) || f3 <= threshold
}
The epsilon constants as well as all top level package functions are wrapped in an interface if you would prefer to inject them into your code rather than referencing the package directly.
A wrapper type is provided around float64 that integrates with the epsilon constants and testing for zero:
f := mathu.Float(1.23)
if f.IsZero() { /* ... */ }
if f.GteZero() { /* ... */ }
if f.LteZero() { /* ... */ }
if f.Equal(f2, 0.001) { /* ... */ }