MCPcopy Create free account
hub / github.com/stretchr/testify / calcRelativeError

Function calcRelativeError

assert/assertions.go:1495–1515  ·  view source on GitHub ↗
(expected, actual interface{})

Source from the content-addressed store, hash-verified

1493}
1494
1495func calcRelativeError(expected, actual interface{}) (float64, error) {
1496 af, aok := toFloat(expected)
1497 bf, bok := toFloat(actual)
1498 if !aok || !bok {
1499 return 0, fmt.Errorf("Parameters must be numerical")
1500 }
1501 if math.IsNaN(af) && math.IsNaN(bf) {
1502 return 0, nil
1503 }
1504 if math.IsNaN(af) {
1505 return 0, errors.New("expected value must not be NaN")
1506 }
1507 if af == 0 {
1508 return 0, fmt.Errorf("expected value must have a value other than zero to calculate the relative error")
1509 }
1510 if math.IsNaN(bf) {
1511 return 0, errors.New("actual value must not be NaN")
1512 }
1513
1514 return math.Abs(af-bf) / math.Abs(af), nil
1515}
1516
1517// InEpsilon asserts that expected and actual have a relative error less than epsilon
1518func InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool {

Callers 1

InEpsilonFunction · 0.85

Calls 2

toFloatFunction · 0.85
ErrorfMethod · 0.65

Tested by

no test coverage detected