Skip to content

Commit 6f1f3b2

Browse files
tniessenzbjornson
authored andcommitted
Avoid propagating rounding errors in linearBuckets
1 parent dbe0aad commit 6f1f3b2

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Breaking
1111

12+
- changed: `linearBuckets` does not propagate rounding errors anymore.
13+
14+
Fewer bucket bounds will be affected by rounding errors. Histogram bucket
15+
labels may change.
16+
1217
### Changed
1318

1419
### Added

lib/bucketGenerators.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ exports.linearBuckets = (start, width, count) => {
77

88
const buckets = new Array(count);
99
for (let i = 0; i < count; i++) {
10-
buckets[i] = start;
11-
start += width;
10+
buckets[i] = start + i * width;
1211
}
1312
return buckets;
1413
};

test/bucketGeneratorsTest.js

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ describe('bucketGenerators', () => {
2727
};
2828
expect(fn).toThrowError(Error);
2929
});
30+
31+
it('should not propagate rounding errors', () => {
32+
result = linearBuckets(0.1, 0.1, 10);
33+
expect(result[9]).toEqual(1);
34+
});
3035
});
3136

3237
describe('exponential buckets', () => {

0 commit comments

Comments
 (0)