From 6f1f3b24c9c21311ff33e7d4b987b40c6b304e04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Wed, 25 Aug 2021 01:19:35 +0000 Subject: [PATCH] Avoid propagating rounding errors in linearBuckets --- CHANGELOG.md | 5 +++++ lib/bucketGenerators.js | 3 +-- test/bucketGeneratorsTest.js | 5 +++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 864a1a76..b6e3588d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,11 @@ project adheres to [Semantic Versioning](http://semver.org/). ### Breaking +- changed: `linearBuckets` does not propagate rounding errors anymore. + + Fewer bucket bounds will be affected by rounding errors. Histogram bucket + labels may change. + ### Changed ### Added diff --git a/lib/bucketGenerators.js b/lib/bucketGenerators.js index ddd51dfc..a9a487c7 100644 --- a/lib/bucketGenerators.js +++ b/lib/bucketGenerators.js @@ -7,8 +7,7 @@ exports.linearBuckets = (start, width, count) => { const buckets = new Array(count); for (let i = 0; i < count; i++) { - buckets[i] = start; - start += width; + buckets[i] = start + i * width; } return buckets; }; diff --git a/test/bucketGeneratorsTest.js b/test/bucketGeneratorsTest.js index 5f8d5860..898be657 100644 --- a/test/bucketGeneratorsTest.js +++ b/test/bucketGeneratorsTest.js @@ -27,6 +27,11 @@ describe('bucketGenerators', () => { }; expect(fn).toThrowError(Error); }); + + it('should not propagate rounding errors', () => { + result = linearBuckets(0.1, 0.1, 10); + expect(result[9]).toEqual(1); + }); }); describe('exponential buckets', () => {