Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl approx::UlpsEqual for geo-types #1312

Merged
merged 1 commit into from
Feb 12, 2025
Merged

impl approx::UlpsEqual for geo-types #1312

merged 1 commit into from
Feb 12, 2025

Conversation

michaelkirk
Copy link
Member

@michaelkirk michaelkirk commented Feb 5, 2025

  • I agree to follow the project's code of conduct.
  • I added an entry to CHANGES.md if knowledge of this change could be valuable to users.

I assumed we implemented all the "approximate equality" traits from approx long ago, but we missed one!

As a reminder, an ULPS comparison is effectively counting "how many floating point numbers are between these two numbers". For the most part it solves the same problems as relative_eq, but it's a little better behaved at the extremes, like when the "max_relative" factor you want to use is itself affected by floating point error.

use approx::{AbsDiffEq, RelativeEq, UlpsEq};

impl<T> AbsDiffEq for Coord<T>
where
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the new UlpsEq impl below, these changes are formatting changes.

Though I also simplified/standardized some of our type constraints.

e.g. here T::Epsilon: Copy is replaced by AbsDiffEq<Epsilon=T>

Since T: CoordNum, we already know it's Copy.

In theory, the previous implementation might have allowed something like:

Coord<f32>.abs_diff(Coord<f64>)

But because pub trait AbsDiffEq<Rhs = Self> we only ever supported comparing to Self anyway (and I think that's just fine).

Bottom line, this doesn't change behavior and is more succinct/uniform with the other declarations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW I recommend turning on the option to hide whitespace changes for this diff.


impl<T> RelativeEq for GeometryCollection<T>
where
T: CoordNum + RelativeEq<Epsilon = T>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another no-op simplification in the type constraints: We don't need to specify AbsDiffEq and RelativeEq because RelativeEq: AbsDiffEq, so just RelativeEq is sufficient.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

}

let mut mp_zipper = self.into_iter().zip(other);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this mp_zipper variable was copy/pasted from MultiPolygon or MultiPoint. No biggie, but I got rid of it since it's confusing in the context of GeometryCollection.

@@ -248,78 +246,101 @@ impl<'a, T: CoordNum> GeometryCollection<T> {
}

#[cfg(any(feature = "approx", test))]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than having several conditional cfgs in the file, I consolidated all the approx implementations into one conditional module.

@michaelkirk michaelkirk force-pushed the mkirk/ulps-eq branch 2 times, most recently from ffc3ee4 to de3e471 Compare February 5, 2025 00:29
Copy link
Member

@frewsxcv frewsxcv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this

@michaelkirk michaelkirk added this pull request to the merge queue Feb 12, 2025
Merged via the queue into main with commit 7cb26fb Feb 12, 2025
18 checks passed
@michaelkirk michaelkirk deleted the mkirk/ulps-eq branch February 12, 2025 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants