@@ -178,6 +178,86 @@ func tryNotEqual(t *testing.T, failedNow bool, actual, expect any, message ...an
178178 )
179179}
180180
181+ // FloatEqual tests the equality between actual and expect floating numbers with epsilon. It'll
182+ // set the result to fail if they are not equal, and it doesn't stop the execution.
183+ //
184+ // a := assert.New(t)
185+ // a.FloatEqual(1.0, 1.0, 0.1) // success
186+ // a.FloatEqual(1.0, 1.01, 0.1) // success
187+ // a.FloatEqual(1.0, 1.2, 0.1) // fail
188+ func (a * Assertion ) FloatEqual (actual , expect , epsilon any , message ... any ) error {
189+ a .Helper ()
190+
191+ return tryFloatEqual (a .T , false , actual , expect , epsilon , message ... )
192+ }
193+
194+ // FloatEqualNow tests the equality between actual and expect floating numbers with epsilon, and
195+ // it'll stop the execution if they are not equal.
196+ //
197+ // a := assert.New(t)
198+ // a.FloatEqualNow(1.0, 1.0, 0.1) // success
199+ // a.FloatEqualNow(1.0, 1.01, 0.1) // success
200+ // a.FloatEqualNow(1.0, 1.2, 0.1) // fail and terminate
201+ func (a * Assertion ) FloatEqualNow (actual , expect , epsilon any , message ... any ) error {
202+ a .Helper ()
203+
204+ return tryFloatEqual (a .T , true , actual , expect , epsilon , message ... )
205+ }
206+
207+ // FloatNotEqual tests the inequality between actual and expect floating numbers with epsilon. It'll
208+ // set the result to fail if they are equal, but it doesn't stop the execution.
209+ //
210+ // a := assert.New(t)
211+ // a.FloatNotEqual(1.0, 1.2, 0.1) // success
212+ // a.FloatNotEqual(1.0, 1.1, 0.1) // success
213+ // a.FloatNotEqual(1.0, 1.0, 0.1) // fail
214+ func (a * Assertion ) FloatNotEqual (actual , expect , epsilon any , message ... any ) error {
215+ a .Helper ()
216+
217+ return tryFloatNotEqual (a .T , false , actual , expect , epsilon , message ... )
218+ }
219+
220+ // FloatNotEqualNow tests the inequality between actual and expect floating numbers with epsilon,
221+ // and it'll stop the execution if they are equal.
222+ //
223+ // a := assert.New(t)
224+ // a.FloatNotEqualNow(1.0, 1.2, 0.1) // success
225+ // a.FloatNotEqualNow(1.0, 1.1, 0.1) // success
226+ // a.FloatNotEqualNow(1.0, 1.0, 0.1) // fail and terminate
227+ func (a * Assertion ) FloatNotEqualNow (actual , expect , epsilon any , message ... any ) error {
228+ a .Helper ()
229+
230+ return tryFloatNotEqual (a .T , true , actual , expect , epsilon , message ... )
231+ }
232+
233+ // tryFloatEqual try to testing the equality between actual and expect floating numbers, and it'll
234+ // fail if the values are not equal.
235+ func tryFloatEqual (t * testing.T , failedNow bool , actual , expect , epsilon any , message ... any ) error {
236+ t .Helper ()
237+
238+ return test (
239+ t ,
240+ func () bool { return isFloatEqual (actual , expect , epsilon ) },
241+ failedNow ,
242+ fmt .Sprintf (defaultErrMessageEqual , actual , expect ),
243+ message ... ,
244+ )
245+ }
246+
247+ // tryFloatNotEqual try to testing the inequality between actual and expect floating numbers, and
248+ // it'll fail if the values are equal.
249+ func tryFloatNotEqual (t * testing.T , failedNow bool , actual , expect , epsilon any , message ... any ) error {
250+ t .Helper ()
251+
252+ return test (
253+ t ,
254+ func () bool { return ! isFloatEqual (actual , expect , epsilon ) },
255+ failedNow ,
256+ fmt .Sprintf (defaultErrMessageEqual , actual , expect ),
257+ message ... ,
258+ )
259+ }
260+
181261// Nil tests whether a value is nil or not, and it'll fail when the value is not nil. It will
182262// always return false if the value is a bool, an integer, a floating number, a complex, or a
183263// string.
@@ -413,6 +493,24 @@ func isEqual(x, y any) bool {
413493 }
414494}
415495
496+ // isFloatEqual checks the equality of two floating numbers with epsilon.
497+ func isFloatEqual (x , y , epsilon any ) bool {
498+ xv := toFloat (x )
499+ yv := toFloat (y )
500+ ev := toFloat (epsilon )
501+
502+ if xv == yv {
503+ return true
504+ }
505+
506+ diff := xv - yv
507+ if diff < 0 {
508+ diff = - diff
509+ }
510+
511+ return diff < ev
512+ }
513+
416514// isNil checks whether a value is nil or not. It'll always return false if the value is not a
417515// channel, a function, a map, a point, an unsafe point, an interface, or a slice.
418516func isNil (val any ) bool {
0 commit comments