Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions wpiunits/generate_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,30 @@ def output(output_dir, outfn: str, contents: str):
"Force": "Torque",
},
"divide": {"Time": "LinearVelocity", "LinearVelocity": "Time"},
"extra": inspect.cleandoc(
"""
/** Returns the hypotenuse (sqrt(a^2 + b^2)) between two Distances.
* The result is returned in the units of the first parameter.
* @param a the first distance
* @param b the second distance
* @return the hypotenuse between a and b
*/
static Distance hypot(Distance a, Distance b) {
return a.unit().ofBaseUnits(
Math.hypot(a.in(a.unit().getBaseUnit()), b.in(b.unit().getBaseUnit()))
);
}

/** Returns the hypotenuse (sqrt(this^2 + other^2)) between two Distances.
* The result is returned in the units of the first parameter.
* @param other the second distance
* @return the hypotenuse between this and other
*/
default Distance hypot(Distance other) {
return hypot(this, other);
}
"""
),
},
"Energy": {
"base_unit": "Joules",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions wpiunits/src/main/java/edu/wpi/first/units/Measure.java
Original file line number Diff line number Diff line change
Expand Up @@ -1777,6 +1777,7 @@ static <U extends Unit> Measure<U> min(Measure<U>... measures) {
return max;
}


/**
* Returns a string representation of this measurement in a shorthand form. The symbol of the
* backing unit is used, rather than the full name, and the magnitude is represented in scientific
Expand Down
24 changes: 24 additions & 0 deletions wpiunits/src/test/java/edu/wpi/first/units/MeasureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,30 @@ void testAs() {
assertEquals(1, m.in(Units.Feet), Measure.EQUIVALENCE_THRESHOLD);
}

@Test
void testHypot() {
Distance a = Units.Meters.of(6);
Distance b = Units.Meters.of(8);

// Static method on Distance
Distance resStatic = Distance.hypot(a, b);
assertEquals(Units.Meters.of(10), resStatic);

// Instance method on Distance delegating to static
Distance resInstance = a.hypot(b);
assertEquals(Units.Meters.of(10), resInstance);

// Mixed units: 3 feet and 48 inches (4 feet) -> hypot = 5 feet
Distance f3 = Units.Feet.of(3);
Distance in48 = Units.Inches.of(48);

Distance mixed = Distance.hypot(f3, in48);
assertTrue(mixed.isEquivalent(Units.Feet.of(5)));

Distance mixedInstance = f3.hypot(in48);
assertTrue(mixedInstance.isEquivalent(Units.Feet.of(5)));
}

@Test
void testPerMeasureTime() {
var measure = Units.Kilograms.of(144);
Expand Down
Loading