Search code examples
javascriptnormalization

Normalizing from values [- 1, 1] to [0, 1]


This function takes value from [-1, 1] and turns value to [0, 1](it's not an array)

function normalize(x){
 var y = 2 * x / 2;
 return y;
}

With x=0.5 it would return x=0.25, but when x is 0, it returns 0, I need value 0,5.


Solution

  • To normalize on an interval from [-1, 1], first add 1 and then divide by 2. You are multiplying by 2 and then dividing by 2, which yields the original number.

    function normalize(x){
     var y = (x + 1) / 2;
     return y;
    }