Skip to content

Commit

Permalink
Fixed rounding errors by recalculating using closer origin
Browse files Browse the repository at this point in the history
  • Loading branch information
craigtaverner authored and Lojjs committed Apr 1, 2019
1 parent bf2c61d commit ee399ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Expand Up @@ -189,8 +189,13 @@ public void mustHandlePointArraysWithinSameTile() throws IndexEntryConflictExcep
exactMatchOnAllValues( pointArrays );
}

/**
* The test mustHandlePointArraysWithinSameTile was flaky on random numbers that placed points just
* within the tile upper bound, and allocated points to adjacent tiles due to rounding errors.
* This test uses a specific point that triggers that exact failure in a non-flaky way.
*/
@Test
public void exactTest()
public void shouldNotGetRoundingErrorsWithPointsJustWithinTheTileUpperBound()
{
PointValue origin = Values.pointValue( WGS84, 0.0, 0.0 );
long derivedValueForCenterPoint = curve.derivedValueFor( origin.coordinate() );
Expand Down
Expand Up @@ -396,7 +396,13 @@ long[] getNormalizedCoord( double[] coord )
}
else
{
normalizedCoord[dim] = (long) ((value - range.getMin( dim )) * scalingFactor[dim]);
normalizedCoord[dim] = (long) ((value - range.getMin(dim)) * scalingFactor[dim]);
// Since we calculated this with an origin at the min extreme, we can get numerical rouding errors, which can be corrected by recalculating using a closer origin
double tileCenter = ((double) normalizedCoord[dim]) / scalingFactor[dim] + range.getMin(dim) + getTileWidth(dim, maxLevel) / 2.0;
// The 1E-16 is to create the behavior of the [min,max) bounds without an expensive if...else if...else check
long normalizedOffset = (long) ((value - tileCenter) * scalingFactor[dim] - 0.5 + 1E-16);
// normalizedOffset is almost always 0, but can be +1 or -1 if there were rounding errors we need to correct for
normalizedCoord[dim] += normalizedOffset;
}
}
return normalizedCoord;
Expand Down

0 comments on commit ee399ba

Please sign in to comment.