MCPcopy
hub / github.com/google/go-cmp / EquateApprox

Function EquateApprox

cmp/cmpopts/equate.go:50–59  ·  view source on GitHub ↗

EquateApprox returns a [cmp.Comparer] option that determines float32 or float64 values to be equal if they are within a relative fraction or absolute margin. This option is not used when either x or y is NaN or infinite. The fraction determines that the difference of two values must be within the s

(fraction, margin float64)

Source from the content-addressed store, hash-verified

48//
49// EquateApprox can be used in conjunction with [EquateNaNs].
50func EquateApprox(fraction, margin float64) cmp.Option {
51 if margin < 0 || fraction < 0 || math.IsNaN(margin) || math.IsNaN(fraction) {
52 panic("margin or fraction must be a non-negative number")
53 }
54 a := approximator{fraction, margin}
55 return cmp.Options{
56 cmp.FilterValues(areRealF64s, cmp.Comparer(a.compareF64)),
57 cmp.FilterValues(areRealF32s, cmp.Comparer(a.compareF32)),
58 }
59}
60
61type approximator struct{ frac, marg float64 }
62

Callers 1

TestOptionsFunction · 0.85

Calls 2

FilterValuesFunction · 0.92
ComparerFunction · 0.92

Tested by 1

TestOptionsFunction · 0.68