Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,8 @@ func calcRelativeError(expected, actual interface{}) (float64, error) {
return 0, errors.New("actual value must not be NaN")
}

return math.Abs(af-bf) / math.Abs(af), nil
denom := math.Max(math.Abs(af), math.Abs(bf))
return math.Abs(af-bf) / denom, nil
}

// InEpsilon asserts that expected and actual have a relative error less than epsilon
Expand Down
3 changes: 2 additions & 1 deletion assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2459,6 +2459,7 @@ func TestInEpsilon(t *testing.T) {
{uint64(100), uint8(101), 0.01},
{0.1, -0.1, 2},
{0.1, 0, 2},
{0.1, 0.14, 0.4},
{math.NaN(), math.NaN(), 1},
{time.Second, time.Second + time.Millisecond, 0.002},
}
Expand Down Expand Up @@ -4209,4 +4210,4 @@ func TestNotErrorAsWithErrorTooLongToPrint(t *testing.T) {
Contains(t, mockT.errorString(), `
in chain: "long: [0 0 0`)
Contains(t, mockT.errorString(), "<... truncated>")
}
}
Loading