Examples of the log () algorithm using arbitrary precision mathematics

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

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

PHP supports its own log () function, I know, but I want this to be possible using arbitrary precision.

Closely related is getting the exp () function. If my Maths student suits me, then you can lead another to another.

+1
source share
1 answer

Well, you will have a Taylor series that can be rewritten for better convergence Improved taylor series of ln: 2 sum_ {k = 0 .. + inf} 1 / (2k + 1) * ((y-1) / (y + 1)) ^ (2k + 1)

To convert this good equality into an algorithm, you must understand how convergent series work: each member is smaller and smaller. This decrease is fast enough, so the total is the final value: ln (y).

Due to the good properties of real numbers, you can consider a sequence converging to ln (y):

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

.. etc.

Obviously, the algorithm for calculating this sequence is simple:

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

, 1e ^ -20, epsilon , .


, . , , ln (aยฒ) = 2 ln (a) , , (y-1)/(y + 1) , , y (, 1, , ).

+4

All Articles