- double lognum = 0;
- double logden = 0;
- while (mpz_sizeinbase(num, 10) > 10)
- {
-
- mpz_div_ui(num, num, 1e10);
- lognum += 10;
- }
- uint64_t n = mpz_get_ui(num);
- if (n == 0)
- {
- return -INFINITY;
- }
- lognum += log(n)/log(10.0);
- //Debug("%lu", mpz_get_ui(den));
- while (mpz_sizeinbase(den, 10) > 10)
+ static double logB = log(ULONG_MAX); // compiler should optimise this anyway?
+
+ // Undefined logs (should probably return NAN if -ve, not -INFINITY, but meh)
+ 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
+ // Note that denominator is not explicitly set to 1, this caused a lot of headache
+ if (abs(mpq_denref(m_op)->_mp_size) > 0)