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

2023-09-11 06:28:14 作者:゛当誓言变成谎言づ

我希望找到我可以在PHP中实施,以获得使用任意precision数学的整数的自然对数()的算法。我通过GMP库的PHP叠加库有限(见的http:// PHP。净/手动/ EN / ref.gmp.php 在PHP中使用GMP的功能。)

I'm looking to find an algorithm that I can implement in PHP to get the natural log() of an integer number using arbitrary precision maths. I'm limited by the PHP overlay library of the GMP library (see http://php.net/manual/en/ref.gmp.php for available GMP functions in PHP.)

如果你知道一个通​​用的算法,可以转化为PHP的,这也将是一个很好的起点。

If you know of a generic algorithm that can be translated into PHP, that would also be a useful starting point.

PHP支持本地日志()函数,我知道,但我希望能够使用任意的precision工作了这一点。

PHP supports a native log() function, I know, but I want to be able to work this out using arbitrary precision.

与之密切相关的是得到一个EXP()函数。如果我的小学生数学提供我的权利,得到一个可能会导致其他。

Closely related is getting an exp() function. If my schoolboy Maths serves me right, getting one can lead to the other.

推荐答案

那么你将有泰勒级数,可以改写为更好的收敛

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).

 
精彩推荐