Search code examples
javajava-mefloating-pointj2mepolish

Problems using the acos function in J2ME in eclipse


"I'm working with J2ME in eclipse, and need to use the method acos() belongs to the Math package, the question is that the package available for J2ME Math is not that function (limitations of mobiles), then I want an algorithm or method alternative to calculate the cosine.

I need the "acos" to calculate the following formula:

long2 = cood[i].getLongitud();
lat2 = cood[i].getLatitud();
dlong = (long1 - long2); 
c = dlong * degtorad;
dvalue = (Math.sin(a) * Math.sin(b))+(Math.cos(a) * Math.cos(b)*Math.cos(c)); 

dd = Math.acos(dvalue) * radtodeg; 

km = (dd * 111.302);

This function allows me from two geographical coordinates to calculate the distance in kilometers between them. If there is an alternative method to calculate this distance (where the cosine is not used), I also would be useful.

Any help on this?


Solution

  • public static double acos(double a)
    {
        final double epsilon=1.0E-7; 
        double x=a;
        do {
            x-=(Math.sin(x)-a)/Math.cos(x);
        }
        while (Math.abs(Math.sin(x)-a)>epsilon);
    
        // returned angle is in radians
        return -1*(x-Math.PI/2);
    }