Skip to content

Commit

Permalink
Merge pull request #53 from wmde/ieee
Browse files Browse the repository at this point in the history
Don't compare floats without epsilon
  • Loading branch information
snaterlicious committed Nov 7, 2014
2 parents 3296640 + bb7d239 commit 4bc3bac
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/globeCoordinate/globeCoordinate.GlobeCoordinate.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ globeCoordinate.GlobeCoordinate = ( function( globeCoordinate ) {
var gc1Iso6709 = globeCoordinate.iso6709( this.getDecimal() ),
gc2Iso6709 = globeCoordinate.iso6709( otherGlobeCoordinate.getDecimal() );

return this.getPrecision() === otherGlobeCoordinate.getPrecision()
return Math.abs( this.getPrecision() - otherGlobeCoordinate.getPrecision() ) < 0.00000001
&& gc1Iso6709 === gc2Iso6709;
}

Expand Down
53 changes: 45 additions & 8 deletions tests/lib/globeCoordinate/globeCoordinate.GlobeCoordinate.tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @licence GNU GPL v2+
* @author H. Snater < [email protected] >
* @author Thiemo Mättig
*/
define( [
'globeCoordinate/globeCoordinate',
Expand Down Expand Up @@ -92,7 +93,7 @@ define( [

} );

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

if(
gcDef1.latitude === gcDef2.latitude
if( gcDef1.latitude === gcDef2.latitude
&& gcDef1.longitude === gcDef2.longitude
&& gcDef1.precision === gcDef2.precision
) {

assert.ok(
c1.equals( c2 ),
'Validated equality for data set #' + i1 + '.'
);

} else {

assert.ok(
!c1.equals( c2 ),
'Validated inequality of data set #' + i1 + ' to #' + i2 + '.'
);

}

} );
} );

} );

QUnit.test( 'Loose equality', function( assert ) {
var gcDefs = [
{ latitude: 0, longitude: 0, precision: 1 },
{ latitude: 0.01, longitude: 0, precision: 1 },
{ latitude: 0.1, longitude: 0, precision: 1 },
{ latitude: 0, longitude: 0.01, precision: 1 },
{ latitude: 0, longitude: 0.1, precision: 1 },
{ latitude: 0, longitude: 0, precision: 1.000000001 },
{ latitude: 0, longitude: 0, precision: 1.00000001 }
],
c1 = new globeCoordinate.GlobeCoordinate( gcDefs[0] );

$.each( gcDefs, function( i2, gcDef2 ) {
var c2 = new globeCoordinate.GlobeCoordinate( gcDef2 );
assert.ok(
c1.equals( c2 ),
'Validated equality of data set #0 to #' + i2 + '.'
);
} );

} );

QUnit.test( 'Loose inequality', function( assert ) {
var c1 = new globeCoordinate.GlobeCoordinate(
{ latitude: 0, longitude: 0, precision: 1 / 3600 }
),
gcDefs = [
{ latitude: 0.0002, longitude: 0, precision: 1 / 3600 },
{ latitude: 0, longitude: 0.0002, precision: 1 / 3600 },
{ latitude: 0, longitude: 0, precision: 1 / 3600 + 0.0000001 },
{ latitude: 0, longitude: 0, precision: 1 / 3600 - 0.0000001 }
];

$.each( gcDefs, function( i2, gcDef2 ) {
var c2 = new globeCoordinate.GlobeCoordinate( gcDef2 );
assert.ok(
!c1.equals( c2 ),
'Validated inequality to data set #' + i2 + '.'
);
} );

} );
Expand Down

0 comments on commit 4bc3bac

Please sign in to comment.