From db0d0170510e7588552e52daf95c1f2116cbf7ff Mon Sep 17 00:00:00 2001 From: Ashod Nakashian Date: Sat, 29 Jun 2019 20:31:28 -0400 Subject: [PATCH] leaflet: correct floating-point equality comparison The ieee 754 supports multiple representations for the same mathematical value (such as 0 and -0). Worse, because not all numbers can be represented exactly, mathematically identical numbers will not be equal (such as 0.1+0.2 != 0.3). The proper way to compare floating-point numbers is, therefore, to use the epsilon. It was observed through debugging that often we triggered layouting of comments even though their coordinates hadn't changed, because the naive != comparison returned true. Also was observed that when checking for (0,0) coordinates, sometimes our value would be -0, which again the naive == comparison yields false. Change-Id: I246fe02d3db4132bd1d7e7a1761d8a9ae1686fb2 --- loleaflet/src/geometry/Point.js | 5 +++-- loleaflet/src/layer/marker/Annotation.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/loleaflet/src/geometry/Point.js b/loleaflet/src/geometry/Point.js index 1738702d6..47d8fe433 100644 --- a/loleaflet/src/geometry/Point.js +++ b/loleaflet/src/geometry/Point.js @@ -98,8 +98,9 @@ L.Point.prototype = { equals: function (point) { point = L.point(point); - return point.x === this.x && - point.y === this.y; + // Proper ieee 754 equality comparison. + return Math.abs(point.x - this.x) < Number.EPSILON && + Math.abs(point.y - this.y) < Number.EPSILON; }, contains: function (point) { diff --git a/loleaflet/src/layer/marker/Annotation.js b/loleaflet/src/layer/marker/Annotation.js index b021c0f70..173df6b7f 100644 --- a/loleaflet/src/layer/marker/Annotation.js +++ b/loleaflet/src/layer/marker/Annotation.js @@ -62,7 +62,7 @@ L.Annotation = L.Layer.extend({ }, setLatLng: function (latlng, skipCheckBounds) { - if (this._latlng != latlng) { + if (!this._latlng.equals(latlng)) { this._skipCheckBounds = !!skipCheckBounds; this._latlng = latlng; this._updatePosition();