Hmm, I''m still confused... tell me if I understand correctly
you have a range from 0 to 255 right now
(0,1,2,3,...254,255) with a step of 1
and you want a new range say, something like 2 - 17 with a step of 3 you''d want the numbers within the set of
(2,5,8,11,14,17)
is this correct?
-Michael
i need help with writing an equation
i think you need this: (k+1) / 2
where k = 255 makes 255 + 1 / 2 = 128.
however it doesnt get you the by 4 thing you want, it instead mapps out like:
1
1
2
2
3
3
...127
127
128
something like that.
where k = 255 makes 255 + 1 / 2 = 128.
however it doesnt get you the by 4 thing you want, it instead mapps out like:
1
1
2
2
3
3
...127
127
128
something like that.
quote:Original post by Thr33d
Hmm, I''m still confused... tell me if I understand correctly
you have a range from 0 to 255 right now
(0,1,2,3,...254,255) with a step of 1
and you want a new range say, something like 2 - 17 with a step of 3 you''d want the numbers within the set of
(2,5,8,11,14,17)
is this correct?
-Michael
yeah
i need to convert from one range to the other range, without decimal values, whole numbers
Solution:
To convert from A to B by Cs (in your terminology)
to D to E by Fs you do for each x in the first sequence:
y=trunc((x-A)/(B-A)*(E-D)/F)*F+D
Note that this means some values of x can be mapped to the same y and visa versa.
Trunc removes the decimal part of the number in question (i forget the C syntax). You could re-cast to integer and the compiler would do this automatically.
Rounding may be better than truncating, you'll need to experiment (I cannot say as you have not said what you want this for...)
EDIT: NB: The reason why everyone was so confused over what you said is that your second post contained a mistake:
(1,4,8,12,...) is wrong as the first interval is 3 when you wanted each step to be SMALL_STEP=4.
[edited by - sadwanmage on November 7, 2003 11:24:10 AM]
To convert from A to B by Cs (in your terminology)
to D to E by Fs you do for each x in the first sequence:
y=trunc((x-A)/(B-A)*(E-D)/F)*F+D
Note that this means some values of x can be mapped to the same y and visa versa.
Trunc removes the decimal part of the number in question (i forget the C syntax). You could re-cast to integer and the compiler would do this automatically.
Rounding may be better than truncating, you'll need to experiment (I cannot say as you have not said what you want this for...)
EDIT: NB: The reason why everyone was so confused over what you said is that your second post contained a mistake:
(1,4,8,12,...) is wrong as the first interval is 3 when you wanted each step to be SMALL_STEP=4.
[edited by - sadwanmage on November 7, 2003 11:24:10 AM]
If you use integers for all your calculations, you don''t need to worry about decimal values. How about this:
newVal = ((oldVal - oldMin) * (newMax - newMin) / (oldMax - oldMin)) / newStep * newStep + newMin
Expanation: You take the oldVal (old values) numbers, and subtract the oldMin (old minimum) number, thus getting the values relative to the old minimum. Then you divide by (oldMax - oldMin) to normalize the range of values to 0..1. Then, you multiply by (newMax - newMin) to expand set of possible values to the size of the new range of numbers. Then swap the order of those so that you multiply first, thus getting relatively good approximations to the correct ratios without using decimal numbers in the middle. Next, divide by newStep and multiply by newStep, in that order (using integer arithmatic) so you only get values that are every newStep numbers. There may be some duplication in this step depending on the old and new steps, but that can''t really be avoided. Next, add newMin to shift all the results up so they actually fall into the range from newMin to newMax.
newVal = ((oldVal - oldMin) * (newMax - newMin) / (oldMax - oldMin)) / newStep * newStep + newMin
Expanation: You take the oldVal (old values) numbers, and subtract the oldMin (old minimum) number, thus getting the values relative to the old minimum. Then you divide by (oldMax - oldMin) to normalize the range of values to 0..1. Then, you multiply by (newMax - newMin) to expand set of possible values to the size of the new range of numbers. Then swap the order of those so that you multiply first, thus getting relatively good approximations to the correct ratios without using decimal numbers in the middle. Next, divide by newStep and multiply by newStep, in that order (using integer arithmatic) so you only get values that are every newStep numbers. There may be some duplication in this step depending on the old and new steps, but that can''t really be avoided. Next, add newMin to shift all the results up so they actually fall into the range from newMin to newMax.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement