1. ## Logarithms in J2Me

I was trying out the J2ME math class to calculate logarithms but it does not have the log method. Is anyone aware of a third party library which can perform logarithmic operations on primitive float datatype.

2. I just happened to come across this while reading some other stuff. Have not tried it out myself yet. Hopefully it will be of use to you. Called the MathFP library.

http://www.jscience.net/

3. ## Re: Logarithms in J2Me

Here is my log code for CLDC 1.1. You may increase accuracy by changing the 7 in the for loop.

-Pelle

Code:
```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;
}```

4. ## Re: Logarithms in J2Me

A O(log(n)) implementation (can also be written without recursion) -

Code:
```private static double pow(double base, int exp){
if (exp == 0) return 1;

double t = pow(base, exp / 2);

if (exp % 2 == 1) {
return t*t*base;
} else {
return t*t;
}
}```

5. ## Re: Logarithms in J2Me

I have a question. What's the name of algorithm pjohnsen uses to compute the value of the logarithm? Where can I read about this algorithm? Where can I find how it works? thanx

6. ## Re: Logarithms in J2Me

This works if base is e, how about if base is 10?

7. ## Re: Logarithms in J2Me

Compute to any base you like:

Code:
```public static double log(double x, double base) {
return log(x) / log(base);
}```
Graham.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•