From d73ee1abad15e6c75dc9ae8eecfd3ce1719d009e Mon Sep 17 00:00:00 2001 From: davi-y08 Date: Thu, 5 Feb 2026 23:11:57 -0300 Subject: [PATCH] fix(assert): stabilize relative error calculation in InEpsilon --- assert/assertions.go | 3 ++- assert/assertions_test.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index 6950636d3..c87361962 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -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 diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 4975f5e41..7eba1c610 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -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}, } @@ -4209,4 +4210,4 @@ func TestNotErrorAsWithErrorTooLongToPrint(t *testing.T) { Contains(t, mockT.errorString(), ` in chain: "long: [0 0 0`) Contains(t, mockT.errorString(), "<... truncated>") -} +} \ No newline at end of file