在处理大量的分数分配时,舍入误差和剩余的重新分配成为重大挑战。这些问题不仅限于财务计算;它们可以发生在其他领域,例如资源分配、任务调度或预算分配。在本文中,我们演示了一种使用 javascript 中的 big.js 库进行验证和测试的方法,以实现精确分配,同时有效处理舍入和剩余重新分配。
想象一个场景,您需要根据多只股票各自的百分比分配大量资金。例如:
要求是:
使用 big.js 库,我们可以通过任意精度算术来应对这些挑战。这是完整的解决方案:
const big = require("big.js");
function allocatemoney(amount, allocations) {
// step 1: convert percentages to rational numbers
let totalpercent = new big(0);
for (let key in allocations) {
totalpercent = totalpercent.plus(new big(allocations[key]));
}
const allocationratios = {};
for (let key in allocations) {
allocationratios[key] = new big(allocations[key]).div(totalpercent);
}
将总金额转换为美分并进行初始舍入:
const totalcents = new big(amount).times(100).tofixed(0); // convert amount to cents
const allocatedcents = {};
for (let key in allocationratios) {
allocatedcents[key] = allocationratios[key].times(totalcents).tofixed(0, 0); // convert to int (round down)
}
计算剩余的美分,并根据余数公平分配:
let distributedtotal = new big(0);
for (let key in allocatedcents) {
distributedtotal = distributedtotal.plus(new big(allocatedcents[key]));
}
const remainingcents = new big(totalcents).minus(distributedtotal).tofixed(0);
// sort allocations by fractional remainder descending for redistribution
const fractionalremainders = {};
for (let key in allocationratios) {
const allocated = allocationratios[key].times(totalcents);
const fractionalpart = allocated.minus(allocated.tofixed(0));
fractionalremainders[key] = fractionalpart;
}
const sortedkeys = object.keys(fractionalremainders).sort((a, b) => {
if (fractionalremainders[b].gt(fractionalremainders[a])) {
return 1;
}
if (fractionalremainders[b].lt(fractionalremainders[a])) {
return -1;
}
return 0;
});
for (let i = 0; i < remainingcents; i++) {
const key = sortedkeys[i % sortedkeys.length];
allocatedcents[key] = new big(allocatedcents[key]).plus(1).tofixed(0);
}
最后,将分配换回美元:
const allocateddollars = {};
for (let key in allocatedcents) {
allocateddollars[key] = new big(allocatedcents[key]).div(100).tofixed(2); // convert cents to dollars with 2 decimals
}
return allocateddollars;
}
以下是如何使用 allocatemoney 函数在股票之间分配资金:
const totalamount = "1234567890123456.78"; // a very large total amount
const stockallocations = {
"stock a": "50.5", // 50.5%
"stock b": "30.3", // 30.3%
"stock c": "19.2", // 19.2%
};
const result = allocatemoney(totalamount, stockallocations);
console.log("allocation:");
console.log(result);
// calculate total allocated
let totalallocated = new big(0);
for (let key in result) {
totalallocated = totalallocated.plus(new big(result[key]));
}
console.log(`total allocated: $${totalallocated.tofixed(2)}`);
对于给定的输入,输出为:
Allocation:
{
'Stock A': '623456784512345.67',
'Stock B': '374074070707407.41',
'Stock C': '237037034903703.70'
}
Total Allocated: $1234567890123456.78
使用big.js进行精确算术:
big.js 库通过避免浮点错误来确保准确性。
公平处理剩菜:
使用分数余数确定且公平地分配剩余单位。
调节总计:
全部调整后,确保分配总额与原始金额相符。
可扩展为大值:
这种方法可以无缝地处理大量资金,使其适合解决财务和资源分配问题。
按照这个方法,在任何对数值精度要求较高的场景下,都可以实现精确、公平的分配。
以上就是使用 Bigjs 进行精确分配:处理舍入和剩余重新分配的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号