开发者

How to calculate logarithm in Java ME?

开发者 https://www.devze.com 2023-01-05 10:14 出处:网络
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开发者_C百科 Math class.It is suggested how it can be done here.

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开发者_C百科 Math class.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消