Results 1 to 7 of 7
  1. #1
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Posts
    30
    Rep Power
    0

    Default Return value NaN

    Hello, I am working on this new project where we are using the great distance formula
    but everytime I run my ending result is NaN. I was researching, and people say its because you divide by 0.
    I think I have my formula correct. any suggestions?

    Java Code:
    
    public class testingFormula {
    
    	public static void main(String[] args) {
    		double lat = 34.01858902;
    		double lon = -118.2817688;
    		double lat2 =33.94530869;
    		double lon2 = -118.3994904;
    
    	
    		System.out.println("Distance =" + greatCircleArea(lat,lon,lat2,lon2));
    		
    	}
    
    	
    	static double greatCircleArea(double lat, double lon, double lat2, double lon2){  
    		
    		double latRad = Math.toRadians(lat);
    		double longRad = Math.toRadians(lon);
    		double latRad1  = Math.toRadians(lat2);
    		double longRad1 = Math.toRadians(lon2);
    		
    		double angle1 = Math.acos(Math.sin(latRad) * Math.sin(latRad1) + Math.cos(latRad) *
    				        Math.cos(latRad1) * Math.cos(longRad) - longRad1);
    		angle1 = Math.toDegrees(angle1);
    
    		double distance1 = 60 * angle1;
    		return distance1;
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Return value NaN

    I believe your formula is incorrect. Check it out here --> Great-circle distance - Wikipedia, the free encyclopedia.

    You might also want to pay special attention to the computational formulas that apply to programming algorithms.

    Regards,
    Jim
    Last edited by jim829; 03-17-2015 at 10:49 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Posts
    30
    Rep Power
    0

    Default Re: Return value NaN

    ah! Yes!
    Easy mistake :(
    Thanks Jim

    Can I post another question regarding my project? Or Shall I create a new thread?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Return value NaN

    Probably best to post another thread. It keeps the topics separate. Even if it is the same application.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Posts
    30
    Rep Power
    0

    Default Re: Return value NaN

    ah. Easy Mistake :(
    Thanks again Jim

    Could I possible ask for further assistance in my project? Or should I create a new thread?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Return value NaN

    Oh, and what is the 60 for in your final conversion. I noticed you converted to degrees before multiplying by the radius. I believe the angle should still be kept in radians.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Posts
    30
    Rep Power
    0

    Default Re: Return value NaN

    I was doing a small tutorial that had this example, it stated that every degree on a great circle of Earth is 60 nautical miles

Similar Threads

  1. Replies: 7
    Last Post: 04-02-2014, 02:15 PM
  2. return 0?????
    By faraa in forum New To Java
    Replies: 8
    Last Post: 05-06-2013, 07:31 PM
  3. return
    By Sanna.1 in forum New To Java
    Replies: 1
    Last Post: 02-17-2012, 01:38 PM
  4. Using int/int, 7/5 would return 1
    By zoe in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 11:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •