Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
var isNumeric = require('fast-isnumeric');
var loggers = require('./loggers');

// don't trust floating point equality - fraction of bin size to call
// "on the line" and ensure that they go the right way specified by
// linelow
var roundingError = 1e-9;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little surprised that this hadn't come up before (other than the test below that was actually wrong before), and obviously it's a bit questionable exactly what small fraction to put here... but if we just use straight < vs <= etc it's pretty easy to trick findBin on the edges.



/**
* findBin - find the bin for val - note that it can return outside the
Expand All @@ -26,20 +31,21 @@ var loggers = require('./loggers');
exports.findBin = function(val, bins, linelow) {
if(isNumeric(bins.start)) {
return linelow ?
Math.ceil((val - bins.start) / bins.size) - 1 :
Math.floor((val - bins.start) / bins.size);
Math.ceil((val - bins.start) / bins.size - roundingError) - 1 :
Math.floor((val - bins.start) / bins.size + roundingError);
}
else {
var n1 = 0,
n2 = bins.length,
c = 0,
n,
test;
if(bins[bins.length - 1] >= bins[0]) {
var n1 = 0;
var n2 = bins.length;
var c = 0;
var binSize = (n2 > 1) ? (bins[n2 - 1] - bins[0]) / (n2 - 1) : 1;
var n, test;
if(binSize >= 0) {
test = linelow ? lessThan : lessOrEqual;
} else {
test = linelow ? greaterOrEqual : greaterThan;
}
val += binSize * roundingError * (linelow ? -1 : 1) * (binSize >= 0 ? 1 : -1);
// c is just to avoid infinite loops if there's an error
while(n1 < n2 && c++ < 100) {
n = Math.floor((n1 + n2) / 2);
Expand Down
2 changes: 1 addition & 1 deletion test/jasmine/tests/histogram_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ describe('Test histogram', function() {
var trace4 = {x: [1, 1.2, 1.4, 1.6], yaxis: 'y2'};

expect(calcPositions(trace1, [trace2, trace3, trace4])).toEqual([1, 2, 3, 4]);
expect(calcPositions(trace3)).toBeCloseToArray([0.9, 1.1, 1.3], 5);
expect(calcPositions(trace3)).toBeCloseToArray([1.1, 1.3], 5);
});

describe('cumulative distribution functions', function() {
Expand Down