Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix float64 comparison test failure on archs using FMA #1133

Merged
merged 4 commits into from Nov 7, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 25 additions & 6 deletions prometheus/histogram_test.go
Expand Up @@ -355,13 +355,12 @@ func TestBuckets(t *testing.T) {

got = ExponentialBucketsRange(1, 100, 10)
want = []float64{
1.0, 1.6681005372000588, 2.782559402207125,
4.641588833612779, 7.742636826811273, 12.915496650148842,
21.544346900318846, 35.93813663804629, 59.94842503189414,
100.00000000000007,
1.0, 1.6681, 2.7825, 4.6415, 7.7426, 12.9154, 21.5443,
35.9381, 59.9484, 100.0000,
}
if !reflect.DeepEqual(got, want) {
t.Errorf("exponential buckets range: got %v, want %v", got, want)
const tolerance = 0.0001
if !equalFloat64s(got, want, 0.0001) {
sbunce marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("exponential buckets range: got %v, want %v (tolerance %f)", got, want, tolerance)
}
}

Expand Down Expand Up @@ -467,3 +466,23 @@ func TestHistogramExemplar(t *testing.T) {
}
}
}

// equalFloat64 returns true if a and b are within the tolerance. We have this
// because float comparison varies on different architectures. For example,
// architectures which do FMA yield slightly different results.
// https://github.com/prometheus/client_golang/pull/899#issuecomment-1244506390
func equalFloat64(a, b, tolerance float64) bool {
return math.Abs(a-b) < tolerance
}

func equalFloat64s(a, b []float64, tolerance float64) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if !equalFloat64(a[i], b[i], tolerance) {
return false
}
}
return true
}