本文介绍了根据另一个数字的百分比从范围中选择一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在两个数字之间选择一个数字,具体取决于输入的百分比.可以说范围在40-60之间.输入范围在1到10之间.如果输入值为 10 ,则输出应为 60. value = 1 , output = 40. value = 5 , output = 50.

I want to select a number between two numbers depends on the percentage of an input. lets say the range is between 40-60. and input range is between 1-10. if the input value is 10 , the output should be 60. value = 1 , output = 40. value = 5 , output = 50.

我首先想弄清楚算法,如何开始

I am first trying to figure the algorithm, how to begin with

到目前为止,我已经使用了各种不同的公式.通常,要将变量x缩放到[a,b]范围内,可以使用:

so far I have used various different formulas.In general, to scale your variable x into a range [a,b] you can use:

normalized = ((b−a)x−min(x))(max(x)−min(x))+a

https://stats.stackexchange.com/a/281165

推荐答案

因此,此处的高阶函数可能会有用:

So, a higher-order function might be useful here:

const func = (outMin, outMax, inMin, inMax) =>
             v => outMin + (outMax - outMin) * (v - inMin) / (inMax - inMin);

const boundFunc = func(40, 60, 1, 10);

const v1 = boundFunc(1); //40
const v2 = boundFunc(5); //48.8888....
const v3 = boundFunc(10); //60

console.log(v1, v2, v3);

这篇关于根据另一个数字的百分比从范围中选择一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 07:07