Fix NaN handling in LinearLocation/EdgeIntersection compareTo - #1229
Open
bjornharrtell wants to merge 1 commit into
Open
Fix NaN handling in LinearLocation/EdgeIntersection compareTo#1229bjornharrtell wants to merge 1 commit into
bjornharrtell wants to merge 1 commit into
Conversation
bjornharrtell
force-pushed
the
fix/1228-nan-compareto
branch
5 times, most recently
from
August 31, 2026 17:45
8c31be5 to
d328bf5
Compare
compareTo() used raw </> comparisons on segmentFraction/dist, which are always false for NaN and made compareTo (and thus equals(), added in locationtech#1201) fall through to 0 whenever NaN was compared to any other value on the same segment/index. This broke transitivity of equals() and its consistency with hashCode(). Use Double.compare() instead, matching Double's NaN ordering semantics already used by hashCode().
bjornharrtell
force-pushed
the
fix/1228-nan-compareto
branch
from
August 31, 2026 17:45
d328bf5 to
ff7abe9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1228
LinearLocation.compareTo()andEdgeIntersection.compare()compared the floating-point fields (segmentFraction/dist) with raw</>, which are alwaysfalseforNaN. This madecompareTofall through toreturn 0("equal") whenever aNaNwas compared against any other value on the same segment/index, not just against anotherNaN.Since
equals()for both classes (added in #1201) is defined ascompareTo(o) == 0, this madeequals()non-transitive and inconsistent withhashCode()wheneverNaNwas involved (e.g.segmentFractionisNaNfor zero-length line segments).Fix
Replace the manual
</>comparisons withDouble.compare(), which givesNaNa well-defined, transitive ordering consistent withDouble.hashCode()/Objects.hash()(already used by both classes'hashCode()). No behavior change for non-NaNvalues.Tests
Added regression tests reproducing the transitivity/hashCode issue from the report:
LinearLocationTest.testEqualsHashCodeConsistentWithNaNEdgeIntersectionTest.testEqualsHashCodeConsistentWithNaN