How to calculate logarithm in Java ME?

How can I calculate the logarithm in Java ME? There is no method in the Java ME Mathclass for it, but it is available in the Java SE class Math.

+5
source share
2 answers

It is suggested how this can be done here .

Here is the solution from this 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;
}
+6
source

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

+2
source

All Articles