Skip to content

Commit bec361e

Browse files
committed
add named constants for floating-point comparison epsilons
1 parent f2a76cc commit bec361e

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

tests/binary_double_test.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ import (
4444
"testing"
4545
)
4646

47+
const (
48+
// doubleComparisonEpsilon is used for direct value comparison
49+
doubleComparisonEpsilon = 1e-15
50+
// doubleAggregateEpsilon is used for aggregate operations (SUM, AVG, etc.)
51+
doubleAggregateEpsilon = 1e-10
52+
)
53+
4754
type BinaryDoubleTest struct {
4855
ID uint `gorm:"column:ID;primaryKey"`
4956
DoubleValue float64 `gorm:"column:DOUBLE_VALUE;type:BINARY_DOUBLE"`
@@ -74,7 +81,7 @@ func TestBinaryDoubleBasicCRUD(t *testing.T) {
7481
t.Fatalf("fetch failed: %v", err)
7582
}
7683

77-
if math.Abs(got.DoubleValue - testValue) > 1e-15 {
84+
if math.Abs(got.DoubleValue - testValue) > doubleComparisonEpsilon {
7885
t.Errorf("expected %v, got %v", testValue, got.DoubleValue)
7986
}
8087

@@ -89,7 +96,7 @@ func TestBinaryDoubleBasicCRUD(t *testing.T) {
8996
if err := DB.First(&updated, bd1.ID).Error; err != nil {
9097
t.Fatalf("fetch after update failed: %v", err)
9198
}
92-
if math.Abs(updated.DoubleValue - newValue) > 1e-15 {
99+
if math.Abs(updated.DoubleValue - newValue) > doubleComparisonEpsilon {
93100
t.Errorf("expected %v after update, got %v", newValue, updated.DoubleValue)
94101
}
95102

@@ -153,7 +160,7 @@ func TestBinaryDoubleSpecialValues(t *testing.T) {
153160
t.Errorf("expected 0, got %v", got.DoubleValue)
154161
}
155162
} else {
156-
if math.Abs(got.DoubleValue - tc.value) > 1e-15 {
163+
if math.Abs(got.DoubleValue - tc.value) > doubleComparisonEpsilon {
157164
t.Errorf("expected %v, got %v", tc.value, got.DoubleValue)
158165
}
159166
}
@@ -198,7 +205,7 @@ func TestBinaryDoubleNullableColumn(t *testing.T) {
198205
}
199206
if got2.NullableDouble == nil {
200207
t.Error("expected non-NULL value")
201-
} else if math.Abs(*got2.NullableDouble - val) > 1e-15 {
208+
} else if math.Abs(*got2.NullableDouble - val) > doubleComparisonEpsilon {
202209
t.Errorf("expected %v, got %v", val, *got2.NullableDouble)
203210
}
204211

@@ -236,7 +243,7 @@ func TestBinaryDoubleSQLNullFloat(t *testing.T) {
236243
if !got1.SQLNullFloat.Valid {
237244
t.Error("expected Valid to be true")
238245
}
239-
if math.Abs(got1.SQLNullFloat.Float64 - 123.456) > 1e-15 {
246+
if math.Abs(got1.SQLNullFloat.Float64 - 123.456) > doubleComparisonEpsilon {
240247
t.Errorf("expected 123.456, got %v", got1.SQLNullFloat.Float64)
241248
}
242249

@@ -277,7 +284,7 @@ func TestBinaryDoubleArithmeticOperations(t *testing.T) {
277284
t.Fatalf("failed to calculate SUM: %v", err)
278285
}
279286
expectedSum := 10.5 + 20.3 + 30.7 + 40.1 + 50.9
280-
if math.Abs(sum - expectedSum) > 1e-10 {
287+
if math.Abs(sum - expectedSum) > doubleAggregateEpsilon {
281288
t.Errorf("expected sum %v, got %v", expectedSum, sum)
282289
}
283290

@@ -287,7 +294,7 @@ func TestBinaryDoubleArithmeticOperations(t *testing.T) {
287294
t.Fatalf("failed to calculate AVG: %v", err)
288295
}
289296
expectedAvg := expectedSum / 5
290-
if math.Abs(avg - expectedAvg) > 1e-10 {
297+
if math.Abs(avg - expectedAvg) > doubleAggregateEpsilon {
291298
t.Errorf("expected avg %v, got %v", expectedAvg, avg)
292299
}
293300

@@ -299,10 +306,10 @@ func TestBinaryDoubleArithmeticOperations(t *testing.T) {
299306
if err := DB.Model(&BinaryDoubleTest{}).Select("MAX(DOUBLE_VALUE)").Scan(&max).Error; err != nil {
300307
t.Fatalf("failed to calculate MAX: %v", err)
301308
}
302-
if math.Abs(min - 10.5) > 1e-10 {
309+
if math.Abs(min - 10.5) > doubleAggregateEpsilon {
303310
t.Errorf("expected min 10.5, got %v", min)
304311
}
305-
if math.Abs(max - 50.9) > 1e-10 {
312+
if math.Abs(max - 50.9) > doubleAggregateEpsilon {
306313
t.Errorf("expected max 50.9, got %v", max)
307314
}
308315
}

0 commit comments

Comments
 (0)