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)
| 48 | // |
| 49 | // EquateApprox can be used in conjunction with [EquateNaNs]. |
| 50 | func 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 | |
| 61 | type approximator struct{ frac, marg float64 } |
| 62 |