问题描述
数学从来都不是我在学校的强项:(
Math was never my strong suit in school :(
int input_start = 0; // The lowest number of the range input.
int input_end = 254; // The largest 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 output.
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
,y
],我们只需要适当的缩放.0到0,x
到y
,数字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
],其中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
],其中R = output_end - output_start
.
Similarly, the output range is equivalent to [0
, R
], where R = output_end - output_start
.
input
的输入等价于 x = input - input_start
.这,从第一段将转换为 y = (R/r)*x
.然后,我们可以通过添加 output_start
将 y
值转换回原始输出范围:output = 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)
或者,另一种方式:
/* 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)
如果想要更正确,您可以在最后一步进行四舍五入而不是截断.您可以通过编写一个简单的 round
函数来实现:
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))
这篇关于将数字范围映射到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!