Normalizing Numbers

Normalizing can be confusing because there are some slightly different, but similar meanings, in different situations.

Normalizing numbers

Typically the target of normalizing is to get numbers either in the range [0 -> 1], or [-1 -> 1].

To do this you'll need to know the minimum and maximum numbers in the range, and map your value to them.

Let's make a function we can use to normalize. We'll work with the number 12, and the range is 0 -> 20.

function normalize(val = 0, max = 1, min = 0) {
  return val / max;

normalize(12, 20);
// returns 0.6

If we pop in our value 12, and set a max of 20, we'll get a value back of 0.6, which is correct.

We're not using the min yet, so lets use 12 as our value again, but make our range 10 -> 20. We'll need to update our function.

function normalize(val = 0, max = 1, min = 0) {
  return (val - min) / (max - min);

normalize(12, 20, 10);
// returns 0.2

There are lots of ways to do this, and lots of choices you can make for style. For example, what if your value is outside of your range?

normalize(22, 20, 10);
// What should this return?

22, our value, is larger than 20 our max. Our function will return 1.2 as it is. Should it return 1? These are all options and fun you can have with normalizing that will give you different results in your experiments!