简体   繁体   English

Decimal.js tan的精度问题

[英]Precision issue with Decimal.js tan

I have been using Decimal.js to increase the precision of my function that calculates the mth positive root of a = tan(a) through trial and error. 我一直在使用Decimal.js来提高函数的精度,该函数通过反复试验来计算a = tan(a)的第m个正根。 It works, however it returns a "Precision limit exceeded" error for nTan(504) (would return 4.4934... to 505 digits) and greater. 它可以工作,但是会为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();
}

My question(s) are: 我的问题是:

  1. Why won't Decimal.js calculate past 504 digits when it advertises the capacity for up to and including 1e+9 digits? 为什么Decimal.js在广告容量达到和包括1e + 9位数字时不计算504位数字?
  2. Is there an alternative node or JS API that would support this program to a greater precision? 是否有其他节点或JS API可以更精确地支持此程序?

1000000000 is the maximum permitted value for the decimal.js precision setting, but that does not mean that the trigonometric methods can return a result to that number of significant digits. 1000000000decimal.js 精度设置的最大允许值,但这并不意味着三角方法可以将结果返回到该有效数字位数。

The limit to the precision of the trigonometric methods is determined by the precision of the value of Pi in the source code. 三角法精度的极限取决于源代码中Pi值的精度。 It is hard-coded in the decimal.js file as the string variable PI , and has a precision of 1025 digits. 它在十进制.js文件中作为字符串变量PI硬编码,并且具有1025位的精度。

This means that the precision limit for the cos , sin and tan methods is up to about 1000 digits, but the actual figure depends on the precision of the argument passed to them. 这意味着cossintan方法的精度极限最高可达1000位数,但实际数字取决于传递给它们的参数的精度。 To calculate the actual figure use 计算实际数字使用

maximum_result_precision = 1000 - argument_precision maximum_result_precision = 1000-参数精度

For example, the following both work fine 例如,以下两种方法都可以正常工作

Decimal.set({precision: 991}).tan(123456789);

Decimal.set({precision: 9}).tan(991_digit_number);

as, for each, the result precision plus the argument precision, ie 991 + 9 and 9 + 991 , is less than or equal to 1000 . 因为每个结果精度加上参数精度,即991 + 99 + 991小于或等于1000

This is why your program fails when you try and calculate the tan of an argument with more than 500 digits to a precision of more than 500 digits. 这就是为什么当你尝试并计算出你的程序失败tan有超过500位的说法,以超过500位的精度。

To do it would require Pi to a higher precision - and that can only be done, and can be done simply, by editing the value of PI in the source code, ie add more digits to it. 要做到这一点,就需要Pi具有更高的精度-这只能通过编辑源代码中PI的值(即在其上添加更多的数字)来实现,并且可以简单地实现。 The time taken by the methods will then be the limiting factor. 这些方法所花费的时间将成为限制因素。

I am the library's author and I need to add this to its documentation. 我是图书馆的作者,需要将其添加到其文档中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM