Math
Math: clamping, interpolation, wrapping, rounding without float noise, smoothing.
| Export | What it does |
|---|---|
applyHysteresis | Updates an on/off state with hysteresis: on at or above highThreshold, off at or below lowThreshold, unchanged in between. |
ceilToStep | Rounds a number up to a multiple of a step, without float noise. |
clamp | Restricts a number to an interval. |
floorToStep | Rounds a number down to a multiple of a step, without float noise. |
hasSignificantChange | Checks whether a value moved enough since the displayed one to be worth a render (a deadband). |
interpolateTable | Reads a lookup table with linear interpolation between its points, such as a calibration curve or a tank gauging table. |
inverseLerp | Computes where a value sits between two numbers: the inverse of lerp. |
isBetween | Checks whether a number lies between two bounds. |
isNearlyEqual | Checks whether two numbers are equal within a tolerance, relative for large numbers and absolute near zero. |
lerp | Interpolates linearly between two numbers, exact at both ends and extrapolated outside [0, 1]. |
moveTowards | Moves a value towards a target at a limited rate (a slew-rate limiter), whatever the frame rate: a needle or a rudder that cannot jump. |
ratio | Divides a value by a total, without NaN or Infinity. |
remap | Maps a value from an input range to an output range, such as a sensor reading to a gauge angle. |
roundToFractionDigits | Rounds a number to a number of decimals, half away from zero, with plain arithmetic: the same result as Number(formatDecimal(value, maxFractionDigits)), much faster. |
roundToSignificantDigits | Rounds a number to a number of significant digits, whatever its magnitude. |
roundToStep | Rounds a number to the nearest multiple of a step, without float noise: roundToStep(0.3, 0.1) is 0.3. |
smoothTowards | Moves a value towards a target with exponential smoothing, whatever the frame rate: about 63 % of the distance is covered every timeConstantMs. |
wrap | Wraps a number into [min, max[ with an always-positive modulo, like a heading wrapping at 360°. |