Round brush coordinates to 14 digits. Fixes #1634

This commit is contained in:
Winston Chang
2017-03-30 15:44:18 -05:00
parent 6993551a44
commit 7492db592b
2 changed files with 14 additions and 0 deletions

View File

@@ -1119,6 +1119,9 @@ imageutils.createBrush = function($el, opts, coordmap, expandPixels) {
// For reversed scales, the min and max can be reversed, so use findBox
// to ensure correct order.
state.boundsData = coordmap.findBox(minData, maxData);
// Round to 14 significant digits to avoid spurious changes in FP values
// (#1634).
state.boundsData = mapValues(state.boundsData, val => roundSignif(val, 14));
// We also need to attach the data bounds and panel as data attributes, so
// that if the image is re-sent, we can grab the data bounds to create a new

View File

@@ -56,6 +56,17 @@ function padZeros(n, digits) {
return str;
}
// Round to a specified number of significant digits.
function roundSignif(x, digits = 1) {
if (digits < 1)
throw "Significant digits must be at least 1.";
// This converts to a string and back to a number, which is inelegant, but
// is less prone to FP rounding error than an alternate method which used
// Math.round().
return parseFloat(x.toPrecision(digits));
}
// Take a string with format "YYYY-MM-DD" and return a Date object.
// IE8 and QTWebKit don't support YYYY-MM-DD, but they support YYYY/MM/DD
function parseDate(dateString) {