我正在获取NaN,为什么?

时间:2018-09-26 15:22:55

标签: java string eclipse function

我不确定为什么我会在此代码上得到输出NaN,我可以借助你们的一些帮助进行查找,谢谢!

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") ;


    }

}

1 个答案:

答案 0 :(得分:0)

 (Math.sin(xAtl)) * (Math.sin(xSav)) + (Math.cos(xAtl)) * (Math.cos(yAtl - ySav));
对于您的特定输入,

大于1。根据三角法则,对于大于1或小于-1的输入,Math.acos返回NaN。