Skip to content

Math ​

Math: clamping, interpolation, wrapping, rounding without float noise, smoothing.

ExportWhat it does
applyHysteresisUpdates an on/off state with hysteresis: on at or above highThreshold, off at or below lowThreshold, unchanged in between.
ceilToStepRounds a number up to a multiple of a step, without float noise.
clampRestricts a number to an interval.
floorToStepRounds a number down to a multiple of a step, without float noise.
hasSignificantChangeChecks whether a value moved enough since the displayed one to be worth a render (a deadband).
interpolateTableReads a lookup table with linear interpolation between its points, such as a calibration curve or a tank gauging table.
inverseLerpComputes where a value sits between two numbers: the inverse of lerp.
isBetweenChecks whether a number lies between two bounds.
isNearlyEqualChecks whether two numbers are equal within a tolerance, relative for large numbers and absolute near zero.
lerpInterpolates linearly between two numbers, exact at both ends and extrapolated outside [0, 1].
moveTowardsMoves 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.
ratioDivides a value by a total, without NaN or Infinity.
remapMaps a value from an input range to an output range, such as a sensor reading to a gauge angle.
roundToFractionDigitsRounds a number to a number of decimals, half away from zero, with plain arithmetic: the same result as Number(formatDecimal(value, maxFractionDigits)), much faster.
roundToSignificantDigitsRounds a number to a number of significant digits, whatever its magnitude.
roundToStepRounds a number to the nearest multiple of a step, without float noise: roundToStep(0.3, 0.1) is 0.3.
smoothTowardsMoves a value towards a target with exponential smoothing, whatever the frame rate: about 63 % of the distance is covered every timeConstantMs.
wrapWraps a number into [min, max[ with an always-positive modulo, like a heading wrapping at 360°.

Generated from the JSDoc of the sources.