tags:

views:

84

answers:

3

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.

+4  A: 

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;
}
npinti
+2  A: 

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.

Thomas Padron-McCarthy
A: 

thanks why log or some other methods isnt available in java mes math class ???

mahdi
Java ME is a much simpler version of Java, intended to run on very limited devices - not just mobile phones, but pagers and things too. (The first version didn't have any floating point at all, just integers.)
Thomas Padron-McCarthy