Is there a GMP logarithm function?

Is there a logarithm function implemented in the GMP library?

+7
source share
3 answers

There is no function in GMP. Only in MPFR.

+3
source

I know that you did not ask how to implement this, but ...

You can implement crude using the properties of the logarithms: http://gnumbers.blogspot.com.au/2011/10/logarithm-of-large-number-it-is-not.html

And the GMP library internals: https://gmplib.org/manual/Integer-Internals.html

(Edit: basically you just use the most significant โ€œdigitโ€ of the GMP view, since the basis of the view is huge, B^N much bigger than B^{N-1} )

Here is my implementation for Rationals.

  double LogE(mpq_t m_op) { // log(a/b) = log(a) - log(b) // And if a is represented in base B as: // a = a_N B^N + a_{N-1} B^{N-1} + ... + a_0 // => log(a) \approx log(a_N B^N) // = log(a_N) + N log(B) // where B is the base; ie: ULONG_MAX static double logB = log(ULONG_MAX); // Undefined logs (should probably return NAN in second case?) if (mpz_get_ui(mpq_numref(m_op)) == 0 || mpz_sgn(mpq_numref(m_op)) < 0) return -INFINITY; // Log of numerator double lognum = log(mpq_numref(m_op)->_mp_d[abs(mpq_numref(m_op)->_mp_size) - 1]); lognum += (abs(mpq_numref(m_op)->_mp_size)-1) * logB; // Subtract log of denominator, if it exists if (abs(mpq_denref(m_op)->_mp_size) > 0) { lognum -= log(mpq_denref(m_op)->_mp_d[abs(mpq_denref(m_op)->_mp_size)-1]); lognum -= (abs(mpq_denref(m_op)->_mp_size)-1) * logB; } return lognum; } 

(A lot later) Coming back to this 5 years later, I just think it's cool that the basic concept of log(a) = N log(B) + log(a_N) even appears in native floating point implementations, here is glibc one for ia64 And I used it again after meeting this question

+5
source

Here it is: https://github.com/linas/anant

Provides a real and complex logarithm of gnu mp, exp, sine, cosine, gamma, arctan, sqrt, polylogarithm Riemann and Hurwitz zeta, confluent hypergeometer, sine topologies, etc.

+1
source

All Articles