assert.InEpsilon fails if actual = 0
af, aok := toFloat(expected)
...
if af < bf {
delta = af * epsilon
} else {
delta = bf * epsilon
}
return delta
If expected = 0, then delta = 0
dt := af - bf
if dt < -delta || dt > delta {
return Fail(t, fmt.Sprintf("Delta: %v, Max difference between %v and %v allowed is %v, but difference was %v", delta, expected, actual, delta, dt), msgAndArgs...)
}
Then, this always fail unless the difference itself is 0.
For my case, this is what I got, which clearly should be the same.
Max difference between 0 and 3.3306690738754696e-16 allowed is 0, but difference was -3.3306690738754696e-16
@leeyikjiun do you want to prepare a PR?
Yes, I'll be preparing a PR.
@ernesto-jimenez actually I have no idea why we need to calculate a delta. InEpsilon works perfectly fine by returning InDelta with delta = epsilon. Furthermore, InEpsilon is true when expected = 5, actual = 10, epsilon = 4. delta = min{|expected|, |actual|} * epsilon = 20 which is unexpected behaviour.
@leeyikjiun that behaviour is expected. InEpsilon asserts that the value is within the Epsilon relative error:
expected = 5
actual = 10
absolute error = | 5 - 10 | = 5
relative error = 5 / 5 = 1
Since 1 < 4, assertion is OK
I just discovered we had an InEpsilon assertion, so I'll have to review the issue and the implementation :)
@ernesto-jimenez I didn't know InEpsilon is for relative error; I thought it's used in the real analysis sense that |a-b| < epsilon where epsilon is a very small number. I should really be using InDelta instead. My bad.
In your example, shouldn't relative error be 5 / 5 = 1 instead? From Wikipedia, it says The relative error is the absolute error divided by the magnitude of the exact value. In this method, I suppose expected = exact and actual = approximation?
Yes, sorry, you are right, I should stop doing two things at once ^_^
I edited in case somebody drops by here later :)
@ernesto-jimenez I think in this case we can close this issue because expected cannot be 0 as it would result in an undefined relative error. It's a false alarm, my bad.
@leeyikjiun there's still a problem when actual value is zero:
Example:
expected = 0.1
actual = 0
absolute error = 0.1
relative error = 0.1 / 0.1 = 1
However, this fails: InEpsilon(t, 0.1, 0, 2)
I'm going to rename the issue and leave it open.
We should:
- [x] Fix false failure when actual is zero
- [x] Panic when expected is zero? Unless there's a way to calculate a relative error with an expected value of zero.
- [x] Compare relative errors rather than using
InDeltaso we can - [x] Review
InEpsilonSlice
@leeyikjiun this is fixed now in master.