×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    31

    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. #2
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249
    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. #3

  4. #4
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Copenhagen, Denmark
    Posts
    110

    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;
    }

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    9

    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;
    	}
    }

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    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

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    4

    Re: Logarithms in J2Me

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

  8. #8
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    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
  •  
×