Function to normalize any number from 0 to 1

I am trying to make a function that takes a number and normalizes it from 0 to 1 between the boundaries of min and max. For example:

If I want to normalize the value of 10 between 5 and 15, I call this:

val = 10; normalize(val, 5, 15); Returns 0.5

normalizing a value of 0 between -10 and 5

val = 0; normalize(val, -10, 5); Returns 0.666

This is the function I came with:

 function normalize(val, min, max){ // Shift to positive to avoid issues when crossing the 0 line if(min < 0){ max += 0 - min; val += 0 - min; min = 0; } // Shift values from 0 - max val = val - min; max = max - min; return Math.max(0, Math.min(1, val / max)); } 

My question is: is this the most efficient method for normalizing a one-dimensional value? I will call this function several thousand times per frame at a speed of 60 frames per second, so I would like it to be optimized as much as possible in order to reduce the load on the calculations. I have been looking for normalization formulas, but all I find is 2- or 3-dimensional solutions.

+9
performance javascript math normalization
source share
5 answers

Why not just:

function(val, max, min) { return (val - min) / (max - min); }

+32
source share

Using Nathan Bertons, answer with a pre-configured function for some values โ€‹โ€‹with the same min and max values, you can use this.

 function normalize(min, max) { var delta = max - min; return function (val) { return (val - min) / delta; }; } console.log([5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].map(normalize(5, 15))); 
+8
source share

Using Nathan Burton's answer, you should check if max-min is zero, otherwise the return value will be NaN:

function (val, max, min) { if(max - min === 0) return 1;//or 0, it up to you return (val - min)/(max - min); }

and you can use generalization to limit the range of values โ€‹โ€‹in the data set between any arbitrary points a and b using:

function (val, max, min) { if(max - min === 0) return a;//or b, it up to you return a + (((val - min) * (ba))/(max - min)); }

+3
source share

I prefer to use this formula when I want to normalize a value between two ranges with known minimum and maximum values. You can add checks to limit the input, although this will correctly calculate the difference from the new range if an input value is out of range.

 /** * Normalizes a value from one range (current) to another (new). * * @param { Number } val //the current value (part of the current range). * @param { Number } minVal //the min value of the current value range. * @param { Number } maxVal //the max value of the current value range. * @param { Number } newMin //the min value of the new value range. * @param { Number } newMax //the max value of the new value range. * * @returns { Number } the normalized value. */ const normalizeBetweenTwoRanges = (val, minVal, maxVal, newMin, newMax) => { return newMin + (val - minVal) * (newMax - newMin) / (maxVal - minVal); }; const e = document.getElementById('result'); e.innerHTML = normalizeBetweenTwoRanges(10, 5, 15, 0, 1); 
 <span id="result"></span> 
0
source share

I will complete Nina Scholzโ€™s answer, you can use the underscore library to find the minimum and maximum for the array, and you can use it as it should, or create your own (simple methods with a simple loop):

if you are using nodeJS, you need to set the underline using the npm install underscore command and use var _ = require('underscore/underscore'); with the command var _ = require('underscore/underscore'); or you can import the library into HTML and use it online

 function min(arr) { if (!arr.length) return null; var minValue = arr[0]; for (var item of arr) { if (item maxValue) maxValue = item; } return maxValue; } var array = [1, 2, 3, 4, 5, 6, 7, 8, 9]; function normalize(min, max) { var delta = max - min; return function (val) { return (val - min) / delta; }; } function normalizeArray(array) { var minValue = min(array); //if you use underscore _.min(array) var maxValue = max(array);//if you use underscore _.max(array) return array.map(normalize(minValue, maxValue)); } console.log(normalizeArray(array));
function min(arr) { if (!arr.length) return null; var minValue = arr[0]; for (var item of arr) { if (item maxValue) maxValue = item; } return maxValue; } var array = [1, 2, 3, 4, 5, 6, 7, 8, 9]; function normalize(min, max) { var delta = max - min; return function (val) { return (val - min) / delta; }; } function normalizeArray(array) { var minValue = min(array); //if you use underscore _.min(array) var maxValue = max(array);//if you use underscore _.max(array) return array.map(normalize(minValue, maxValue)); } console.log(normalizeArray(array)); 
-one
source share

All Articles