| 1459 | } |
| 1460 | |
| 1461 | func calcRelativeError(expected, actual interface{}) (float64, error) { |
| 1462 | af, aok := toFloat(expected) |
| 1463 | bf, bok := toFloat(actual) |
| 1464 | if !aok || !bok { |
| 1465 | return 0, fmt.Errorf("Parameters must be numerical") |
| 1466 | } |
| 1467 | if math.IsNaN(af) && math.IsNaN(bf) { |
| 1468 | return 0, nil |
| 1469 | } |
| 1470 | if math.IsNaN(af) { |
| 1471 | return 0, errors.New("expected value must not be NaN") |
| 1472 | } |
| 1473 | if af == 0 { |
| 1474 | return 0, fmt.Errorf("expected value must have a value other than zero to calculate the relative error") |
| 1475 | } |
| 1476 | if math.IsNaN(bf) { |
| 1477 | return 0, errors.New("actual value must not be NaN") |
| 1478 | } |
| 1479 | |
| 1480 | return math.Abs(af-bf) / math.Abs(af), nil |
| 1481 | } |
| 1482 | |
| 1483 | // InEpsilon asserts that expected and actual have a relative error less than epsilon |
| 1484 | func InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool { |