-
Notifications
You must be signed in to change notification settings - Fork 202
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
[wip] First attempts at implementing Haversine distance #48
Conversation
So you're looking to have a In general with Rust, the way to create new methods on types is through traits. In this particular case: trait ToRadians<T> {
fn to_radians(self) -> T;
}
Anyways, we want impl<F: Float> ToRadians<F> for F {
fn to_radians(self) -> F {
// code here
}
} This is an generic trait implementation ( Anyways, now for the meat of the implementation: impl<F: Float> ToRadians<F> for F {
fn to_radians(self) -> F {
self * F::from(PI / 180f64).unwrap()
}
} As you can see here, You should be able to put this I hastily wrote this as I'm pretty busy with some work things, so sorry if there's any typos. Also, don't hesitate if you have any questions. |
Also, in case it makes it easier to understand, here's how it would be implemented with a plain old function: fn to_radians<F: Float>(x: F) -> F {
x * F::from(PI / 180f64).unwrap()
} |
@frewsxcv thanks for the input! There is one bit of the implementation which had a slightly different intent, though: to_radians is a built-in method of the f64 and f32 types - I was originally going to just implement it from scratch, but when I noticed that there was a built-in, went through these hoops in an effort to use it, rather than implementing it anew on a different type. |
Oh wow, totally missed that it was already implemented for |
FYI, |
New version of rust-num just got published. |
👍 awesome, taking another shot at this branch. |
The baseline algorithm is implemented, and matches with a javascripty baseline! Now to think about how to expose it - right now it's very separate from the existing distance algorithm, but now looking at how |
That's a good point. If we went the type parameter route for |
b88809a
to
e26beeb
Compare
Superseded by #62. |
This very much doesn't work yet. We're requiring the Float trait, but I want to use
.to_radians()
instead of doing it the manual multiplication way, an in order to require that the numeric type has that method, it requires a new trait, ToRadians, and currently that wreaks some havoc on the rest of the rust-geo codebase and I'm still learning why.