Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate logarithm in Java ME?

Tags:

java

java-me

How can I calculate logarithm in Java ME? There isn't any method in Java ME's Math class for it, but it is available in the Java SE's Math class.

like image 434
mahdi Avatar asked Jun 28 '10 12:06

mahdi


2 Answers

It is suggested how it can be done here.

Here's a solution from that site:

private static double pow(double base, int exp){
    if(exp == 0) return 1;
    double res = base;
    for(;exp > 1; --exp)
        res *= base;
    return res;
}

public static double log(double x) {
    long l = Double.doubleToLongBits(x);
    long exp = ((0x7ff0000000000000L & l) >> 52) - 1023;
    double man = (0x000fffffffffffffL & l) / (double)0x10000000000000L + 1.0;
    double lnm = 0.0;
    double a = (man - 1) / (man + 1);
    for( int n = 1; n < 7; n += 2) {
        lnm += pow(a, n) / n;
    }
    return 2 * lnm + exp * 0.69314718055994530941723212145818;
}
like image 92
npinti Avatar answered Oct 04 '22 08:10

npinti


I have used Nikolay Klimchuk's "Float11" class for floating-point calculations in Java ME. The original link seems broken, but it's available here.

like image 39
Thomas Padron-McCarthy Avatar answered Oct 04 '22 07:10

Thomas Padron-McCarthy