java: logarithm of biginteger

Hi

I want to know how to find the logarithm of a BigInteger object in Java.

Thanks,
Mike
nerdmikeAsked:
Who is Participating?
 
InteractiveMindCommented:
The bitLength() method will compute:

   ceil(log2(this < 0 ? -this : this+1))

Which is an estimation of log, with base 2.

So:

   BigInteger a = new BigInteger( "7298472342" ) ;
   
   int base = 123 ;
   
   double log = a.bitLength() / ( Math.log(base) / Math.log( 2 ) ) ;


..I'll see if there's a more accurate way though.
0
 
rbvoigtCommented:
I'll assume you're not going to pass in negative numbers...  this ought to be pretty good:

double log(BigInteger big)
{
int shiftcount = big.bitLength() - 64;
if (shiftcount < 200) return Math.log(big.doubleValue());
return Math.log(big.shiftRight(shiftcount).longValue()) + shiftcount * Math.log(2.0);
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.