mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -109,6 +109,15 @@ public class Per<N extends Unit<N>, D extends Unit<D>> extends Unit<Per<N, D>> {
|
||||
return m_denominator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the reciprocal of this Per.
|
||||
*
|
||||
* @return the reciprocal
|
||||
*/
|
||||
public Per<D, N> reciprocal() {
|
||||
return m_denominator.per(m_numerator);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
|
||||
@@ -157,6 +157,15 @@ public class Velocity<D extends Unit<D>> extends Unit<Velocity<D>> {
|
||||
return m_period;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the reciprocal of this velocity.
|
||||
*
|
||||
* @return the reciprocal
|
||||
*/
|
||||
public Per<Time, D> reciprocal() {
|
||||
return m_period.per(m_unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
|
||||
Reference in New Issue
Block a user