go - Difference between InDelta and InEpsilon -
from documentation:
https://godoc.org/github.com/stretchr/testify/assert#indelta
indelta asserts 2 numerals within delta of each other
https://godoc.org/github.com/stretchr/testify/assert#inepsilon
inepsilon asserts expected , actual have relative error less epsilon
and code seems identical in purpose:
func indelta(t testingt, expected, actual interface{}, delta float64, msgandargs ...interface{}) bool { af, aok := tofloat(expected) bf, bok := tofloat(actual) if !aok || !bok { return fail(t, fmt.sprintf("parameters must numerical"), msgandargs...) } if math.isnan(af) { return fail(t, fmt.sprintf("expected must not nan"), msgandargs...) } if math.isnan(bf) { return fail(t, fmt.sprintf("expected %v delta %v, nan", expected, delta), msgandargs...) } dt := af - bf if dt < -delta || dt > delta { return fail(t, fmt.sprintf("max difference between %v , %v allowed %v, difference %v", expected, actual, delta, dt), msgandargs...) } return true }
func calcrelativeerror(expected, actual interface{}) (float64, error) { af, aok := tofloat(expected) if !aok { return 0, fmt.errorf("expected value %q cannot converted float", expected) } if af == 0 { return 0, fmt.errorf("expected value must have value other 0 calculate relative error") } bf, bok := tofloat(actual) if !bok { return 0, fmt.errorf("actual value %q cannot converted float", actual) } return math.abs(af-bf) / math.abs(af), nil } // inepsilon asserts expected , actual have relative error less epsilon // // returns whether assertion successful (true) or not (false). func inepsilon(t testingt, expected, actual interface{}, epsilon float64, msgandargs ...interface{}) bool { actualepsilon, err := calcrelativeerror(expected, actual) if err != nil { return fail(t, err.error(), msgandargs...) } if actualepsilon > epsilon { return fail(t, fmt.sprintf("relative error high: %#v (expected)\n"+ " < %#v (actual)", epsilon, actualepsilon), msgandargs...) } return true }
what difference? use cases 1 used on other , vice versa?
they related not identical.
indelta
receives absolute value , checks difference less value.
inepsilon
receives % of difference acceptable.
the behaviour of indelta
quite straightforward:
indelta(t, 100, 101, 2) // that's ok indelta(t, 100, 103, 2) // fail!
but sometimes, care actual value not far away expected value.
depending on how small or large expected value "not far away" might tricky indelta
.
it might problem use same delta
value number:
indelta(t, 100, 101, 2) // ok, 101 "not far away" 100 indelta(t, 1, 2, 2) // hm, 2 sounds "too far away" 1...
if use inepsilon
, can use same %:
inepsilon(t, 100, 101, 0.02) // ok, 102 acceptable inepsilon(t, 1, 2, 0.02) // not pass.. time 1.02 acceptable
in summary, use case Ìnepsilon
discard small differences (and making "small" relative actual values compared).
Comments
Post a Comment