mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[wpiunits] Add isNear function implementation (#6396)
This implementation uses a tolerance in the same units as the measure it checks.
This commit is contained in:
@@ -265,6 +265,25 @@ public interface Measure<U extends Unit<U>> extends Comparable<Measure<U>> {
|
||||
return Math.abs(this.baseUnitMagnitude() - other.baseUnitMagnitude()) <= tolerance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this measure is near another measure of the same unit, with a specified tolerance of
|
||||
* the same unit.
|
||||
*
|
||||
* <pre>
|
||||
* Meters.of(1).isNear(Meters.of(1.2), Millimeters.of(300)) // true
|
||||
* Degrees.of(90).isNear(Rotations.of(0.5), Degrees.of(45)) // false
|
||||
* </pre>
|
||||
*
|
||||
* @param other the other measure to compare against.
|
||||
* @param tolerance the tolerance allowed in which the two measures are defined as near each
|
||||
* other.
|
||||
* @return true if this unit is near the other measure, otherwise false.
|
||||
*/
|
||||
default boolean isNear(Measure<U> other, Measure<U> tolerance) {
|
||||
return Math.abs(this.baseUnitMagnitude() - other.baseUnitMagnitude())
|
||||
<= Math.abs(tolerance.baseUnitMagnitude());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this measure is equivalent to another measure of the same unit.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user