且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

日志的例子()算法利用任意precision数学

更新时间:2023-02-26 19:06:19

那么你将有泰勒级数,可以改写为更好的收敛 日志的例子()算法利用任意precision数学

Well you would have the Taylor series, that can be rewritten for better convergence

要改变这个漂亮的平等纳入一个算法,你必须了解的收敛一系列工作:每学期越来越小。这种减少会发生足够快,以使总和为有限的值:LN(y)的

To transform this nice equality into an algorithm, you have to understand how a converging series work : each term is smaller and smaller. This decrease happens fast enough so that the total sum is a finite value : ln(y).

的实数好的属性,因为,你可以考虑序列收敛于LN(Y):

Because of nice properties of the real numbers, you may consider the sequence converging to ln(y) :

  • L(1)= 2/1 *(Y-1)/(Y + 1)
  • L(2)= 2/1 *(γ-1)/(Y + 1)+ 2/3 *((Y-1)/(Y + 1))^ 3
  • L(3)= 2/1 *(γ-1)/(Y + 1)+ 2/3 *((Y-1)/(Y + 1))^ 3 + 2/5 *(( Y-1)/(Y + 1))^ 5

..等。

显然,算法计算该序列很容易:

Obviously, the algorithm to compute this sequence is easy :

x = (y-1)/(y+1);
z = x * x;
L = 0;
k = 0;
for(k=1; x > epsilon; k+=2)
{
    L += 2 * x / k;
    x *= z;
}

目前一些点,你的x将变得如此小,它无助到L的有趣位数了,而不是仅修改小得多位数。当这些修改开始是太微不足道了你的目的,你可能会停止。

At some point, your x will become so small that it will not contribute to the interesting digits of L anymore, instead only modifying the much smaller digits. When these modifications start to be too insignificant for your purposes, you may stop.

因此​​,如果你想达到一个precision 1E ^ -20,集小量是合理的小于,你是好去。

Thus if you want to achieve a precision 1e^-20, set epsilon to be reasonably smaller than that, and you're good to go.

不要忘了,如果你能在日志中因式分解。如果是例如LN(A²)完全平方= 2号法律公告(一) 事实上,该系列将收敛更快时(γ-1)/(Y + 1)较小,因此当y较小(或者说,越接近1,但应该是等价的,如果你打算使用整数)。

Don't forget to factorize within the log if you can. If it's a perfect square for example, ln(a²) = 2 ln(a) Indeed, the series will converge faster when (y-1)/(y+1) is smaller, thus when y is smaller (or rather, closer to 1, but that should be equivalent if you're planning on using integers).