如何计算相对百分比(将分数应用于新范围)?
How to calculate relative percentage (apply score to a new range)?
我是一名前端和后端开发人员,在处理简单的游戏开发和动画时遇到过几次这样的挑战。我希望我有一个函数来解决这个问题,所以我希望你数学 guys/gals 可以帮助我解决伪代码,然后我可以 post 一个我们可以 运行 通过的函数浏览器进行测试。
这个挑战对我来说有点难解释,所以请原谅我,我会像谜语一样给出它:
You're trying to find your test score as a percent. You know that for a normal percent, expressed as a decimal from 0.00 - 1.00
, you'd do something like actual_test_score / max_test_score
.
However, you're not looking for any old percent. You'd like to express your percent as a decimal within a specified range of decimal numbers. For example, you'd like (given an actual_test_score
and a max_test_score
) to get your test score as a percent from 0.50 - 1.00
.
How can you calculate actual_test_score / max_test_score
expressed as a percentage between a range of 0.50 - 1.00
, instead of your typical range that is 0.00 - 1.00
.
我花了一些时间来解释这一点(这可能不是最好的解释),因为这不是我必须经常解释的事情,但我 运行 时不时地也会这样做。
如果我没看错,你希望 0
表示为 0.5
吗?
在那种情况下,您需要将您的数字乘以分数范围; highest_score - lowest_score
在您的情况下似乎是 1 - 0.5
。
然后将 lowest_score
添加到您的结果中。
这里是粗函数形式的例子;
function relative_percentage (score, max_score, min_score) {
// score should be a value from 0 to 1.
return score * (max_score - min_score) + min_score;
}
这是从[0,1]到[newMin,newMax]的线性范围映射,这是从[oldMin,oldMax]到[newMin,newMax]的一般线性范围映射的特例:
function linearRemap(value, oldMin, oldMax, newMin, newMax) {
var newScale = newMax - newMin;
var valueAsPct = (val - oldMin) / (oldMax - oldMin);
var scaledValue = valueAsPct * newScale;
var shiftedAndScaledValue = scaledValue + newMin;
return shiftedAndScaledValue;
}
因此这里的有限情况允许您跳过百分比计算行:
function linearRemapPct(valueAsPct, newMin, newMax) {
var newScale = newMax - newMin;
var scaledValue = valueAsPct * newScale;
var shiftedAndScaledValue = scaledValue + newMin;
return shiftedAndScaledValue;
}
或者只是
function linearRemapPct(valueAsPct, newMin, newMax) {
return (valueAsPct * (newMax - newMin) + newMin);
}
如果你想要一些更小心的精度损失:
function linearRemapPct(valueAsPct, newMin, newMax) {
return (valueAsPct * newMax) + (1 - valueAsPct) * newMin;
}
我是一名前端和后端开发人员,在处理简单的游戏开发和动画时遇到过几次这样的挑战。我希望我有一个函数来解决这个问题,所以我希望你数学 guys/gals 可以帮助我解决伪代码,然后我可以 post 一个我们可以 运行 通过的函数浏览器进行测试。
这个挑战对我来说有点难解释,所以请原谅我,我会像谜语一样给出它:
You're trying to find your test score as a percent. You know that for a normal percent, expressed as a decimal from
0.00 - 1.00
, you'd do something likeactual_test_score / max_test_score
.However, you're not looking for any old percent. You'd like to express your percent as a decimal within a specified range of decimal numbers. For example, you'd like (given an
actual_test_score
and amax_test_score
) to get your test score as a percent from0.50 - 1.00
.How can you calculate
actual_test_score / max_test_score
expressed as a percentage between a range of0.50 - 1.00
, instead of your typical range that is0.00 - 1.00
.
我花了一些时间来解释这一点(这可能不是最好的解释),因为这不是我必须经常解释的事情,但我 运行 时不时地也会这样做。
如果我没看错,你希望 0
表示为 0.5
吗?
在那种情况下,您需要将您的数字乘以分数范围; highest_score - lowest_score
在您的情况下似乎是 1 - 0.5
。
然后将 lowest_score
添加到您的结果中。
这里是粗函数形式的例子;
function relative_percentage (score, max_score, min_score) {
// score should be a value from 0 to 1.
return score * (max_score - min_score) + min_score;
}
这是从[0,1]到[newMin,newMax]的线性范围映射,这是从[oldMin,oldMax]到[newMin,newMax]的一般线性范围映射的特例:
function linearRemap(value, oldMin, oldMax, newMin, newMax) {
var newScale = newMax - newMin;
var valueAsPct = (val - oldMin) / (oldMax - oldMin);
var scaledValue = valueAsPct * newScale;
var shiftedAndScaledValue = scaledValue + newMin;
return shiftedAndScaledValue;
}
因此这里的有限情况允许您跳过百分比计算行:
function linearRemapPct(valueAsPct, newMin, newMax) {
var newScale = newMax - newMin;
var scaledValue = valueAsPct * newScale;
var shiftedAndScaledValue = scaledValue + newMin;
return shiftedAndScaledValue;
}
或者只是
function linearRemapPct(valueAsPct, newMin, newMax) {
return (valueAsPct * (newMax - newMin) + newMin);
}
如果你想要一些更小心的精度损失:
function linearRemapPct(valueAsPct, newMin, newMax) {
return (valueAsPct * newMax) + (1 - valueAsPct) * newMin;
}