映射一个数字范围到另一个 [英] Mapping a numeric range onto another
问题描述
数学从来不是我在学校的强项:(
Math was never my strong suit in school :(
int input_start = 0; // The lowest number of the range input.
int input_end = 254; // The lowest number of the range input.
int output_start = 500; // The lowest number of the range output.
int output_end = 5500; // The largest number of the range ouput.
int input = 127; // Input value.
int output = 0;
我怎样才能输入值转换为一系列相应的产值?
How can I convert the input value to the corresponding output value of that range?
例如,0的输入值应等于500的产值中,254的输入值应等于5500的输出值。我无法弄清楚如何如果输入值是说50或101计算的输出值。
For example, an input value of "0" would equal an output value of "500", an input value of "254" would equal an output value of "5500". I can't figure out how to calculate an output value if an input value is say 50 or 101.
我敢肯定,这很简单,我不认为现在:)
I'm sure it's simple, I can't think right now :)
编辑:我只需要整数,无小数或任何
I just need whole numbers, no fractions or anything.
推荐答案
让我们忘记了数学和尝试直观地解决这个问题。
Let's forget the math and try to solve this intuitively.
首先,如果我们想输入数字范围为[ 0
, X
]输出范围[图 0
,是
],我们只需要一个适当的量规模。 0到0, X
进入是
,以及一些 T
将前往(Y / X)* T
。
First, if we want to map input numbers in the range [0
, x
] to output range [0
, y
], we just need to scale by an appropriate amount. 0 goes to 0, x
goes to y
, and a number t
will go to (y/x)*t
.
所以,让我们减少您的问题上面简单的问题。
So, let's reduce your problem to the above simpler problem.
输入电压范围为[ input_start
, input_end
]已 input_end - input_start + 1
号码。因此,它相当于一个范围[ 0
,研究
],其中 R = input_end - input_start
。
An input range of [input_start
, input_end
] has input_end - input_start + 1
numbers. So it's equivalent to a range of [0
, r
], where r = input_end - input_start
.
同样,输出范围等效于[ 0
,研究
],其中 R = OUTPUT_END - OUTPUT_START
Similarly, the output range is equivalent to [0
, R
], where R = output_end - output_start
.
输入
的输入等同于 X =输入 - input_start
。这一点,从第一款将转化为 Y =(R / R)* X
。然后,我们可以通过添加 OUTPUT_START
的是
值转换回原来的输出范围:输出= OUTPUT_START + Y
。
An input of input
is equivalent to x = input - input_start
. This, from the first paragraph will translate to y = (R/r)*x
. Then, we can translate the y
value back to the original output range by adding output_start
: output = output_start + y
.
这给了我们:
output = output_start + ((output_end - output_start) / (input_end - input_start)) * (input - input_start)
或者另一种方式:
Or, another way:
/* Note, "slope" below is a constant for given numbers, so if you are calculating
a lot of output values, it makes sense to calculate it once. It also makes
understanding the code easier */
slope = (output_end - output_start) / (input_end - input_start)
output = output_start + slope * (input - input_start)
现在,这是C,除用C截断,你应该尝试在浮点计算的东西,以获得更准确的答案:
Now, this being C, and division in C truncates, you should try to get a more accurate answer by calculating things in floating-point:
double slope = 1.0 * (output_end - output_start) / (input_end - input_start)
output = output_start + slope * (input - input_start)
如果希望更加正确,你会做的最后一步,而不是四舍五入截断。您可以通过编写一个简单的圆形
功能做到这一点:
If wanted to be even more correct, you would do a rounding instead of truncation in the final step. You can do this by writing a simple round
function:
#include <math.h>
double round(double d)
{
return floor(d + 0.5);
}
然后:
output = output_start + round(slope * (input - input_start))
这篇关于映射一个数字范围到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!