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

Add hypotnuse method to Distance #7666

Open
MrTinker64 opened this issue Jan 12, 2025 · 2 comments
Open

Add hypotnuse method to Distance #7666

MrTinker64 opened this issue Jan 12, 2025 · 2 comments
Labels
component: wpiunits Java units library type: feature Brand new functionality, features, pages, workflows, endpoints, etc.

Comments

@MrTinker64
Copy link
Contributor

MrTinker64 commented Jan 12, 2025

Is your feature request related to a problem? Please describe.
I'm always frustrated when I have to do distance.in(distanceUnit) followed by some math and then unit.of(resultOfMyMath)

Describe the solution you'd like
The same way there is .div and .plus methods for Distance I would like a .hypot(otherDistance) method. Using the plus method at Line 59 of Distance as an example I would write it like this:

@Override
default Distance hypot(Measure<? extends DistanceUnit> other) {
  return (Distance) unit().ofBaseUnits(Math.hypot(baseUnitMagnitude() + other.baseUnitMagnitude()));
}

Additional context
Here's what I'm currently using as a work around in my code:

public static Distance hypot(Distance a, Distance b) {
  return a.unit().of(Math.hypot(a.magnitude(), b.magnitude()));
}
@MrTinker64 MrTinker64 added the type: feature Brand new functionality, features, pages, workflows, endpoints, etc. label Jan 12, 2025
@auscompgeek
Copy link
Member

IMO this doesn't sound like it should be an instance method. Reading a.hypot(b) it's very unclear what operation it's meant to be performing on a. I'm not sure how one would explain/document this as an operation either.

On the other hand a static method hypot(a, b) would be significantly easier to explain.

@auscompgeek auscompgeek added the component: wpiunits Java units library label Jan 12, 2025
@MrTinker64
Copy link
Contributor Author

So instead it would be Distance.hypot(a, b)? That sounds good to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: wpiunits Java units library type: feature Brand new functionality, features, pages, workflows, endpoints, etc.
Projects
None yet
Development

No branches or pull requests

2 participants