IntelliJ Java IDE
The moose likes Beginning Java and the fly likes Math equation help Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Math equation help" Watch "Math equation help" New topic
Author

Math equation help

Mark Captain
Ranch Hand

Joined: Nov 01, 2011
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
Saloon Keeper

Joined: Oct 27, 2005
Posts: 18371

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 26720
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

Joined: May 03, 2008
Posts: 3221

Mark Captain wrote:(i have already imported the math class)

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


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Saloon Keeper

Joined: Oct 27, 2005
Posts: 18371

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

Joined: Mar 12, 2011
Posts: 547
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.


OCPJP 6
Rob Spoor
Saloon Keeper

Joined: Oct 27, 2005
Posts: 18371

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

Joined: Mar 12, 2011
Posts: 547
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
Sheriff

Joined: Oct 13, 2005
Posts: 26720
I couldn’t get the algorithm to work; I got 69.1702 miles.
 
 
subject: Math equation help
 
Threads others viewed
Math class objectives
How far am I from directly opposite the Ranch?
cut a certain stripe of a square
java.land.Math methods knowhow
Math Puzzle - Where Are You?
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com