Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1901
  • Last Modified:

java: logarithm of biginteger

Hi

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

Thanks,
Mike
0
nerdmike
Asked:
nerdmike
2 Solutions
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now