MCPcopy
hub / github.com/stretchr/testify / calcRelativeError

Function calcRelativeError

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

Source from the content-addressed store, hash-verified

1545}
1546
1547func calcRelativeError(expected, actual interface{}) (float64, error) {
1548 af, aok := toFloat(expected)
1549 bf, bok := toFloat(actual)
1550 if !aok || !bok {
1551 return 0, fmt.Errorf("Parameters must be numerical")
1552 }
1553 if math.IsNaN(af) && math.IsNaN(bf) {
1554 return 0, nil
1555 }
1556 if math.IsNaN(af) {
1557 return 0, errors.New("expected value must not be NaN")
1558 }
1559 if af == 0 {
1560 return 0, fmt.Errorf("expected value must have a value other than zero to calculate the relative error")
1561 }
1562 if math.IsNaN(bf) {
1563 return 0, errors.New("actual value must not be NaN")
1564 }
1565
1566 return math.Abs(af-bf) / math.Abs(af), nil
1567}
1568
1569// InEpsilon asserts that expected and actual have a relative error less than epsilon
1570func 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