-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detect and warn on zero delta/epison values in InDelta
/InEpsilon
#211
Comments
\InDelta
/InEpsilon
InDelta
/InEpsilon
Hi, @oschwald Thank you for request. Why this is issue? If zero tolerance is meaningless, then it should be detected by corresponding test cases (with tolerance ajusting after). Or the issue in detecting of needs to adding such cases? And this will be false positive warning for tests where 0 is ok 🤔 |
The stated reason of the Consider this example: package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAssert(t *testing.T) {
x := -1e9
y := 1e9
z := 1e-9
assert.Equal(t, x+y+z, x+(y+z))
assert.InDelta(t, x+y+z, x+(y+z), 0)
} These both fail as floating point math is not associative. Of the three, I find the message on the
In most cases when doing floating point comparisons, we do not what bitwise equality but want to ensure that the two numbers are within some non-zero delta of each other. This makes the tests less brittle as they are less likely to fail on code refactoring due to meaningless changes in order of the floating point operations. |
I agree that if we report Equal when comparing float We should report require.InDelta(t, expectedValue, actualValue, 0) to be invalid too Now, it's unclear if InDelta with 0 should be reported to be:
|
At my workplace, colleagues are circumventing the
float-compare
rules by using a zero value for the delta or epsilon. This defeats the purpose ofInDelta
andInEpsilon
which are meant for floating-point comparisons with a meaningful tolerance.Example of problematic code
The text was updated successfully, but these errors were encountered: