| 1493 | } |
| 1494 | |
| 1495 | func 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 |
| 1518 | func InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool { |