InDelta asserts that the two numerals are within delta of each other.
Delta must be greater than or equal to zero.
Expected and actual values should convert to float64.
To compare large integers that can’t be represented accurately as float64 (e.g. uint64),
prefer InDeltaT to preserve the original type.
Behavior with IEEE floating point arithmetic
expected NaN is matched only by a NaN, e.g. this works: [InDeltaT](math.Sqrt, math.Sqrt, 0.0)
// real-world test would inject *testing.T from TestInDelta(t *testing.T)packagemainimport (
"fmt""testing""github.com/go-openapi/testify/v2/assert")
funcmain() {
t:= new(testing.T) // should come from testing, e.g. func TestInDelta(t *testing.T)success:=assert.InDelta(t, 1.0, 1.01, 0.02)
fmt.Printf("success: %t\n", success)
}
// real-world test would inject *testing.T from TestInDelta(t *testing.T)packagemainimport (
"fmt""testing""github.com/go-openapi/testify/v2/require")
funcmain() {
t:= new(testing.T) // should come from testing, e.g. func TestInDelta(t *testing.T)require.InDelta(t, 1.0, 1.01, 0.02)
fmt.Println("passed")
}
// real-world test would inject *testing.T from TestInDeltaMapValues(t *testing.T)packagemainimport (
"fmt""testing""github.com/go-openapi/testify/v2/assert")
funcmain() {
t:= new(testing.T) // should come from testing, e.g. func TestInDeltaMapValues(t *testing.T)success:=assert.InDeltaMapValues(t, map[string]float64{"a": 1.0}, map[string]float64{"a": 1.01}, 0.02)
fmt.Printf("success: %t\n", success)
}
// real-world test would inject *testing.T from TestInDeltaMapValues(t *testing.T)packagemainimport (
"fmt""testing""github.com/go-openapi/testify/v2/require")
funcmain() {
t:= new(testing.T) // should come from testing, e.g. func TestInDeltaMapValues(t *testing.T)require.InDeltaMapValues(t, map[string]float64{"a": 1.0}, map[string]float64{"a": 1.01}, 0.02)
fmt.Println("passed")
}
// real-world test would inject *testing.T from TestInDeltaSlice(t *testing.T)packagemainimport (
"fmt""testing""github.com/go-openapi/testify/v2/assert")
funcmain() {
t:= new(testing.T) // should come from testing, e.g. func TestInDeltaSlice(t *testing.T)success:=assert.InDeltaSlice(t, []float64{1.0, 2.0}, []float64{1.01, 2.01}, 0.02)
fmt.Printf("success: %t\n", success)
}
// real-world test would inject *testing.T from TestInDeltaSlice(t *testing.T)packagemainimport (
"fmt""testing""github.com/go-openapi/testify/v2/require")
funcmain() {
t:= new(testing.T) // should come from testing, e.g. func TestInDeltaSlice(t *testing.T)require.InDeltaSlice(t, []float64{1.0, 2.0}, []float64{1.01, 2.01}, 0.02)
fmt.Println("passed")
}
// real-world test would inject *testing.T from TestInDeltaT(t *testing.T)packagemainimport (
"fmt""testing""github.com/go-openapi/testify/v2/assert")
funcmain() {
t:= new(testing.T) // should come from testing, e.g. func TestInDeltaT(t *testing.T)success:=assert.InDeltaT(t, 1.0, 1.01, 0.02)
fmt.Printf("success: %t\n", success)
}
// real-world test would inject *testing.T from TestInDeltaT(t *testing.T)packagemainimport (
"fmt""testing""github.com/go-openapi/testify/v2/require")
funcmain() {
t:= new(testing.T) // should come from testing, e.g. func TestInDeltaT(t *testing.T)require.InDeltaT(t, 1.0, 1.01, 0.02)
fmt.Println("passed")
}
// real-world test would inject *testing.T from TestInEpsilon(t *testing.T)packagemainimport (
"fmt""testing""github.com/go-openapi/testify/v2/assert")
funcmain() {
t:= new(testing.T) // should come from testing, e.g. func TestInEpsilon(t *testing.T)success:=assert.InEpsilon(t, 100.0, 101.0, 0.02)
fmt.Printf("success: %t\n", success)
}
// real-world test would inject *testing.T from TestInEpsilon(t *testing.T)packagemainimport (
"fmt""testing""github.com/go-openapi/testify/v2/require")
funcmain() {
t:= new(testing.T) // should come from testing, e.g. func TestInEpsilon(t *testing.T)require.InEpsilon(t, 100.0, 101.0, 0.02)
fmt.Println("passed")
}
// real-world test would inject *testing.T from TestInEpsilonSlice(t *testing.T)packagemainimport (
"fmt""testing""github.com/go-openapi/testify/v2/assert")
funcmain() {
t:= new(testing.T) // should come from testing, e.g. func TestInEpsilonSlice(t *testing.T)success:=assert.InEpsilonSlice(t, []float64{100.0, 200.0}, []float64{101.0, 202.0}, 0.02)
fmt.Printf("success: %t\n", success)
}
// real-world test would inject *testing.T from TestInEpsilonSlice(t *testing.T)packagemainimport (
"fmt""testing""github.com/go-openapi/testify/v2/require")
funcmain() {
t:= new(testing.T) // should come from testing, e.g. func TestInEpsilonSlice(t *testing.T)require.InEpsilonSlice(t, []float64{100.0, 200.0}, []float64{101.0, 202.0}, 0.02)
fmt.Println("passed")
}
InEpsilonT asserts that expected and actual have a relative error less than epsilon.
When expected is zero, epsilon is interpreted as an absolute error threshold,
since relative error is mathematically undefined for zero values.
Unlike InDeltaT, which preserves the original type, InEpsilonT converts the expected and actual
numbers to float64, since the relative error doesn’t make sense as an integer.
Behavior with IEEE floating point arithmetic
expected NaN is matched only by a NaN, e.g. this works: [InDeltaT](math.NaN, math.Sqrt, 0.0)
expected +Inf is matched only by a +Inf
expected -Inf is matched only by a -Inf
Edge case: for very large integers that do not convert accurately to a float64 (e.g. uint64), prefer InDeltaT.
Formula:
If expected == 0: fail if |actual - expected| > epsilon
If expected != 0: fail if |actual - expected| > epsilon * |expected|
This allows InEpsilonT to work naturally across the full numeric range including zero.
// real-world test would inject *testing.T from TestInEpsilonT(t *testing.T)packagemainimport (
"fmt""testing""github.com/go-openapi/testify/v2/assert")
funcmain() {
t:= new(testing.T) // should come from testing, e.g. func TestInEpsilonT(t *testing.T)success:=assert.InEpsilonT(t, 100.0, 101.0, 0.02)
fmt.Printf("success: %t\n", success)
}
// real-world test would inject *testing.T from TestInEpsilonT(t *testing.T)packagemainimport (
"fmt""testing""github.com/go-openapi/testify/v2/require")
funcmain() {
t:= new(testing.T) // should come from testing, e.g. func TestInEpsilonT(t *testing.T)require.InEpsilonT(t, 100.0, 101.0, 0.02)
fmt.Println("passed")
}