| 1545 | } |
| 1546 | |
| 1547 | func 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 |
| 1570 | func InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool { |