views:

1536

answers:

5

Hi all,

I'm kind of stuck here, I guess it's a bit of a brain teaser. If I have numbers in the range between 0.5 to 1 how can I normalize it to be between 0 to 1?

Thanks for any help, maybe I'm just a bit slow since I've been working for the past 24 hours straight O_O

+14  A: 

× 2 − 1

should do the trick

Glenner003
That will round to 0,1 or 2.
erelender
It was the math part of the solution I was solving not the language related part
Glenner003
+26  A: 

Subtract 0.5 (giving you a new range of 0 - 0.5) then multiply by 2.

double normalize( double x )
{
    // I'll leave range validation up to you
    return (x - 0.5) * 2;
}
Bill the Lizard
+11  A: 

The solution to this problem is to get some sleep.

dingle_thunk
+30  A: 

Others have provided you the formula, but not the work. Here's how you approach a problem like this. You might find this far more valuable then just knowning the answer.

To map [0.5, 1] to [0, 1] we will seek a linear map of the form x -> ax + b. We will require that endpoints are mapped to endpoints and that order is preserved.

Method one: The requirement that endpoints are mapped to endpoints and that order is preserved implies that 0.5 is mapped to 0 and 1 is mapped to 1

a * (0.5) + b = 0 (1)
a * 1 + b = 1     (2)

This is a simultaneous system of linear equations and can be solved by multiplying equation (1) by -2 and adding equation (1) to equation (2). Upon doing this we obtain b = -1 and substituting this back into equation (2) we obtain that a = 2. Thus the map x -> 2x - 1 will do the trick.

Method two: The slope of a line passing through two points (x1, y1) and (x2, y2) is

(y2 - y1) / (x2 - x1).

Here we will use the points (0.5, 0) and (1, 1) to meet the requirement that endpoints are mapped to endpoints and that the map is order-preserving. Therefore the slope is

m = (1 - 0) / (1 - 0.5) = 1 / 0.5 = 2.

We have that (1, 1) is a point on the line and therefore by the point-slope form of an equation of a line we have that

y - 1 = 2 * (x - 1) = 2x - 2

so that

y = 2x - 1.

Once again we see that x -> 2x - 1 is a map that will do the trick.

Jason
In method one, to solve, why do you multiply equation (1) by -2 ?
RobertL
@RobertL: To solve a linear system of two equations with two unknowns, the easiest method is to convert the system to an equivalent one where the coefficients on one of the unknowns are equal in both equations. So here we have the two equations `a * (0.5) + b = 0` and `a * 1 + b = 1`. After multiplying the first equation by `2` we arrive at the two equations `a + 2 * b = 0` and `a * 1 + b = 1`. Since the coefficients on `a` are equal in both equations we can subtract the first equation from the second to obtain `-b = 1`.
Jason
+3  A: 

To add another generic answer.

If you want to map the linear range [A..B] to [C..D], you can apply the following steps:

Shift the range so the lower bound is 0. (subract A from both bounds:

[A..B] -> [0..B-A]

Scale the range so it is [0..1]. (divide by the upper bound):

[0..B-A] -> [0..1]

Scale the range so it has the length of the new range which is D-C. (multiply with D-C):

[0..1] ->  [0..D-C]

Shift the range so the lower bound is C. (add C to the bounds):

[0..D-C] -> [C..D]

Combining this to a single formula, we get:

       (D-C)*(X-A)
X' =   -----------  + C
          (B-A)

In your case, A=0.5, B=1, C=0, D=1 you get:

       (X-0.5)
X' =   ------- = 2X-1
        (0.5)

Note, if you have to convert a lot of X to X', you can change the formula to:

       (D-C)         C*B - A*D
X' =   ----- * X  +  ---------  
       (B-A)           (B-A)

It is also interesting to take a look at non linear ranges. You can take the same steps, but you need an extra step to transform the linear range to a nonlinear range.

Gamecat