java: logarithm of biginteger

Posted on 2006-05-12
Last Modified: 2012-08-13

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

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.


       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.
    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);

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    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 …

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now