我收到NaN,为什么?

问题描述 投票:-6回答:1

我不知道为什么我在这段代码上获得输出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") ;


    }

}
java string eclipse function
1个回答
0
投票
 (Math.sin(xAtl)) * (Math.sin(xSav)) + (Math.cos(xAtl)) * (Math.cos(yAtl - ySav));

对于您的特定输入,大于1。 Math.acos通过三角法规则返回大于1或小于-1的输入的NaN。

© www.soinside.com 2019 - 2024. All rights reserved.