【发布时间】:2017-10-14 11:46:04
【问题描述】:
我一直在使用 Decimal.js 来提高我的函数的精度,该函数通过反复试验计算 a = tan(a) 的第 m 个正根。它可以工作,但是对于 nTan(504) 会返回“超出精度限制”错误(将返回 4.4934... 到 505 位)或更大。
var Decimal = require("decimal.js");
var fs = require("fs");
function nTan (acc, m) {
var test = [1], acc = (parseInt(acc) || 15) + 1;
Decimal.set({precision: acc});
var n = new Decimal(fs.readFileSync("result.txt", "utf-8") || 4.4).toString();
while (n.length + test.length - 2 < acc) {
var dec = (new Decimal(n + test.join("")));
if (dec.tan().cmp(n + test.join("")) >= 0) {
test[test.length - 1]--;
test.push(1);
} else test[test.length - 1]++;
if (test[test.length - 1] == 10) { test[test.length - 1] = 9; test.push(1); }
}
return (new Decimal(n + test.slice(0, -1).join(""))).plus(Math.PI * (parseInt(m) || 0)).toString();
}
我的问题是:
- 为什么 Decimal.js 在宣传最多 1e+9 位的容量时不计算过去的 504 位?
- 是否有替代节点或 JS API 可以更精确地支持该程序?
【问题讨论】:
-
我不明白你的观点/问题。调用
nTan(504)时,您将精度设置为 504 位。这里:Decimal.set({precision: acc}) -
如果我调用 nTan(504) Decimal.js 会产生“超出精度限制”错误。任何小于该值的值(例如 nTan(250))都将按预期返回
-
好的。当您超过 10 自然对数的内部存储字符串的长度时,函数
getLn10中会引发此错误。在存储库中,这是 1024 个字符。有没有可能,您使用的是旧版本的 lib,它存储了一个较短的字符串? -
错误表明它来自 Decimal.js 的 tan 函数:在 ...,在 nTan,在 Decimal.P.tangent.P.tan,在 Decimal.P.sine.P.sin,在 toLessThanHalfPi,在 getPi,在错误:[DecimalError] 超出精度限制
标签: javascript node.js math bignum decimal.js