Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I'm struggling with this for quite some time now. I'm trying to recalculate values uniformly distributed in real numbers range to numbers in non-negative range. Length of non-negative range can vary (but always is a power of two), and with increasing the length, amount of numbers increases up to length of second range, and step decreases.

Example 1: The fixed real range is (-16,16). The non-negative range is <0,32). Amount of numbers is 32. Step is equal 0.5.
Now we want to recalculate numbers so that the first number in first range will be equal to first range in second range, i.e.:
-15.5 = 0,
-15 = 1,
15.5 = 31.

Example 2: The fixed real range is (-16,16). The non-negative range is <0,64). Amount of numbers is 64. Step is equal 0.25.
Now:
-15.75 = 0,
15.75 = 31.

Probably it's just a simple task, but I'm after many hours of hardcore programming and samehow cannot come up with a proper solution.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

The number of steps in your range is $\dfrac{2\max }{\text{step}}-1$, (where $\max$ is the end of the interval, but that value is not used) so in your first case there are $\dfrac{2\times 16}{0.5}-1=63$ If you want to map them into $[0,32)$ the stepsize has to be $0.5$. As the step sizes are the same, the mapping is just $\text{new}=\text{old}+15.5$. For your second case, there are $\dfrac{2\times 16}{0.25}-1=127$ steps. To fit them into $[0,64)$ you need a step of $0.5$ As the step size doubles, the mapping is $\text{new}=2(\text{old}+15.75)$ In the general case the procedure would be:

1) calculate the number of steps as $\dfrac{2\max}{\text{step}}-1$

2) calculate the minimum old value as $\min + \text{step}$

3) calculate the new step size = $(\text{old step}) \dfrac{\text{new range}}{\text{old range}}$

4) as you want the minimum new value to be zero (is this always true?), you have $\text{new value}=\dfrac{\text{new step}}{\text{old step}}(\text{old value}-\text{minimum old value})$

share|improve this answer
    
yes, the minimum value is always 0 –  mike_hornbeck Sep 29 '11 at 22:49
    
Shouldn't you add one instead of subtract one to find the number of steps? For example, from -1 to 1 in steps of 1 is 3 possibilities, but your formula only yields 1. Or are you assuming that the OP means to exclude the endpoints? –  Henning Makholm Sep 29 '11 at 23:07
    
@HenningMakholm:I subtracted $1$ because I used the end of the interval (16) instead of the end step (15.5). Yes, from the examples I took OP to be excluding the endpoints. I will make that clear. –  Ross Millikan Sep 29 '11 at 23:10
    
Ah, I must be blind. –  Henning Makholm Sep 29 '11 at 23:13
    
the ranges look just as Ross said. First is opened both sides, second is closed from the zero side. –  mike_hornbeck Sep 29 '11 at 23:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.