Skip to content

Commit

Permalink
Round brush coordinates to 14 digits. Fixes #1634
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed Mar 30, 2017
1 parent 6993551 commit 67d0529
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions srcjs/output_binding_image.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => roundPrecision(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
Expand Down
16 changes: 16 additions & 0 deletions srcjs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ function padZeros(n, digits) {
return str;
}

// Round to a specified number of digits after the decimal. 0 means round to
// integer value. Negative values mean round to digits before decimal.
function roundDigits(x, digits = 0) {
const n = Math.pow(10, digits);
return Math.round(x * n) / n;
}

// Round to a specified number of significant digits.
function roundPrecision(x, digits = 1) {
if (digits < 1)
throw "Significant digits must be 1 or greater";

const scaleFactor = Math.pow(10, Math.floor(Math.log10(Math.abs(x))) + 1);
return roundDigits(x / scaleFactor, digits) * scaleFactor;
}

// 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) {
Expand Down

0 comments on commit 67d0529

Please sign in to comment.