首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Java中的Haversine公式计算大圆周距离时的不正确输出

用Java中的Haversine公式计算大圆周距离时的不正确输出
EN

Stack Overflow用户
提问于 2021-12-22 09:57:10
回答 3查看 280关注 0票数 2

我试图用Java中的Haversine公式计算大圆周的公里数,如下所示

代码语言:javascript
复制
/* Program to demonstrate the Floating-point numbers and the Math library.
 * The great-circle distance is the length of the shortest path between two points (x1,y1) and (x2,y2) on the surface of a sphere, where the path is constrained to be along the surface.*/
public class GreatCircle 
{
    public static void main(String[] args) 
    {
        double r = 6371.0; // Equatorial radius of the Earth
        double x1 = Math.toRadians(Double.parseDouble(args[0]));
        double y1 = Math.toRadians(Double.parseDouble(args[1]));
        double x2 = Math.toRadians(Double.parseDouble(args[2]));
        double y2 = Math.toRadians(Double.parseDouble(args[3]));

        // Compute using Haversine formula
        double distance = 2 * r * Math.asin(Math.sqrt(Math.pow(Math.sin((x2 - x1) / 2),2 + Math.cos(x2) * Math.pow(Math.sin((y2 - y1) / 2),2)));

        // Output the distance
        System.out.println(distance + " kilometers ");
    }
}

我正在运行输入java GreatCircle 60.0 15.0 120.0 105.0。预期的输出是4604.53989281927 kilometers,但我得到了13406.238676180266 kilometers。有人能指出我哪里出了问题吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-12-24 09:57:04

该公式的实施不正确。在做了以下修正之后,它就起了作用。在公式中,我们取整个表达式的弧罪恶。

代码语言:javascript
复制
        // Compute using Haversine formula
        double distance = 2 * r * Math.asin(Math.sqrt(Math.pow(Math.sin((x2 - x1) / 2),2 + Math.cos(x2) * Math.pow(Math.sin((y2 - y1) / 2),2)));

        // Output the distance
        System.out.println(distance + " kilometers ");
    }
}
票数 1
EN

Stack Overflow用户

发布于 2021-12-27 13:53:05

您忘记计算Math.cos(x1) * Math.cos(x2)**了,这就是为什么您得到了不同的结果。

代码语言:javascript
复制
// Compute using Haversine formula<br>
double distance = 2 * r * Math.asin(Math.sqrt((Math.pow(Math.sin((x2 - x1) / 2),2) + Math.cos(x1) * Math.cos(x2) * Math.pow(Math.sin((y2 - y1) / 2),2))));```
票数 0
EN

Stack Overflow用户

发布于 2022-01-30 01:11:20

代码语言:javascript
复制
double distance = 2 * r * Math.asin(Math.sqrt(Math.pow(Math.sin((x2 - x1) / 2),2)
            + Math.cos(x2) * Math.cos(x1) * Math.pow(Math.sin((y2 - y1) / 2),2)));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70447203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档