mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[wpiunits] Add Measure.divide(Measure<U2>) (#6611)
This commit is contained in:
@@ -137,16 +137,29 @@ public interface Measure<U extends Unit<U>> extends Comparable<Measure<U>> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides this measurement by some constant divisor and returns the result. This is equivalent to
|
||||
* {@code divide(divisor.baseUnitMagnitude())}
|
||||
* Divides this measurement by another measure and performs some dimensional analysis to reduce
|
||||
* the units.
|
||||
*
|
||||
* @param divisor the dimensionless measure to divide by
|
||||
* @param <U2> the type of the other measure to multiply by
|
||||
* @param other the unit to multiply by
|
||||
* @return the resulting measure
|
||||
* @see #divide(double)
|
||||
* @see #times(double)
|
||||
*/
|
||||
default Measure<U> divide(Measure<Dimensionless> divisor) {
|
||||
return divide(divisor.baseUnitMagnitude());
|
||||
default <U2 extends Unit<U2>> Measure<?> divide(Measure<U2> other) {
|
||||
if (unit().getBaseUnit().equals(other.unit().getBaseUnit())) {
|
||||
return Units.Value.ofBaseUnits(baseUnitMagnitude() / other.baseUnitMagnitude());
|
||||
}
|
||||
if (other.unit() instanceof Dimensionless) {
|
||||
return divide(other.baseUnitMagnitude());
|
||||
}
|
||||
if (other.unit() instanceof Velocity<?> velocity
|
||||
&& velocity.getUnit().getBaseUnit().equals(unit().getBaseUnit())) {
|
||||
return times(velocity.reciprocal().ofBaseUnits(1 / other.baseUnitMagnitude()));
|
||||
}
|
||||
if (other.unit() instanceof Per<?, ?> per
|
||||
&& per.numerator().getBaseUnit().equals(unit().getBaseUnit())) {
|
||||
return times(per.reciprocal().ofBaseUnits(1 / other.baseUnitMagnitude()));
|
||||
}
|
||||
return unit().per(other.unit()).ofBaseUnits(baseUnitMagnitude() / other.baseUnitMagnitude());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user