Solved

# java: logarithm of biginteger

Posted on 2006-05-12
1,361 Views
Hi

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

Thanks,
Mike
0
Question by:nerdmike

LVL 25

Accepted Solution

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

LVL 1

Assisted Solution

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

### Suggested Solutions

Adobe Customization Wizard XI issues 26 137
fizzArray2 challenge 1 44
wordlen challenge 3 32
python sqlite question 11 27
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …