Skip to content

Commit 65f591b

Browse files
craig[bot]michae2
andcommitted
107490: testutils/floatcmp: revert to base-10 normalization in FloatsMatch r=rharding6373 a=michae2 In cockroachdb#106552 we tried changing float normalization to use base 2 instead of base 10 (in other words, to use `math.Frexp` instead of our hand-rolled `normalize`). This appears to have broken a logic test, so revert back to the pre-existing base 10 normalization. Fixes: cockroachdb#107461 Release note: None Co-authored-by: Michael Erickson <[email protected]>
2 parents acc75f4 + aff21b6 commit 65f591b

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

pkg/testutils/floatcmp/floatcmp.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,23 @@ func FloatsMatch(expectedString, actualString string) (bool, error) {
121121
actual = math.Abs(actual)
122122
// Check that 15 significant digits match. We do so by normalizing the
123123
// numbers and then checking one digit at a time.
124+
//
125+
// normalize converts f to base * 10**power representation where base is in
126+
// [1.0, 10.0) range.
127+
normalize := func(f float64) (base float64, power int) {
128+
for f >= 10 {
129+
f = f / 10
130+
power++
131+
}
132+
for f < 1 {
133+
f *= 10
134+
power--
135+
}
136+
return f, power
137+
}
124138
var expPower, actPower int
125-
expected, expPower = math.Frexp(expected)
126-
actual, actPower = math.Frexp(actual)
139+
expected, expPower = normalize(expected)
140+
actual, actPower = normalize(actual)
127141
if expPower != actPower {
128142
return false, nil
129143
}

pkg/testutils/floatcmp/floatcmp_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ func TestFloatsMatch(t *testing.T) {
178178
{f1: "0.1234567890123456", f2: "0.1234567890123457", match: true},
179179
{f1: "-0.1234567890123456", f2: "0.1234567890123456", match: false},
180180
{f1: "-0.1234567890123456", f2: "-0.1234567890123455", match: true},
181+
{f1: "0.142857142857143", f2: "0.14285714285714285", match: true},
181182
} {
182183
match, err := FloatsMatch(tc.f1, tc.f2)
183184
if err != nil {

0 commit comments

Comments
 (0)