【发布时间】:2021-12-16 10:15:37
【问题描述】:
我从这个链接https://www.dcode.fr/lagrange-interpolating-polynomial 实现了一个公式来计算坐标之间的某种分数。
结果值在坐标下按预期工作
const coordinates = [
[0, 100],
[2.5, 70],
[10, 30],
]
其中 y 轴是偶数,但 y 值如 67、33 未按预期工作。
function getScore (thresholds, macro) {
let value = 0
for (let j = 0; j < thresholds.length; j++) {
let temp = 1
for (let i = 0; i < thresholds.length; i++) {
if (i !== j) {
temp *= (macro - thresholds[i][0]) / (thresholds[j][0] - thresholds[i][0])
}
}
value += thresholds[j][1] * temp
}
return value
}
console.log(
'Expecting Something above 33 but get 31',
getScore(
[
[0, 100],
[2.5, 66],
[10, 33],
],
9
)
)
console.log(
'Expecting Something above 30 but and got 31',
getScore(
[
[0, 100],
[2.5, 70],
[10, 30],
],
9
)
)
我的代码有误吗?
谢谢,
【问题讨论】:
标签: javascript algorithm