gpt4 book ai didi

javascript - 该算法会累积 chop 误差吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:00:15 24 4
gpt4 key购买 nike

我计划编写一个允许网页评级系统的第 3 方脚本,但我只希望它嵌入的每个网页在数据库中占据一行(或者更准确地说是一个文档,因为我正在使用NoSQL 路线)。我的评级伪代码如下所示:

function update(page, rate) {
collection.get({
page: page
}, function callback(err, doc) {
if (!err) {
var rating = doc.rating,
votes = doc.votes;

collection.update({
page: page
}, {
votes: votes + 1,
rating: (rating * votes + rate) / (votes + 1) // here's the iffy part
});
}
});
}

有没有办法改进算法以避免浮点值的 chop 错误,或者这从一开始就不是问题?

最佳答案

Is there a way to improve the algorithm in order to avoid truncation error for floating point values, or will this not be a problem in the first place?

是的。将所有评分的总和和评分的数量作为整数存储在数据库中,并且只在显示时计算这些的平均值。

关于javascript - 该算法会累积 chop 误差吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37008933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com