-8

I am not sure why I am getting the output NaN on this code, I could use some help from you guys to look it over, thanks!

package ch_4_PA;

public class PA_4_3 {

    public PA_4_3() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double xAtl = Math.toRadians(33.7489954) ;
        double yAtl = Math.toRadians(-84.3879824) ;

        double xOrl = Math.toRadians(28.538355) ;
        double yOrl = Math.toRadians(-81.3792365) ;

        double xSav = Math.toRadians(32.0835407) ;
        double ySav = Math.toRadians(-81.0998342) ;

        double xCha = Math.toRadians(35.2270869) ;
        double yCha = Math.toRadians(-80.8431267) ; 
        //sets coordinates for each city

        double radius = 6371.01 ;
        //radius of Earth
        double distanceAtltoSav = radius * Math.acos( (Math.sin(xAtl)) * (Math.sin(xSav)) + (Math.cos(xAtl)) * (Math.cos(yAtl - ySav))) ; 
        //calculates distance from Atl to Sav
        double distanceOrltoCha = radius * Math.acos( (Math.sin(xOrl)) * (Math.sin(xCha)) + (Math.cos(xOrl)) * (Math.cos(yOrl - yCha))) ;
        //Calculates distance from Orl to Cha
        double area; 
        //triangles will be h=distance Atl to Sav / 2 and b = distance Orl to Cha
        double triarea = (distanceAtltoSav / 2) * (distanceOrltoCha) * 0.5 ; 
        area = triarea * 2 ; 

        System.out.println("The area between the cities is: " + area + "km^2") ;


    }

}
VGR
  • 40,506
  • 4
  • 48
  • 63
JVinitsky
  • 1
  • 1
  • 1
    Why do you define `area` before the calculation and set it after? `double area = triarea * 2 ;` should be just fine. Besides that, did you debug to check which value is the first to be NaN? Any further calculation will then have NaN as its result. – Thomas Sep 26 '18 at 15:24
  • Lmao I didn’t even notice, just messy haha – JVinitsky Sep 26 '18 at 15:25
  • 4
    Have you tried [debugging](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems)? – QBrute Sep 26 '18 at 15:27
  • I have, my distance variables seem to be reporting as NaN but idk why – JVinitsky Sep 26 '18 at 15:59

1 Answers1

0
 (Math.sin(xAtl)) * (Math.sin(xSav)) + (Math.cos(xAtl)) * (Math.cos(yAtl - ySav));

is greater than 1 for your specific input. Math.acos returns NaN for input greater than 1 or less than -1 by trigonometry rules.