testify icon indicating copy to clipboard operation
testify copied to clipboard

assert.InEpsilon fails if actual = 0

Open leeyikjiun opened this issue 10 years ago • 9 comments

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 avatar Dec 30 '15 07:12 leeyikjiun

@leeyikjiun do you want to prepare a PR?

ernesto-jimenez avatar Jan 02 '16 10:01 ernesto-jimenez

Yes, I'll be preparing a PR.

leeyikjiun avatar Jan 02 '16 11:01 leeyikjiun

@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 avatar Jan 02 '16 16:01 leeyikjiun

@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 avatar Jan 04 '16 12:01 ernesto-jimenez

@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?

leeyikjiun avatar Jan 05 '16 02:01 leeyikjiun

Yes, sorry, you are right, I should stop doing two things at once ^_^

I edited in case somebody drops by here later :)

ernesto-jimenez avatar Jan 05 '16 10:01 ernesto-jimenez

@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 avatar Jan 07 '16 03:01 leeyikjiun

@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 InDelta so we can
  • [x] Review InEpsilonSlice

ernesto-jimenez avatar Jan 07 '16 15:01 ernesto-jimenez

@leeyikjiun this is fixed now in master.

ernesto-jimenez avatar Jan 09 '16 16:01 ernesto-jimenez