diff --git a/prometheus/histogram_test.go b/prometheus/histogram_test.go index e0999a92f..4d514fb16 100644 --- a/prometheus/histogram_test.go +++ b/prometheus/histogram_test.go @@ -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) { + t.Errorf("exponential buckets range: got %v, want %v (tolerance %f)", got, want, tolerance) } } @@ -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 +}