Skip to content

Commit 4bc3bac

Browse files
committed
Merge pull request #53 from wmde/ieee
Don't compare floats without epsilon
2 parents 3296640 + bb7d239 commit 4bc3bac

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

lib/globeCoordinate/globeCoordinate.GlobeCoordinate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ globeCoordinate.GlobeCoordinate = ( function( globeCoordinate ) {
145145
var gc1Iso6709 = globeCoordinate.iso6709( this.getDecimal() ),
146146
gc2Iso6709 = globeCoordinate.iso6709( otherGlobeCoordinate.getDecimal() );
147147

148-
return this.getPrecision() === otherGlobeCoordinate.getPrecision()
148+
return Math.abs( this.getPrecision() - otherGlobeCoordinate.getPrecision() ) < 0.00000001
149149
&& gc1Iso6709 === gc2Iso6709;
150150
}
151151

tests/lib/globeCoordinate/globeCoordinate.GlobeCoordinate.tests.js

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @licence GNU GPL v2+
33
* @author H. Snater < mediawiki@snater.com >
4+
* @author Thiemo Mättig
45
*/
56
define( [
67
'globeCoordinate/globeCoordinate',
@@ -92,7 +93,7 @@ define( [
9293

9394
} );
9495

95-
QUnit.test( 'equals()', function( assert ) {
96+
QUnit.test( 'Strict (in)equality', function( assert ) {
9697
var gcDefs = [
9798
{ latitude: 0, longitude: 0, precision: 1 },
9899
{ latitude: -3, longitude: 2, precision: 1 },
@@ -114,28 +115,64 @@ define( [
114115
$.each( gcDefs, function( i2, gcDef2 ) {
115116
c2 = new globeCoordinate.GlobeCoordinate( gcDef2 );
116117

117-
if(
118-
gcDef1.latitude === gcDef2.latitude
118+
if( gcDef1.latitude === gcDef2.latitude
119119
&& gcDef1.longitude === gcDef2.longitude
120120
&& gcDef1.precision === gcDef2.precision
121121
) {
122-
123122
assert.ok(
124123
c1.equals( c2 ),
125124
'Validated equality for data set #' + i1 + '.'
126125
);
127-
128126
} else {
129-
130127
assert.ok(
131128
!c1.equals( c2 ),
132129
'Validated inequality of data set #' + i1 + ' to #' + i2 + '.'
133130
);
134-
135131
}
136-
137132
} );
133+
} );
134+
135+
} );
136+
137+
QUnit.test( 'Loose equality', function( assert ) {
138+
var gcDefs = [
139+
{ latitude: 0, longitude: 0, precision: 1 },
140+
{ latitude: 0.01, longitude: 0, precision: 1 },
141+
{ latitude: 0.1, longitude: 0, precision: 1 },
142+
{ latitude: 0, longitude: 0.01, precision: 1 },
143+
{ latitude: 0, longitude: 0.1, precision: 1 },
144+
{ latitude: 0, longitude: 0, precision: 1.000000001 },
145+
{ latitude: 0, longitude: 0, precision: 1.00000001 }
146+
],
147+
c1 = new globeCoordinate.GlobeCoordinate( gcDefs[0] );
148+
149+
$.each( gcDefs, function( i2, gcDef2 ) {
150+
var c2 = new globeCoordinate.GlobeCoordinate( gcDef2 );
151+
assert.ok(
152+
c1.equals( c2 ),
153+
'Validated equality of data set #0 to #' + i2 + '.'
154+
);
155+
} );
156+
157+
} );
138158

159+
QUnit.test( 'Loose inequality', function( assert ) {
160+
var c1 = new globeCoordinate.GlobeCoordinate(
161+
{ latitude: 0, longitude: 0, precision: 1 / 3600 }
162+
),
163+
gcDefs = [
164+
{ latitude: 0.0002, longitude: 0, precision: 1 / 3600 },
165+
{ latitude: 0, longitude: 0.0002, precision: 1 / 3600 },
166+
{ latitude: 0, longitude: 0, precision: 1 / 3600 + 0.0000001 },
167+
{ latitude: 0, longitude: 0, precision: 1 / 3600 - 0.0000001 }
168+
];
169+
170+
$.each( gcDefs, function( i2, gcDef2 ) {
171+
var c2 = new globeCoordinate.GlobeCoordinate( gcDef2 );
172+
assert.ok(
173+
!c1.equals( c2 ),
174+
'Validated inequality to data set #' + i2 + '.'
175+
);
139176
} );
140177

141178
} );

0 commit comments

Comments
 (0)