Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the log 2 base of a double variable?

I'm trying to calculate the log 2 base of some double form numbers but it seems that there isn't a standard method from a libraly for that.

I tried out this code,error occurs though.

        double entropy=0;
        for(int i=0; i<26;i++){
            entropy+=-possibilityCounter[i]*log2(possibilityCounter[i]);
        }

NOTE: possibilityCounter table is full of double variables e.g 0.00133536

Any suggestion?

like image 932
Michael Pnrs Avatar asked Oct 21 '25 16:10

Michael Pnrs


2 Answers

You can calculate the log base 2 from the natural log like this

public double log2(double v) {
    return Math.log(v) / Math.log(2);
}
like image 90
b.GHILAS Avatar answered Oct 23 '25 06:10

b.GHILAS


Indeed in java.util.Math you have logs for base e and 10.

However you can use the following formula:

log2(x) = log(x)/log(2)
like image 44
Mark Bramnik Avatar answered Oct 23 '25 04:10

Mark Bramnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!