Transforming range of variable to 0 to 1 and custom ranges

Transforming range of variable to 0 to 1 and custom ranges

Created:18 Jul 2020 00:39:07 , in  Maths

One useful skill when it comes to dealing with variables is to be able to change their range. One of the most useful and desirable ranges is [0,1]. Occasionally you might need a different range like [-1,1], too. This article describes a couple of methods of scaling and descaling a variable's range.

Quick and easy transformation to [0,1]

One way to scale range of variable x to range from 0 to 1 is based on the following function:

y = x / ( 1 + x ) y = x / (1 + x)

scaled variable x, called y, will be in the range between 0 and 1.

Scaling a to custom range

If a range from a to b, where a is the minimum and b is the maximum allowed value, is required, one of the three following formulas can be used:


y = ( ax + b ) ( 1 + x ) y = (ax + b) / (1 + x)


y = a + ( b a ) ( 1 e x ) y = a + (b - a)(1 - e^(-x))

where e is the Euler's number


y = a + 2 ( b a ) ( arctan ( x ) ) / π y = a + 2 (b - a) (arctan(x)) / pi

Scaling variable from [0,1] to a custom range

If a variable y is in range from 0 to 1, it can be scaled to a variable z which is in range between a and b as follows:

z = a + ( b a ) y z = a + (b - a) * y

Min-max scaling

Min-max scaling, also known as min-max normalization, is another easy way, which is frequently used for transforming variable's range.

The formula for min-max scaling is as follows:

y = ( x a ) ( b a ) y = (x - a) / (b - a)

where a and b denote minimum and maximum values of x in a set of data respectively.

Descaling variable y back to x can be done using:

x = y ( b a ) + a x = y * (b - a) + a

Min-max scaling in PHP

Two functions given below perform min-max scaling and descaling as described in the previous section.

function minMaxNormalize($x, $a, $b) {
  $y = false;
  try {
    $y = ($x - $a) / ($b - $a);
  }catch(DivisionByZeroError $e){ 
    echo $e -> getMessage(),"\n";
  return $y;

function minMaxDenormalize($y, $a, $b) {
  $x = ($y * ($b - $a) + $a);
  return $x;

Here is how to use these functions

Suppose that the range of values of x is from -10 to 10, but we want it to be between 0 and 1. To transform the range, do the following:

minMaxNormalize(7, -10, 10);
// => 0.85

To transform variables to be back in the range [-10,10] :

// => 7


As can be seen in this article, there are quiet a few ways of transforming a range of a variable both to [0,1] or to a custom range. I hope you have found at least one of them sufficient for your purposes. If you have something to add, you are welcome to post a comment below.

This post was updated on 18 Jul 2020 01:30:45

Tags:  php 

Author, Copyright and citation


Sylwester Wojnowski

Author of the above article, Sylwester Wojnowski, enjoys sWWW writing computer code in PHP, JavaScript and BASH, and some other things he wrote more on on the About page of this website.


©Copyright, 2020 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.

Computer code, if present in the article, is excluded from the above and licensed under GPLv3.


Cite this article as:

Wojnowski, Sylwester. "Transforming range of variable to 0 to 1 and custom ranges." From sWWW - Code For The Web .

Add Comment

Allowed BB Code - style tags: [b][/b], [i][/i], [code=text][/code],[code=javascript][/code],[code=php][/code],[code=bash][/code],[code=css][/code],[code=html][/code]

I constent to processing my data given through this form for purposes of a reply by the administrator of this website.

Recent Comments

Nobody has commented on this post yet. Be first!