Scale
Scaling takes an input value within some range and scales (or maps) it to a target range, most likely a scalar.
For example, say you have a value of 105, that lies on a scale of 0..1024, common for analog sensors. What is the proportional value of 105? We want to scale from an input range of 0…1024 to the scalar range of 0..1.
A basic Numbers.scale function (included in ixfx) looks like this:
This is how it looks in action. The default output range is 0..1, so we don’t need to include it.
You can also specify an output range. Below, we scale 30 from an input range of 20-40 (ie it’s 50% of that scale) to an output range of 100-200, yielding a value of 150.
If the input value is outside of the specified input range, the output value will likewise be outside of the output range. Clamp the value to ensure the output range is respected, or use Numbers.scaledClamped .
Scaler
If you’re scaling things by the same value repeatedly, rather than put the scale ranges all over your code, use Numbers.scaler to create a reusable scaling function.