isNear

fun Number.isNear(expected: Number, tolerance: Number): Boolean

Checks if the given value matches an expected value within a certain tolerance.

Return

Whether or not the actual value is within the allowed tolerance

Parameters

expected

The expected value

tolerance

The allowed difference between the actual and the expected value


fun Number.isNear(expected: Number, tolerance: Number, min: Number, max: Number): Boolean

Checks if the given value matches an expected value within a certain tolerance. Supports continuous input for cases like absolute encoders.

Continuous input means that the min and max value are considered to be the same point, and tolerances can be checked across them. A common example would be for absolute encoders: calling isNear(2, 359, 5, 0, 360) returns true because 359 is 1 away from 360 (which is treated as the same as 0) and 2 is 2 away from 0, adding up to an error of 3 degrees, which is within the given tolerance of 5.

Return

Whether or not the actual value is within the allowed tolerance

Parameters

expected

The expected value

tolerance

The allowed difference between the actual and the expected value

min

Smallest value before wrapping around to the largest value

max

Largest value before wrapping around to the smallest value