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
This commit is contained in:
Ashod Nakashian 2019-06-29 20:31:28 -04:00 committed by Marco Cecchetti
parent ff07323154
commit db0d017051
2 changed files with 4 additions and 3 deletions

View file

@ -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) {

View file

@ -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();