Skip to content

Commit 7492db5

Browse files
committed
Round brush coordinates to 14 digits. Fixes rstudio#1634
1 parent 6993551 commit 7492db5

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

srcjs/output_binding_image.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,9 @@ imageutils.createBrush = function($el, opts, coordmap, expandPixels) {
11191119
// For reversed scales, the min and max can be reversed, so use findBox
11201120
// to ensure correct order.
11211121
state.boundsData = coordmap.findBox(minData, maxData);
1122+
// Round to 14 significant digits to avoid spurious changes in FP values
1123+
// (#1634).
1124+
state.boundsData = mapValues(state.boundsData, val => roundSignif(val, 14));
11221125

11231126
// We also need to attach the data bounds and panel as data attributes, so
11241127
// that if the image is re-sent, we can grab the data bounds to create a new

srcjs/utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ function padZeros(n, digits) {
5656
return str;
5757
}
5858

59+
// Round to a specified number of significant digits.
60+
function roundSignif(x, digits = 1) {
61+
if (digits < 1)
62+
throw "Significant digits must be at least 1.";
63+
64+
// This converts to a string and back to a number, which is inelegant, but
65+
// is less prone to FP rounding error than an alternate method which used
66+
// Math.round().
67+
return parseFloat(x.toPrecision(digits));
68+
}
69+
5970
// Take a string with format "YYYY-MM-DD" and return a Date object.
6071
// IE8 and QTWebKit don't support YYYY-MM-DD, but they support YYYY/MM/DD
6172
function parseDate(dateString) {

0 commit comments

Comments
 (0)