programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Math equation help

Mark Captain
Ranch Hand
Posts: 30
Hi, im working on an equation to find the distance between to GPS locations and am having trouble where my variable "c" ends up as 1 when it should not, then resulting in the output as the radius of the earth.

Here is the equation i was given to work off of

d = R ∗ C, wℎere
R = 3963.16 miles (radius of the earth)
a = (sin(0.5 * abs(lat1 - lat2)))^2 + cos(lat1) * cos(lat2) * (sin(0.5 * abs(long1 - long2)))^2
c = 2 * atan2(√a,√(1-a))

here is my code, i should be recieving an output of 69.1703 but always output 3963.16 (i have already imported the math class)

Rob Spoor
Sheriff
Posts: 21135
87
You need to convert the latitude and longitude differences inside the abs calls to radians as well. Right now, part of your calculation uses degrees and another uses radians.

Campbell Ritchie
Marshal
Posts: 56600
172
Don’t use Math.pow(xxx, 2).
Use xxx * xxx for squaring. You will get slightly better precision and performance.
I have had to break some of the lines in your code, because they made the display too wide for my screen. Also look at our style guide under “formatting”, where it tells you why you ought to use spaces for indenting.
I would suggest you divide that formula up into smaller stages. If you have something like sin2(x + y * z) break it into

Darryl Burke
Bartender
Posts: 5167
11
Mark Captain wrote:(i have already imported the math class)

You never need to import any class from the java.lang package.

Rob Spoor
Sheriff
Posts: 21135
87
I think Mark meant he has import static java.lang.Math.*; in his import list; otherwise he wouldn't be able to call methods like toRadians like this.

dennis deems
Ranch Hand
Posts: 808
Rob Spoor wrote:I think Mark meant he has import static java.lang.Math.*; in his import list; otherwise he wouldn't be able to call methods like toRadians like this.

I detest this practice. Unless I have the API of every Java library memorized, I do not know where methods like toRadians come from. Static imports make code a lot harder to read; I would a thousand times rather see Math.toRadians.

Rob Spoor
Sheriff
Posts: 21135
87
Don't get me wrong, I fully agree with you. I never use static imports myself. However, some people think differently and that's fair; everybody is entitled to their opinion.

dennis deems
Ranch Hand
Posts: 808
Rob Spoor wrote:Don't get me wrong, I fully agree with you. I never use static imports myself. However, some people think differently and that's fair; everybody is entitled to their opinion.

Granted; and I suppose I can imagine that perhaps, if one works a great deal of the time with equations like these, one might prefer the code to resemble them visually as much as possible.

Campbell Ritchie
Marshal
Posts: 56600
172
I couldn’t get the algorithm to work; I got 69.1702 miles.

 It is sorta covered in the JavaRanch Style Guide.